Git Commands

We have talked about the DevOps Lifecycle and Mapping Tools previously. Under the Code stage of the lifecycle, the source control is the key component. And Git is the first word that jumps out. Remembering all the Git commands can be a daunting task. So we want to use this article to be the repository of common Git commands.

The diagram below demonstrate the Git workflow and the key commands. We’ll cover these key commands first, and then list out the other command options.

Git workflow

Please note that the workflow above is assuming that you’ve already established your local repository. If not, it’s ok. We’ll also include the initiation commands, like git init and git clone, in the Additional Commands section.

Key Commands

CommandsDescription and Exmaples
git addStage changes for next commit
git add .
git add file_name
git add c:\local_repo_name\repo_folder_name/sub_folder_name
git commitCommit the staged snapshot to the local repository
git commit -m "commit message"
git pushUpload local repository content to a remote repository
git push origin master
git push -u origin local_branch_name
git fetchDownload content from remote repository, but doesn’t force the merge
git fetch origin master
git mergeJoin two branches together
git merge local_branch_name
git merge local_branch_1 local_branch_2
git pullCombo of git fetch and git merge
git pull
git pull origin remote_branch_name

For detailed Git reference, see

Additional Commands

To make it easier for searching, we’ve put the commands below in the alphabetical order.

CommandsDescription and Examples
git blameReview a file’s modification history
git blame file_name
git blame -L 1,10 file_name
It’s often used together with git log to find out what, how and why a change was made to a file
git branchmanage branches of a repository, e.g. create, list, rename and delete
git branch new_branch_name
git branch
git branch -a
git branch -m renamed_branch_name
git branch -d existing_branch_name
git checkoutSwitch to another branch in the working directory
git checkout local_branch_name
git checkout -b new_branch_name
git cleanRemove the untracked files
git clean -n
git clean -f
git clean -dn
git clean -df
git cloneClone a remote repository on to a local repository
git clone
git configCustomise the git environment
git config --list --show-origin
git config --global "Richard"
git config
git diffInspect changes in a repository
git diff
git diff file_name
git diff commit_id_1 commit_id_2
git diff branch_name_1 branch_name_2
git initCreate an empty Git repository in a specified directory
git init c:\local_repo_name\repo_folder_name
git logView the commit history of a branch
git log
git log -p -3
git rebaseMove one branch to the tip of another branch
git rebase base_branch_name
*do not rebase any shared branches
git reflogView the HEAD change history of all local branches
git reflog
git reflog --all -3
git remoteManage remote repositories’ information stored locally
git remote -v
git remote repository_name repository_url
git remote rm repository_name
git resetMove both current HEAD pointer and branch ref pointer
git reset --soft commit_id
git reset --mixed commit_id
git reset --hard commit_id
git reset commit_id file_name
git revertUndo changes to a commit history
git revert HEAD
git revert commit_id
git revert -n HEAD
git rmRemove tracked files from the staging area and (but not or) working directory
git rm file_name_1 file_name_2
git rm --cached file_name_1
git rm -r folder_name
git stashSave and hide committed and uncommitted changes
git stash save "stash_message"
git stash -u
git stash -a
git stash -p
git stash list
git stash show
git stash pop
git clean -n -d -x
git statusView the state of working directory and staging area
git status
git status -s
git tagSnapshot specific points in a repository history
git tag
git tag -a tag_name -m "tag message"
git tag -a tag_name commit_id

For detailed Git reference, see

Advanced Tips

Set up git aliasgit config --global "log --oneline --graph --decorate"
git config --global "checkout"
git config --global "branch"
git config --global "commit"
git config --global "status"
Config levels--local apply configuration at the repository level (<repository folder/.git/config>)
--global apply configuration at the user level (C:\Users\<user name>\.gitconfig)
--system apply configuration for all users across an entire machine (C:\ProgramData\Git\config)
Customise git log formatgit log --pretty=format:"%cn committed %h on %cd"
git log --oneline --graph --decorate
Filter git log outputgit log --after="yyyy-mm-dd" --before="yyyy-mm-dd"
git log --author="author1\|author2"
git log --grep="commit_message_key_word"
git log -S"file_content_key_word"
git log branch_name_1 branch_name_2
Some plumbing commandsgit cat-file -p HEAD
git ls-tree -r HEAD
git ls-files -s
Wildcard git rmgit rm folder_name/\*.yml
git rm *.tags
Undo git rmgit reset HEAD
git checkout .


Key WordsExplanation
originorigin stands for the remote repository. When we use “git push -u origin local_branch_name“, it tells the system that we want to push our local branch to the remote repository. Usually there is one default remote repository and origin represents this default repository.
If you don’t like this name, you can rename it by using “git remote rename origin new_name
HEADHEAD stands for the last commit of the active/current branch. Each repository only has one current branch, hence one HEAD as well. If you want to check where the HEAD of a repository is pointing to, run “cat .git/HEAD
Detached HEAD happens when a checkout command is applied to a specific historical commit, tag or remote branch.
mastermaster is a branch, the default branch, the main branch, and it’s always there.
branchbranch is a like a fork in the history of a repository. One branch represents an independent line of development, like a fork teeth.
indexindex is the proposed next commit, also called staging area.

For detailed Git reference, see

For undo scenarios, see

For reset vs checkout, see

You May Also Like

About the Author: Richard Zhao

My name is Richard Zhao. I'm a solution architect and owner of Having built knowledge bases for many companies, I'd like to use this cloud studio to share knowledge and ideas with wider people on the internet.

Leave a Reply

Your email address will not be published. Required fields are marked *