Skip to content

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 as the commit message.
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 , then apply it to the current branch.
git reset <file> Remove from the staging area, but leave the working directory unchanged. This unstaged a file without overwriting any changes.
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 argument to create a new branch with the name .
git checkout -b <branch> Create and check out a new branch named . Drop the -b flag to checkout an existing branch.
git merge <branch> Merge into the current branch.

REMOTE REPOSITORIES

Command Usage
git remote add <name> <url> Create a new connection to a remote repo. After adding a remote,you can use as a shortcut for in other commands.
git fetch <remote> <branch> Fetches a specific , from the repo. Leave off to fetch all remote refs.
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 , along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

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 , reset the staging area to match, but leave the working directory alone.
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.