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
- View the changes of current branch
- Show all untracked files and directories
- 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
- Switch to master branch
- Create a new branch named newbranch and switch to
newbranch
branch - 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/*
- Stage README.md file
- Stage all changes
- Stage new files and modifications but not
deletions
- Stage new modifications and deletions but not
new files
- 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"
- Commit changes with messages
- 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"
- View the last commit
- View all of today's commits
- 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
- Unstage README.md file
- 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~
- Unstage README.md file
- Unstage all files
- Rollback to specific commit
- Undo the most recent commit
git-rm #
This command removes files from the working directory and the index.
$ git rm -r logs/*
- 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
- View the changes that branch
feature1
contains that do not exist in branchmaster
- View the names of changes files of the above result
- 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
- Upload local master branch to origin
- 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
- Replay current branch onto the master branch
- Resume rebase process
- Cancel rebase process
- 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
- Integrate changes from local branch master
- Integrate changes from remote branch master
- Cancel merge process
git-cherry-pick #
This command picks a commit and appends it to the current working HEAD.
$ git cherry-pick 5c3540f8c
- 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
- 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
- Show URLs of remote repositories when listing your current remote connections
- Disconnect the remote origin from the local repository
git-branch #
This command manipulates git branches.
$ git branch -m newbranch
- Modify the branch name to newbranch
- Next: Common Linux Commands