A Git and Github Cheatsheet for developers || Git CLI Cheatsheet for beginners to advance
GIT is the most widely used distributed open-source Version Control System that allows you to track and manage changes made to the files locally on your computer.
However, the tool is so powerful and extensive enough to get lost in all the possible commands it has.
Hence, based on my own experience, here’s a compilation of the GIT cheat sheet
which is the most important and commonly used GIT commands for easy reference.
Here, you can download GIT for all platforms.
GIT CHEAT SHEET 📋
From existing data,
git initcreates a new repository in the current directory
git add .add all the latest changes to the next commit
cd ~/projects/myprojectgit initgit add .
From existing repo
git cloneis used to clone a repository from a remote server
git clone ~/existing/repo ~new/repogit clone you@host:dir/project.git (default protocol is ssh)
A single branch from an existing repo
git clone --single-branch -b <BranchName> <RepoLink>.
Remote repository for existing local data
mkdir repo.git && cd repo.gitgit init --bare[--shared=group]
Fetch the latest changes from the origin
git fetch (this does not merge them)
Pull latest changes from origin
git pull (does a fetch followed by a merge)
Apply a patch that someone sent you
git am -3 patch.mbox (In case of conflict, resolve the conflict and)git am --resolve
Commit all local changes
git commit -a
Commit previously staged changes
git commit -m "descriptive message"
Prepare a patch for other developers
git format-patch origin
Push changes to the origin
git push [origin][branch]
Make a version or a milestone
git tag <version_name>
Switch to the BRANCH branch
git checkout <BRANCH>
Merge branch B1 into branch B2
git checkout <B2>git merge <B1>
Create a branch based on HEAD
git branch <BRANCH>
Create a branch based on another
git checkout <new><base>
Delete a branch
git branch -d <branch>
Return to the last committed state
git checkout -f | git reset --hard (you cannot undo a hard reset)
Revert the last commit
git revert HEAD (Creates a new commit)
Revert specific commit
git revert $id (Creates a new commit)
Fix the last commit
git commit -a --amend (after editing the broken files)
Check out the ID version of a file
git checkout <ID><file>
Remove the previous origin
git remote remove originrm -r -fo .git
Files changed in the working directory
Changes to tracked files
Changes between ID1 and ID2
git diff <ID1><ID2>
History of changes
History of changes with files changed
Who changed what and when in a file
git blame <file>
A commit identifies by ID
git show <ID>
A specific file from a specific ID
git diff <ID>:<file>
All local branches
git branch (star "*" marks the current branch)
Search for patterns
- master is the default development branch
- origin is the default upstream repository
- HEAD is the current branch
That’s it from me today. I hope this cheat sheet helps you with some of the problems you may encounter along the way.
Certainly, it does not cover all the things, but it’s a good article, to begin with.
Thanks for reading and let me know about your favorite Git commands in response to this article and share it with your friends and colleagues.