Open C# Playlist
Git cheat sheet

Git cheat sheet

October 26, 2021
Ervis TrupjaBy Ervis Trupja

Per definition, Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Even though git is easy to learn, the problem is that people introduce the powerful features of Git that only a few percentages of developers will ever use. Below, you are going to find the most used git commands grouped by their functionality.

Git Basics

Git commandDescription
git init <directory>Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
git clone <repo>Clone repo located at <repo> onto 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 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 . to stage all changes in the current directory.
git commit -m "<message>"Commit the staged snapshot, but instead of launching a text editor, use it as the commit message.
git statusList which files are staged, unstaged, and untracked.
git logDisplay the entire commit history using the default format. For customization see additional options.
git diffShow unstaged changes between your index and working directory.

Git basic commands table

Git Undo Changes

Git commandDescription
git revert <commit>Create a new commit that undoes all of the changes made in <commit>, then apply it to the current branch.
git reset <file>Remove <file> from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
git clean -nShows which files would be removed from the working directory. Use the -f flag in place of the -n flag to execute the clean.

Git undo changes commands table

Git Branches

Git commandDescription
git branchList all of the branches in your repo. Add an argument to create a new branch with the name.
git checkout -b <branch>Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch.
git merge <branch>Merge <branch> into the current branch.

Git branches command table

Remote Repositories

Git commandDescription
git remote add <name> <url>Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.
git fetch <remote> <branch>Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs.
git pull <remote>Fetch the specified remote's copy of the current branch and immediately merge it into the local copy.
git rebase <base>Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.
git pull --rebase <remote>Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.
git push <remote> <branch>Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn't exist.
git push <remote> --forceForces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you're absolutely sure you know what you're doing.
git push <remote> --allPush all of your local branches to the specified remote.
git push <remote> --tagsTags 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.

Ready to Start Your Journey?

Join thousands of developers getting weekly tips, tutorials, and exclusive offers.