Git Cli Cheat Sheet¶
Git is a free and open source distributed version control system designed to quickly and efficiently manage everything from small to very large projects.
A new repository can either be created locally, or an existing repository can be cloned. When a repository was initialized locally, you have to push it to GitHub afterwards.
The git init command turns an existing directory into a new Git repository inside the folder you are running this command. After using the
git init command, link the local repository to an empty GitHub repository using the following command:
Specifies the remote repository for your local repository. The url points to a repository on GitHub.
git remote add origin [url]
Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits
git clone [url]
Configure user information for all local repositories
Sets the name you want attached to your commit transactions
git config --global user.name "[name]"
Sets the email you want attached to your commit transactions
git config --global user.email "[email address]"
Enables helpful colorization of command line output
git config --global color.ui auto
Synchronize your local repository with the remote repository on GitHub.com
Downloads all history from the remote tracking branches
Combines remote tracking branches into current local branch
Uploads all local branch commits to GitHub
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub.
git pull is a combination of
git fetch and
Erase mistakes and craft replacement history
Undoes all commits after [commit], preserving changes locally
git reset [commit]
If you don't want to reset absolutely, but relatively that is also possible using
git reset HEAD~2
Discards all history and changes back to the specified commit
git reset --hard [commit]
Branches are an important part of working with Git. Any commits you make will be made on the branch you’re currently “checked out” to. Use git status to see which branch that is.
Creates a new branch
git branch [branch-name]
Switches to the specified branch and updates the working directory
git switch -c [branch-name]
git checkout -b [branch-name]
Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.
git merge [branch]
Deletes the specified branch
git branch -d [branch-name]
Browse and inspect the evolution of project files
Lists version history for the current branch
Lists version history for a file, beyond renames (works only for a single file)
git log --follow [file]
Shows content differences between two branches
git diff [first-branch]...[second-branch]
Outputs metadata and content changes of the specified commit
git show [commit]
Snapshots the file in preparation for versioning
git add [file]
Records file snapshots permanently in version history
git commit -m "[descriptive message]"
The .gitignore file¶
Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore. You can find helpful templates for
.gitignore files at github.com/github/gitignore. If there are certain files (like
.ide) that should be discluded from all projects, you can create a global
.gitignore file to do so.
Untrack Files Already Added to git Repository Based on .gitignore¶
Commit all your changes. Before proceeding, make sure all your changes are committed, including your .gitignore file. Remove everything from the repository. To clear your repo, use:
git rm -r --cached .
Re add everything.
git add .
git commit -m ".gitignore fix"
Use Gist as Repository¶
It's probably easiest if you just start by cloning the gist, so that
origin (a "remote" that refers to the original repository) is set up for you. Then you can just do
git push origin master. For example:
git clone firstname.lastname@example.org:869085.git mygist cd mygist
Add you changes to the repository.
git add . git commit -m "Better comments" git push origin master
However, if you don't want to redo your changes, you can do:
cd mygist git remote add origin email@example.com:869085.git git fetch origin # Push your changes, also setting the upstream for master: git push -u origin master
Strictly speaking, the
git fetch origin and
-u argument to
git push origin master are optional, but they will helpfully associate the upstream branch
origin with your local branch