Git Cheat Sheet: Essential Commands for Version Control
Srijan Karki
Posted on August 13, 2024
Git is a powerful version control system that allows developers to track changes, collaborate on projects, and manage their code efficiently. This cheat sheet provides a quick reference to the most commonly used Git commands, helping you navigate through your version control tasks with ease.
Getting Started
Create a New Repository
To start a new project with Git, you can initialize a new repository:
$ git init [project name]
If you want to clone an existing repository, use:
$ git clone git_url
To clone a repository into a specified directory:
$ git clone git_url my_directory
Making Changes
Check Status
To see which files have been modified and staged for your next commit:
$ git status
Stage Changes
To stage a specific file:
$ git add [file]
To stage all changed files:
$ git add .
Commit Changes
To commit all staged files with a message:
$ git commit -m "commit message"
To commit all tracked files (including those that are not staged) with a message:
$ git commit -am "commit message"
Discard Changes
If you want to discard changes in your working directory that are not staged:
$ git restore [file]
To unstage a staged file:
$ git restore --staged [file]
To unstage a file but keep the changes:
$ git reset [file]
Revert Changes
To revert everything to the last commit:
$ git reset --hard
View Differences
To see the differences between your working directory and the last commit:
$ git diff
To see the differences between staged changes and what is yet to be committed:
$ git diff --staged
Rebase
To apply any commits of the current branch ahead of a specified one:
$ git rebase [branch]
Configuration
Set User Information
To set the name that will be attached to your commits and tags:
$ git config --global user.name "name"
To set an email address for your commits:
$ git config --global user.email "email"
Customize Git Output
To enable colorization of Git output:
$ git config --global color.ui auto
To edit the global configuration file in a text editor:
$ git config --global --edit
Working with Branches
List Branches
To list all local branches:
$ git branch
To list all branches, both local and remote:
$ git branch -av
Switch Branches
To switch to a specific branch:
$ git checkout my_branch
To create and switch to a new branch:
$ git checkout -b new_branch
Delete a Branch
To delete a branch:
$ git branch -d my_branch
Merge Branches
To merge branchA
into branchB
:
$ git checkout branchB
$ git merge branchA
Tagging
To tag the current commit:
$ git tag my_tag
Observing Your Repository
View Commit History
To show the commit history for the currently active branch:
$ git log
To see the commits on branchA
that are not on branchB
:
$ git log branchB..branchA
File-Specific Logs
To show the commits that changed a specific file, even across renames:
$ git log --follow [file]
Compare Branches
To see the differences between branchA
and branchB
:
$ git diff branchB...branchA
Show an Object in Human-Readable Format
To display any object in Git in a human-readable format:
$ git show [SHA]
Synchronize
Fetch Changes
To fetch all branches from a remote repository:
$ git fetch [alias]
Merge Changes
To merge a remote branch into your current branch:
$ git merge [alias]/[branch]
No Fast-Forward
To prevent fast-forwarding during the merge:
$ git merge --no-ff [alias]/[branch]
Only Fast-Forward
To allow only fast-forward merges:
$ git merge --ff-only [alias]/[branch]
Push Changes
To push local branch commits to a remote repository:
$ git push [alias] [branch]
Pull Changes
To fetch and merge any commits from the remote branch you're tracking:
$ git pull
Cherry-Pick Commits
To merge just one specific commit from another branch:
$ git cherry-pick [commit_id]
Working with Remotes
Manage Remotes
To add a remote repository URL as an alias:
$ git remote add [alias] [url]
To show the names of the remote repositories you've set up:
$ git remote
To show the names and URLs of the remote repositories:
$ git remote -v
To remove a remote repository:
$ git remote rm [remote repo name]
Change Remote URL
To change the URL of the remote repository:
$ git remote set-url origin [git_url]
Temporary Commits
Stash Changes
To save modified and staged changes temporarily:
$ git stash
To list stashed changes:
$ git stash list
To apply changes from the top of the stash stack:
$ git stash pop
To discard changes from the top of the stash stack:
$ git stash drop
Tracking Path Changes
Remove Files
To delete a file from the project and stage the removal:
$ git rm [file]
Move Files
To change an existing file path and stage the move:
$ git mv [existing-path] [new-path]
View File History
To show all commit logs with an indication of any paths that moved:
$ git log --stat -M
Ignoring Files
A .gitignore
file specifies intentionally untracked files that Git should ignore. Here are some examples:
/logs/*
!logs/.gitkeep
.DS_store
node_modules
.sass-cache
Git Tricks
Rename Branch
To rename the current branch:
$ git branch -m <new_name>
Push and Reset
To push and set the upstream branch:
$ git push origin -u <new_name>
Delete Remote Branch
To delete a remote branch:
$ git push origin --delete <old>
View Logs
To search for a change by content:
$ git log -S'<a term in the source>'
To show changes over time for a specific file:
$ git log -p <file_name>
To print out a visualization of your log:
$ git log --pretty=oneline --graph --decorate --all
Branch Management
To list all branches and their upstreams:
$ git branch -vv
To quickly switch to the previous branch:
$ git checkout -
To get only remote branches:
$ git branch -r
To checkout a single file from another branch:
$ git checkout <branch> -- <file>
Rewriting History
Rewrite Last Commit Message
To rewrite the last commit message:
$ git commit --amend -m "new message"
Git Aliases
To create shortcuts for commonly used Git commands:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Conclusion
Git is an incredibly versatile tool for version control, offering numerous commands to manage your project's history, collaborate with others, and ensure your code is always in a good state. Keep this cheat sheet handy as a quick reference to streamline your Git workflow.
Posted on August 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.