Git Guide

This article is a collection of git commands that I commonly use. Essentially it is a personal cheat sheet for when I am doing stuff with git.

Let's start out with some basic commands:

git init initializes a local git repository in the current folder.
git add -p asks you for each change that you have made whether you want to stage it for a commit.
git commit -m "message" commits the currently staged changes with the commit message as the text between the quotes.
git log see a list of commits in this repository.

Git can work fine locally, but you can also push it to a remote server, which helps with collaboration. Here are a couple of commands:

git clone
git remote -v prints the current remote URLs and their names.
git remote add origin url_of_remote
git remote get-url name_of remote
git remote set-url name_of_remote new_url_of_remote
git push pushes current changes to the remote repo.
git pull pulls changes from the remote repo to your local folder.

Add to ~/.gitconfig:
default = simple
name = Marno
email = [email protected]
graph-full = log --graph --all --decorate
graph = log --graph --oneline --all --decorate
patch = add -p
fwl = push --force-with-lease
contrib = commit -v

mkdir NAME
git clone --bare URL git
cd git
git worktree add ../BRANCH BRANCH
cd ..


Submodules are a great feature that allows nesting of git repositories:

git submodule update --recursive --init if you have cloned a repository that contains submodules, you can use this command to initialize all of them recursively.
git submodule add remote_url destination_folder if you want to add a submodule to one of your repositories you can use this command.

Removing a git submodule:

  1. mv a/submodule a/submodule_tmp
  2. git submodule deinit -f -- a/submodule
  3. rm -rf .git/modules/a/submodule
  4. git rm -f a/submodule


To merge branch1 into branch2:
git checkout branch2
git merge branch1

Create a new branch branch2 based on content of branch1:
git checkout branch1
git checkout -b branch2

If you have a commit on a detached HEAD and want to add it to branch1:
git checkout -b tmp_branch
git checkout branch1
git rebase tmp_branch
git branch -d tmp_branch

Amending history

If you would like to see a diff between two commits look up their hex values in git log and use the following command (--word-diff is optional and is useful for written text): git diff --word-diff commit1..commit2

To change just the last commit: git commit --amend

If you have changes you don't want to commit, adding files to the .gitignore file is one option. Another is stashing your work:
git stash push
git stash list
git stash pop

Rebasing, replace X by the amount of commits you want to go back:
git rebase -i HEAD~X
git push --force-with-lease
In the rebase, you can reorder commits and I often use squash to combine commmits. It is better to use force with lease as opposed to a regular force because this first checks whether the head of the remote is still what you expect it to be. This reduces the chance of accidentally overwriting work that someone else may have pushed to the remote in the meantime.


Show, add and push tags:
git tag
git tag -a vX.Y -m "version message for X.Y"
git push --tags