Skip to main content
查利博客

Common Git Commands

Git is by far the most popular version control system today and every programmer should learn Git because it is virtually an industry standard. In this article, I have summarized some useful git commands along with a short explanation of each example.

git-status #

This command shows the state of the working directory and the index.

$ git status
$ git status -u
$ git status -uno
  1. View the changes of current branch
  2. Show all untracked files and directories
  3. Show no untracked files

git-checkout #

This command switches branches or restores working tree files.

$ git checkout master
$ git checkout -b newbranch
$ git checkout 5c3540f8c README.md
  1. Switch to master branch
  2. Create a new branch named newbranch and switch to newbranch branch
  3. Take README.md file out of another commit

git-add #

This command adds a change in the working directory to the index.

$ git add README.md
$ git add -A
$ git add .
$ git add -u
$ git add images/*
  1. Stage README.md file
  2. Stage all changes
  3. Stage new files and modifications but not deletions
  4. Stage new modifications and deletions but not new files
  5. Stage all files in images directory by wildcard

git-commit #

This command saves changes to the repository.

$ git commit -m "initial commit"
$ git commit --amend -m "new commit message"
  1. Commit changes with messages
  2. Change commit messages

git-log #

This command shows the commit history.

$ git log --pretty=format:"%h - %an, %ar : %s" HEAD^..HEAD
$ git log --pretty=format:"%h - %an, %ar : %s" --since="6am"
$ git log -n1 --format="%h"
  1. View the last commit
  2. View all of today's commits
  3. View the short hash of the last commit

git-restore #

This command helps to unstage or discard uncommitted changes.

$ git restore --staged README.md
$ git restore README.md
  1. Unstage README.md file
  2. Discard uncommitted changes to README.md file

git-reset #

This command helps to undo local changes to the state of a Git repo.

$ git reset README.md
$ git reset
$ git reset --hard e1a943e70
$ git reset HEAD~
  1. Unstage README.md file
  2. Unstage all files
  3. Rollback to specific commit
  4. Undo the most recent commit

git-rm #

This command removes files from the working directory and the index.

$ git rm -r logs/*
  1. Remove all files in logs directory

git-diff #

This command compares the changes between commits, commit and working tree, etc.

$ git diff master..feature1
$ git diff-tree --no-commit-id --name-only -r master..feature1
$ git diff-tree --no-commit-id --name-only -r 5c3540f8c
  1. View the changes that branch feature1 contains that do not exist in branch master
  2. View the names of changes files of the above result
  3. View the changes of a commit

git-push #

This command uploads local repository content to a remote repository.

$ git push -u origin master
$ git push -u -f origin master
  1. Upload local master branch to origin
  2. Force upload local master branch to origin

git-rebase #

This command helps to rewrite commits from one branch onto another branch.

$ git rebase master
$ git rebase --continue
$ git rebase --abort
$ git rebase -i HEAD~2
  1. Replay current branch onto the master branch
  2. Resume rebase process
  3. Cancel rebase process
  4. Rebase with interactive tool of last 2 commits (reword, edit, squash, etc.)

git-merge #

This command integrates changes from another branch.

$ git merge master
$ git merge origin/master
$ git merge abort
  1. Integrate changes from local branch master
  2. Integrate changes from remote branch master
  3. Cancel merge process

git-cherry-pick #

This command picks a commit and appends it to the current working HEAD.

$ git cherry-pick 5c3540f8c
  1. Copy the changes from a commit to current branch

git-init #

This command creates an empty Git repository or reinitialize an existing on.

$ git init
  1. Create an empty or reinitialize an existing git repo

git-remote #

This command manages set of tracked repositories.

$ git remote -v
$ git remote remove origin
  1. Show URLs of remote repositories when listing your current remote connections
  2. Disconnect the remote origin from the local repository

git-branch #

This command manipulates git branches.

$ git branch -m newbranch
  1. Modify the branch name to newbranch