Git Notes

In this article, I will discuss git usage, i.e., the most commonly used commands, assuming that you have a basic understanding of git. Main aim is to create a cheat sheet for us to check whenever we get stuck while using git. I assume you are familiar with git, and you have knowledge about working directories, staging area, repo and etc. Well, let’s begin.


  1. Initial Configs
  2. Commits
  3. Logs
  4. Branches
  5. Merging
  6. Diff
  7. Stash
  8. Checkout
  9. Restore
  10. Revert
  11. Reset
  12. References

Initial Configs

The first thing after you install git on your machine is to make some settings. You need to specify your name and email as follows:

git config --global "your name"
git config --global ""

If your want to check these you can type the following:

git config --global
git config --global

After handling the initial configuration, we wish to start a project with git, thus we should create a git repo in the folder we work in as follows:

git init

We can check the status of our repo by typing:

git status


Before we commit the changes we made, we first need to move them to the staging area as follows:

git add <filename>

After we carry the changes into the staging area, we commit them into the repo with a message as follows:

git commit -m "message for the commit"

However, I usually make a typo in the commit message, which we can correct just after the commit as follows:

git commit --amend "new message for the previous commit"


Sometimes, we might want to see our commit history and hash number related to each commit by typing:

git log

If we want to see a shorter version:

git log --oneline


Sometimes, we make some changes which we are not sure to take into the production, try new things or collaborate with others. After the changes, we might want to revoke them, or sometimes we combine out work with others. In that case, we create branches. To create a new branch we can use the followings:

git branch <branch-name>
git switch -c <branch-name>
git checkout -b <branch-name>

We can change the branch we are working on as follows:

git switch <branch-name>
git checkout <branch-name>

If we want to delete a branch:

git branch -D <branch-name>
git branch --delete --force <branch-name>

In order to rename a branch

git branch -m <new-branch-name>


Let’s pretend you created a website in your main branch and in another branch you created a dark theme for your website to try it out, finally you decided to keep the dark theme. What you are going to do is to merge those branches. Here is how you can do it:

git switch main
git merge dark-theme

After creating the dark-theme brach, if you have made no changes in your main branch, when you merge it, there won’t be any conflicts and it is going to be a fast-forward merge. The commits in the dark-theme branch is carried to the main branch. However, if you have new commits in the main branch after commiting into dark-theme, there might be conflicts and when you merge those two, you will be prompted to handle them. At the end, merge will end up with a new commit which combined the commits in the branches.


With this command, we could check the changes between two commits, branches, staged and unstaged etc. If we would like to list the changes in working directory that are not staged. This compares the working directory and the staging area.

git diff

To check the changes since last commit, use:

git diff HEAD

To check the staged changes:

git diff --staged
git diff --cached

In order to diff specific files:

git diff <filename>
git diff HEAD <filename>
git diff --staged <filename>

To compare changes between two branches:

git diff branch1..branch2
git diff branch1 branch2

To check changes between two commits:

git diff commit1..commit2


In the case where we have more than one branch, let’s say we have made some changes in one branch. When we switch to another branch without committing, it brings the changes, but if there are conflicts, it is not possible. That’s where we make use of stash. We carry the changes we make into the stash as follows:

git stash

It takes all staged or unstaged uncommitted changes and stash them, reverting the changes in the working copy. If we want to bring the most recently stashed changes in the stash to keep working when getting back to branch:

git stash pop

If we want to let the stash keep itself as it is (or apply ith one), we use the following:

git stash apply
git stash apply stash@{<i>}

The stash is not emptified unlike git stash pop. There we can use the stash in different branches. We can add multiple stashes onto the stack of stashes. We can see the inside of stash:

git stash list

We remove a stash as follows:

git stash drop stash@{<i>}

If we fully clean stash:

git stash clear


This command lets you to travel in the time line of your commits. With git log —online command, we got the commit hash that we want to return and then use the following:

git chechkout <commit_hash>

Then, it becomes that we are in a detached HEAD situation when we check git status. Of course, we did not lost the commits after the commit. HEAD usually points to a branch NOT as commit. If we jump back in the time, then HEAD points to the commit we jumped in which is called a detached HEAD situation. To reference commits relative to HEAD:

git checkout HEAD~1
git checkout HEAD~2

When we make a change in a file and do not want to keep the changes, i.e. we want to revert the file to the last commit where HEAD refers. Then we do:

git checkout HEAD <filename>


To discard changes and revert the file to the last commit:

git restore <filename>

We can also unmodify file to a specified commit:

git restore --source HEAD~1 <filename>
git restore --source <commit_hash>

To unstage the file that has been staged already:

git restore --staged <filename>


To reset the repo to a specific commit via its hash:

git reset <commit_hash>

Be careful, we do not loose the changes but only commit. This command could be useful if we commit in a wrong branch. If we want to loose the changes as well:

git reset --hard <commit_hash>


Reset vs Revert: In reset, we loose the commit after the commit we reset to. But, in revert we keep record of the commit and undo all the changes. It does not delete the commit we want to remove, revert keeps it, and create a new commit by loosing changes.

git revert <commit_hash>

In summary, we all can use page to remember most common commands, what they are used for, how they are used and etc. By the way, you can also check git status command where you can use some hints to use.

Finally, I will make improvement on this post. In the near feature, I am going to add commands related to remote repositories such as push, pull, fetch and etc.


  1. Git Docs, Reference