Git Cheat Sheet¶
Git is a version control system tool. Developers follow to create software, there is going to be a lot of code that developers write or integrate into their software. all this code from different developers in the team has to be merged at a centralized place, which can keep track of all the versions of their code, maintain the code and even revert in time if anything breaks.
GIT BASICS
Command | Usage |
---|---|
git init <directory> |
Create empty Git repo in the specified directory. Run with no arguments to initialize the current directory as a git repository. |
git clone <repo> |
Clone repo located at <repo> onto the local machine. The original repo can be located on the local filesystem or on a remote machine via HTTP or SSH. |
git config user.name <name> |
Define the author name to be used for all commits in the current repo. Devs commonly use --global flag to set config options for the current user. |
git add <directory> |
Stage all changes in <directory> for the next commit. Replace <directory> with a <file> to change a specific file. |
git commit -m "<message>" |
Commit the staged snapshot, but instead of launching a text editor, use |
git status | List which files are staged, unstaged, and untracked. |
git log | Display the entire commit history using the default format. |
git diff | Show unstaged changes between your index and working directory. |
UNDOING CHANGES
Command | Usage |
---|---|
git revert <commit> |
Create a new commit that undoes all of the changes made in |
git reset <file> |
Remove |
git clean -n | Shows which files would be removed from the working directory. Use the -f flag in place of the -n flag to execute the clean. |
GIT BRANCHES
Command | Usage |
---|---|
git branch | List all of the branches in your repo. Add a |
git checkout -b <branch> |
Create and check out a new branch named |
git merge <branch> |
Merge |
REMOTE REPOSITORIES
Command | Usage |
---|---|
git remote add <name> <url> |
Create a new connection to a remote repo. After adding a remote,you can use |
git fetch <remote> <branch> |
Fetches a specific |
git pull |
Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. |
git push <remote> <branch> |
Push the branch to |
GIT RESET
Command | Usage |
---|---|
git reset | Reset the staging area to match the most recent commit but leave the working directory unchanged. |
git reset --hard | Reset staging area and working directory to match the most recent commit and overwrites all changes in the working directory. |
git reset <commit> |
Move the current branch tip backward to |
git reset --hard <commit> |
Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after |
GIT PULL
Command | Usage |
---|---|
git pull --rebase <remote> |
Fetch the remote’s copy of the current branch and rebase it into the local copy. Uses git rebase instead of the merge to integrate the branches. |
GIT PUSH
Command | Usage |
---|---|
git push <remote> --force |
Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re sure you know what you’re doing. |
git push <remote> --all |
Push all of your local branches to the specified remote. |
git push <remote> --tags |
Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo. |