Skip to main content

Section A.1 Git Cheat Sheet

Subsection A.1.1 Getting started with a project

$ git config --global user.name "John Doe" and $ git config --global user.email [email protected]
Set your username and email address for Git commits. Note you need to run these only once if you pass the --global option, because Git will always use that information on that system. If you want to later override either of these with a different name or email address for a specific project, you can run the command without the --global option while in that project.
git init <directory-name>
Create an empty git repo in a new directory named directory-name. Take care with the directory-name; it is typical to use a short, descriptive, name using only lowercase letters and the "-" character. Note that if you leave off the directory name then the current directory will be initialized as a git repository. For more detail see Git Guides: Git Init
 1 
github.com/git-guides/git-init
git clone <repository-url>
Clone a repository from a repository in the cloud to your local system. Note that if you are planning to contribute to a project that is not yours, you will likely want to fork the repository into your own account first. For example, to contribute to a project hosted on GitHub, go to that repository and fork it into your own GitHub account. For more detail see Git Guides: Git Clone
 2 
github.com/git-guides/git-clone
git help <command>
Get help for a specific git command. For example, git help clone will give you help for the git clone command.

Subsection A.1.2 Current state

git diff
List the changes to files that have not yet been added (staged).
git status
Check the status to see if there are files that have been changed but not added (staged). This command will also list all staged changes. For more detail see Git Guides: Git Status
 3 
github.com/git-guides/git-status
git log
List recent commits.
git pull
Download content from the remote repository and integrate these changes into the local repository. This command should be used regularly because without it your local branch won’t have any of the updates that are present on the remote. For more detail see Git Guides: Git Pull
 4 
github.com/git-guides/git-pull
git add <filename>
Add all current changes in <filename>to the those staged for the next commit. For more detail see Git Guides: Git Add
 5 
github.com/git-guides/git-add
git commit -m "<commit-message>"
Commit all staged files to the local repository. For more detail see Git Guides: Git Commit
 6 
github.com/git-guides/git-commit
git commit --amend
modify the most recent commit by either updating its content or changing its commit message.
git push
Push the current branch and all commits to the remote repository. Note that git push only updates the corresponding branch on the remote.
git push --set-upstream origin <branchname >
Here,>origin refers to the remote git repository from which the repo was cloned (note that this might be a fork). This command will set upstream of the the local branch of the remote respository on Github.
git push -u origin <branchname>
Here -u stands for set upstream. It is used to push a new branch named <branchname> by creating an upstream tracking branch that is related to your current local branch. For more detail see Git Guides: Git Push
 7 
github.com/git-guides/git-push
.

Subsection A.1.3 Git branching

git branch
List all existing branches.
git checkout <branch-name>
Switch to an existing branch called <branch-name>.
git checkout -b <branch-name>
Create a new branch called <branch-name> and switch to that branch. Without the -b flag will allow you to checkout an existing branch.
git merge <branch-name>
Merge <branch-name> into the current branch.

Subsection A.1.4 Git stash

git stash
Oops, you forgot to update your branch and now the branch is dirty and can’t be merged. The git stash command saves the local modifications away and reverts the working directory to match the HEAD commit.
git stash apply
The apply option tries to apply the most recently stashed work to the current branch. It remains on your stash stack.
git stash drop
The git stash drop command deletes the most recent stashed work.

Subsection A.1.5 Git Restoring/Reverting

git restore
Provide a safer way to revert changes in the working tree or staging area. It’s designed specifically for restoring files without switching branches or altering commit history.
git reset
Allows users to reset the current HEAD to a specified state, often used to unstage changes or modify the commit history.
git revert
Create a new commit that undoes changes from a specified commit. This can be seen as a way to "restore" the state of the repository to a prior condition, but through a new commit.
git clean
Remove untracked files from the working tree. It’s similar in spirit to git restore in that it helps you bring your working directory to a clean state, but it’s focused on untracked files.
You have attempted of activities on this page.