Git Cheat Sheet – Git Commands You Should Know
Abdullah
Posted on June 21, 2024
- What is a Distributed Version Control System?
-
Git Commands You Should Know
- How to check your Git configuration
- How to setup your Git username
- How to setup your Git user email
- How to cache your login credentials in Git
- How to initialize a Git repo
- How to add a file to the staging area in Git
- How to add all files in the staging area in Git
- How to add only certain files to the staging area in Git
- How to check a repository’s status in Git
- How to commit changes in the editor in Git
- How to commit changes with a message in Git
- How to commit changes (and skip the staging area) in Git
- How to see your commit history in Git
- How to see your commit history including changes in Git
- How to see a specific commit in Git
- How to see log stats in Git
- How to see changes made before committing them using “diff” in Git
- How to see changes using “git add -p”
- How to remove tracked files from the current working tree in Git
- How to rename files in Git
- How to ignore files in Git
- How to revert unstaged changes in Git
- How to revert staged changes in Git
- How to amend the most recent commit in Git
- How to rollback the last commit in Git
- How to rollback an old commit in Git
- How to create a new branch in Git
- How to switch to a newly created branch in Git
- How to list branches in Git
- How to create a branch in Git and switch to it immediately
- How to delete a branch in Git
- How to merge two branches in Git
- How to show the commit log as a graph in Git
- How to show the commit log as a graph of all branches in Git
- How to abort a conflicting merge in Git
- How to add a remote repository in Git
- How to see remote URLs in Git
- How to get more info about a remote repo in Git
- How to push changes to a remote repo in Git
- How to pull changes from a remote repo in Git
- How to check remote branches that Git is tracking
- How to fetch remote repo changes in Git
- How to check the current commits log of a remote repo in Git
- How to merge a remote repo with your local repo in Git
- How to get the contents of remote branches in Git without automatically merging
- How to push a new branch to a remote repo in Git
- How to remove a remote branch in Git
- How to use Git rebase
- How to run rebase interactively in Git
- How to force a push request in Git
- Conclusion
What is a Distributed Version Control System?
A Distributed Version Control System (DVCS) is a type of version control where the complete codebase—including its full history—is mirrored on every developer's computer. This setup allows for more flexible workflows, including offline work, easier branching, and faster operations compared to centralized version control systems like SVN.
Git Commands You Should Know
Understanding and mastering Git commands is essential for effective version control. Here's a comprehensive guide to help you get started and become proficient with Git.
How to check your Git configuration
Before diving into Git commands, it's crucial to check your Git configuration to ensure everything is set up correctly.
git config --list
How to setup your Git username
Your Git username is used to label your commits with your identity. Set it up using:
git config --global user.name "Your Name"
How to setup your Git user email
Similar to the username, your email address is attached to your commits.
git config --global user.email "you@example.com"
How to cache your login credentials in Git
To avoid entering your credentials repeatedly, you can cache them.
git config --global credential.helper cache
How to initialize a Git repo
Start tracking your project with Git by initializing a repository.
git init
How to add a file to the staging area in Git
Prepare your changes for a commit by adding them to the staging area.
git add <file>
How to add all files in the staging area in Git
Add all your changes to the staging area at once.
git add .
How to add only certain files to the staging area in Git
Sometimes, you only want to stage specific files.
git add <file1> <file2>
How to check a repository’s status in Git
Check the status of your working directory and staging area.
git status
How to commit changes in the editor in Git
Commit your staged changes with an editor for the commit message.
git commit
How to commit changes with a message in Git
Commit your changes directly from the command line with a message.
git commit -m "Your commit message"
How to commit changes (and skip the staging area) in Git
You can commit changes directly without staging them first.
git commit -a -m "Your commit message"
How to see your commit history in Git
Review the history of your commits.
git log
How to see your commit history including changes in Git
View commit history along with the changes introduced.
git log -p
How to see a specific commit in Git
Inspect a particular commit in detail.
git show <commit-hash>
How to see log stats in Git
Get a concise summary of your commit history.
git log --stat
How to see changes made before committing them using “diff” in Git
Compare changes in your working directory.
git diff
How to see changes using “git add -p”
Interactively select changes to stage.
git add -p
How to remove tracked files from the current working tree in Git
Remove files from your working directory and staging area.
git rm <file>
How to rename files in Git
Rename and move files within your repository.
git mv <oldfile> <newfile>
How to ignore files in Git
Use a .gitignore
file to exclude files from tracking.
# .gitignore
node_modules/
*.log
How to revert unstaged changes in Git
Discard changes in your working directory.
git checkout -- <file>
How to revert staged changes in Git
Unstage changes while retaining them in your working directory.
git reset <file>
How to amend the most recent commit in Git
Modify the last commit with new changes.
git commit --amend
How to rollback the last commit in Git
Undo the last commit while keeping changes in the working directory.
git reset --soft HEAD~1
How to rollback an old commit in Git
Revert to a specific commit.
git revert <commit-hash>
How to create a new branch in Git
Branching allows you to work on different features or fixes.
git branch <branch-name>
How to switch to a newly created branch in Git
Move to your new branch.
git checkout <branch-name>
How to list branches in Git
See all branches in your repository.
git branch
How to create a branch in Git and switch to it immediately
Create and switch to a new branch in one command.
git checkout -b <branch-name>
How to delete a branch in Git
Remove branches that you no longer need.
git branch -d <branch-name>
How to merge two branches in Git
Combine changes from different branches.
git merge <branch-name>
How to show the commit log as a graph in Git
Visualize your commit history as a graph.
git log --graph
How to show the commit log as a graph of all branches in Git
See the commit history graph for all branches.
git log --graph --all
How to abort a conflicting merge in Git
If a merge goes wrong, you can abort it.
git merge --abort
How to add a remote repository in Git
Link your local repository to a remote one.
git remote add origin <url>
How to see remote URLs in Git
List the URLs of your remote repositories.
git remote -v
How to get more info about a remote repo in Git
Detailed information about your remotes.
git remote show origin
How to push changes to a remote repo in Git
Upload your local changes to a remote repository.
git push origin <branch-name>
How to pull changes from a remote repo in Git
Fetch and merge changes from a remote repository.
git pull
How to check remote branches that Git is tracking
List the remote branches your local repository is tracking.
git branch -r
How to fetch remote repo changes in Git
Download changes from a remote repository without merging.
git fetch
How to check the current commits log of a remote repo in Git
View the commit log for a remote repository.
git log origin/<branch-name>
How to merge a remote repo with your local repo in Git
Combine changes from a remote repository into your local one.
git merge origin/<branch-name>
How to get the contents of remote branches in Git without automatically merging
Fetch remote branches without merging them.
git fetch origin
How to push a new branch to a remote repo in Git
Share your new branch with the remote repository.
git push origin <branch-name>
How to remove a remote branch in Git
Delete a branch from the remote repository.
git push origin --delete <branch-name>
How to use Git rebase
Rebase your current branch onto another branch.
git rebase <branch-name>
How to run rebase interactively in Git
Use interactive rebase for a more controlled process.
git rebase -i <commit-hash>
How to force a push request in Git
Forcefully push changes to a remote repository.
git push --force
Conclusion
Mastering Git commands empowers you to manage your codebase efficiently, collaborate with others, and maintain a robust history of your project's development. Practice these commands and explore more advanced features as you grow more comfortable with Git.
Posted on June 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.