This article contains the most commonly used Git commands. I think this list is enough for daily development.
git clone and checkout
How to clone new repository
When we join to a team there’s probably a repository on which we work. Then, start with these commands. CI server may start to build for the new branch if we create it on a browser. It is better to create a new branch on the local machine.
git clone <url to the git repository>
cd top-directory
git checkout -b "new-branch-name"
How to create new branch
When we already have a cloned repository.
git checkout master
# should update the contents first
git pull
git checkout -b "new-branch-name"
If we want to create a branch based on another branch
git checkout <where-you-want-to-use-as-base-branch>
git pull
git checkout -b "new-branch-name"
git add, commit and reset
How to add a commit
Firstly, we need to add files that we want to commit.
# To add all files
git add -A
# To add a specific file
git add <file name>
We should confirm if only necessary files are added there.
git status
Then, commit and push it to the remote repository.
git commit -m "commit message"
git push
How to add files to last commit
Sometimes I forget to add some files. Then, do amend. It puts newly added files to the last commit. git opens an editor to edit the commit message. Change the commit message if necessary.
git add <additional files>
git commit --amend
git rebase
How to rebase onto a branch or abort it
Since the master branch is updated by other developers while developing, we need to update our own dev branch too. Run this command when we want to rebase onto master.
git pull --rebase origin master
If we want to rebase onto another branch
git pull --rebase origin "based-branch-name"
How to proceed with conflicts
When rebase causes conflicts, we need to resolve the conflicts followed by executing one of the followings.
# after resolving the conflicts
git rebase --continue
# if there's no conflicts
git rebase --skip
How to abort the rebase
We need to abort it if we start rebasing onto an incorrect branch.
git rebase --abort
When pushing a change to the remote repository by mistake (reflog and reset)
I sometimes rebase onto an incorrect branch. Even worse, I push it to the remote repository. In this case, we can run the following commands. reflog shows full Git history. If we rebased before reflog command, it shows lots of commit numbers because rebase command does commit one by one. Choose the correct commit number carefully.
# Find a hash number where you want to go back
git reflog
# Go back to the commit
git reset --hard <hash number where you want to go back>
git diff
How to compare changed files
When we want to show the difference.
git diff <branch-name-to-compare>
Showing diff for specific file
git diff <branch-name-to-compare> -- <file-name>
When we want to see which files are updated
git diff <branch-name-to-compare> --name-only
git stash
How to store modified files
We often need to change the working branch but we don’t want to lose modified files. Then, we can store them by stash but new files are not included by default. Choose one of the commands which you want to desire.
# without new files
git stash
# including new files
git stash -u
# add comment
git stash save -u "able to add comment for the stash"
How to apply the stashed changes
When we want to apply the latest stash change and remove it from the stash list.
git stash pop
If we don’t want to remove it.
git stash apply
If we need to apply one of the specific stashed changes.
# check the stash list first
git stash list
# take the specific one
git stash apply stash@{2}
# take the specific one and remove it from the stash list
git stash pop stash@{2}
git log
When we want to check the commit history with the corresponding messages, author, date, and commit number
git log
I think the author and date are not necessary in most cases. The following command shows it more compactly.
git log --oneline
git reset
How to cancel commits
This command cancels it when we did commit by mistake and don’t want to remove the change.
git reset <last commit number>
If we want to cancel the commit and want to remove all changes.
git reset --hard <last commit number>
How to resolve conflict when pulling for code review
When we do git pull
for code review, it can bring code conflicts. Don’t try to resolve it by yourself. Run this command instead.
git reset --hard <latest commit number of the branch which we want to review>
git branch
How to show branch list that includes a keyword
Git doesn’t provide a feature to search branches that include keywords. However, we can use grep if the terminal supports it to show the list.
git branch -a | grep "keyword"
How to remove a branch
git branch -D "branch-name"
git cherry-pick
This command applies commits from another branch to the destination branch. This is useful for example when we fix a bug and merged it onto a released branch, and then we need to apply the changes to the main/master branch.
How to pick one commit
git cherry-pick <commit number that we want to apply>
How to pick multiple commits
It’s possible to apply multiple commits at once by the following command.
git cherry-pick <start commit number>..<end commit number>
Note that the commit specified first here isn’t applied. We need to specify the commit number that is one before the commit that we actually want.
For example, if we want to apply 4 commits from 9e308f4 to 19e7ef8 in the following case
$ git log --oneline
19e7ef8 (HEAD -> master) refactor // <---- want to apply to here
5e479b4 update typescript
c0097a6 Add array-type
9e308f4 update arg-value // <---- want to apply from here
3e4dfce update readme // <-- specify this as start commit number
git command looks like this
git cherry-pick 3e4dfce..19e7ef8
Cleaning up local branches
Delete branches that no longer exists in remote
When using git for a long time, the number of garbage branches increases in the local repository. This is because the local branches are not deleted automatically when they are merged in the remote repository. Those branches’ name is for example “remote/feature/xxx”. These branches remain in the local repository. git fetch or git pull with prune option removes those branches.
git fetch --prune
git pull --prune
Delete branches that have already merged
The commands above remove “remote/feature/xxx” but not “feature/xxx”. When we want to remove those branches as well, we need another command.
Firstly we need to get the branch list that has already been merged onto the main branch. Let’s get the list following command. Adjust “main” depending on your repository.
$ git branch --merged main
feature/aaa
feature/bbb
feature/ccc
* main
Its result contains “* main” branch. We need to get rid of that from the result by using grep.
$ git branch --merged main | grep -v '* main$'
feature/aaa
feature/bbb
feature/ccc
The last step is passing the result to deleting command.
git branch --merged main | grep -v '* main$' | xargs git branch -d
Adjust the search string specified for grep command.
Comments