Register to Better Coder! It's free.
Enjoy the premium features and succeed at every job interview.
Git Cheat Sheet For Beginners
Memorizing all Git commands is not easy. Use this cheat sheet to quickly find the right command for the job.
New project setup
git init https://repositoryurl
Creates a new empty repository
git init https://repositoryurl mydir
Creates a new empty repository in a directory mydir
Copies the remote repository from the remote URL to your local computer and stores it there as a local repository
Reviewing changes and history
Shows all changes in your workspace. New untracked files, changed files, staged files. And also current branch.
Shows a history of all commits
git log -3
Shows last 3 commits
git blame my-file.txt
Shows history of all changes in a file including the author of each change
Basic day-to-day use
Retrieves changes from remote repository to local repository and integrates the changes to your workspace
git add file.htm
Adds changed file.htm to the index (stage)
git add .
Adds all changes to the index (stage)
git add */*.html
Adds all changed files with extension .html to the index (stage)
git rm --cached */*.tmp
Removes all changed files with extension .tmp from index (stage). This can be used when you staged some files by mistake.
git commit -m "Added header"
Commits the changes from the index (stage) to the repository and adds a short description describing the change
git commit -a -m "Added header"
The same as git add . with git commit . Adds all current changes to the index and commits the changes from the index (stage) to the repository and adds a short description describing the change
Pushes changes from local repository to remote repository
Branches give you freedom
Gets a list of all local branches
git branch -r
Gets a list of all remote branches
git branch -a
Gets a list of all local and remote-tracking branches
git branch abc
Creates a new branch with name abc
git checkout abc
Changes the current branch to branch abc. The version of files from branch abc will be checked out to the workspace.
git checkout -b abc
Creates a new branch abc and changes the current branch to this newly created branch. The version of files from branch abc will be checked out to the workspace.
git push -u origin newbranch
Creates a new remote branch newbranch in origin repository and pushes changes from current local branch to the newly created remote branch.
git reset --hard HEAD
Discards all local changes in your workspace
When you need to multi-task
Takes current changes and stashes them away so that there won't be any changes in your workspace. The stashed changes are saved and can be later restored.
git stash pop
Restores previously stashed changes to your workspace and removes the stash, so that the stash will be no longer available in the list of stashes.
git stash list
Displays a list of all existing stashes (previously stashed changes).
git stash drop 1
Deletes the stash with index 1.