Git Essentials

4min read
Tags: Git

This blog post is not meant to be an introduction to Git but rather lists various Git commands which I regularly use when I started working in a professional work environment. We all know that things can sometimes get tricky if you start to work with multiple people on the same project. Therefore I decided to write them down so I can always refer back to them if I ever need to:

The basics

The absolute minimum you should memorize when using Git on your own:

- Initialize Git repository in current directory
git init

- Clone a remote repository
git clone <url> 

- Get/Set Git username and email on machine
git config --list
git config --global user.email <new-email>
git config --global user.name <new-username>

- List all untracked, unstaged and staged files
git status

- Show Git commit history
git log
git log -n <number-of-commits>
git log --oneline
git log --graph

- Stage files/directories
git add <directory-or-file>

- Commit staged files
git commit -m "<message>"

Undoing changes

For moments when you messed up and want to start from a clean slate again:

- Start an interactive rebase of current branch on another
git rebase -i <branch-name>

- Drop all changes and point to specific commit
git reset --hard <SHA>

- Restore a specific revision of a file
git restore --source <SHA> <file-name>

- Edit last commit
git commit --amend

Disclaimer: If there is one Git command I have to recommend newbies to further to look into, it is definitely git rebase -i <branch-name>. Especially if you work in a team environment where every feature/ticket is represented in its own Git branch, it can help you to tidy things up before or even after you publish your branch to other.

Working with branches

It’s good practice to utilize branches when you work on a new feature or bug fixing instead of solely relying on the main/master branch:

- List all branches
git branch

- Checkout to an existing branch
git checkout <branch-name>

- Create new branch
git checkout -b <new-branch-name>

- Rename current branch
git branch -M <new-branch-name>

- Rename arbitrary branch
git branch -M <current-branch-name> <new-branch-name>

- Delete branch
git branch -D <branch-name>

- Merge branch into current one
git merge <branch-name>

Remote repositories

Remote repositories are unavoidable in case you want to host your code on remote servers or intend to work with other people over the Internet:

- List all remote repositories
git remote -v

- Add remote repository
git add <short-name> <url>

- Remove remote repository
git remote remove <short-name>

- Fetch latest changes
git fetch <short-name>

- Set pull option to fast-forward only (important!)
git config --global pull.ff only

- Pull latest changes into current branch (fetch and merge)
git pull <short-name> <branch-name>

- Push changes to existing remote branch
git push <short-name> <branch-name>

- Create new remote branch and track it
git push -u <short-name> <branch-name>

- Overwrite remote branch
git push -f <short-name> <branch-name>

Resolving conflicts

Whenever I encounter any sort of conflicts, I always refer back to VS Code’s resolving window. Personally, it is much easier for me to resolve conflicts if I have a visual representation of which content is overwritten and what changes are incoming. Plus, you get syntax highlighting on top!

Merge tool from VS Code (taken from https://code.visualstudio.com/docs/editor/versioncontrol#_merge-conflicts)Merge tool from VS Code (taken from https://code.visualstudio.com/docs/editor/versioncontrol#_merge-conflicts)

Utilize the stash

When you want to set aside your progress and retrieve it later on:

- Stash all current and untracked changes
git stash --include-untracked
git stash -u

- List stash content
git stash list

- Pop n-th stash content to current work directory
git stash pop stash@{n}

- Apply n-th  stash content to current work directory
git stash apply stash@{n}

Pointing to Git commits

In case you do not want to go the long way and address every commit by its SHA value:

- Current ref pointer is pointing towards
HEAD

- Get parent from current pointer
HEAD~

- Get n-th parent from current pointer (generation-wise)
HEAD~n

- Get m-th parent from current pointer (e.g., after merge)
HEAD^m

- Commit from remote
<short-name>/<ref>

The internals

I also recommend everybody to look behind the scenes and read up on how Git actually works under the hood (e.g., the internals of the .git folder, what exactly a branch is or the structure of a simple Git commit). Having the knowledge about the technicality of Git, gives you more confidence and understanding behind each command you execute.

Next.js
Mantine
Vercel