Cheatsheet of commands to use when working with git. This is a quick reference for the commands that are most commonly used when working with Git.
A description of what each command does if executed, is provided under each command.
This project has been archived. The latest version of this cheat sheet is available at https://thealmostengineer.com/resources/git-cheatsheet/.
git init
Creates a new repository in the current directory.
git branch
Lists all of the branches that are in the current branch.
git add .
Stages all of the files for commit in the current and subirectories.
git add -u
Stages all of the previously commited files in the current and subdirectories.
git commit -m "MESSAGE"
Commits the changes made to files, removal of files from the repo, and addition of files to the repo.
MESSAGE
should be replaced with a details of the changes made since the last commit.
git clone REPOURL
Makes a copy of the repository from Github to your local computer. This allows for changes to be made
from your local computer. REPOURL
should be replaced with the URL from the remote repository.
git status
Shows the current branch, files that have added but not committed, files that have been removed but not committed, files that have not been staged for committing.
git rm FILENAME
Removes files from the repository. If removing directories, "-r" option needs to be used. Replace
FILENAME
with the name of the actual file.
git diff --cached COMMITHASH
Shows the changes that have been made between a previous commit and the files pending commitment.
If no files have been staged for commit, then this command will not return any output.
COMMITHASH
should be replaced with the unique identifer of a previous commit.
git diff --cached $(git log | head -1 | awk '{print $2}')
Performs the same command above, but automatically gets the latest commit from the git log
command. This command will only work on Unix or Linux based systems.
git diff $(git log | head -1 | awk '{print $2}') $(git log | head -7 | tail -1 | awk '{print $2}')
Shows the changes between the latest commit and the previous commit by pulling the commit hash from
git log
command. This command will only work on Unix or Linux based systems.
git log
Shows the commit history. This include the commit message, date, time, author, and commit ID (or hash).
git clean
Removes files that are not being tracked.
git push origin BRANCH
Pushes files from the origin branch to the git server that hosts the repository. BRANCH
should
be replaced with the name of the branch, although it does not have to be specified. if BRANCH
is
not entered, then the current branch will be pushed.
git pull origin BRANCH
Pulls the latest version of the branch from your git server. BRANCH
should be replaced with the
name of the branch, although it does not have to be specified.
git ls-files --deleted -z | xargs -0 git rm
Deletes the local files that have already been removed from the git repo.
git branch | grep -v master | xargs git branch -D
Deletes all of the local branches except master. Useful for when multiple remote branches have been removed and you need to remove the local corresponding branches.
A commit is a revision, or set of updates, made to one or more files.
Makes a copy of repo that is owned by somebody else and allows you to make changes to the code without making changes to the original repo.
Issues are used to track bugs or enhancements for a project.
Combines changes from two branches into a single branch.
A version of code in the repository. Changes to the code are normally done in another branch and then merged into the master branch of the repository.
To send the code from your local machine to the central repository (server).
To get the latest code from the central repository (server) to your local machine.
To make a copy of the repository from the central repository (server) to the local machine.
Kenny Robinson, @almostengr
https://instagram.com/almostengr