Git Cheat Sheet

Git Cheat Sheet

Basics

CommandDescription
git –helpList help commands
git –versionGet installed Git version
git update-git-for-windowsUpdate the installed version of Git

Configuration

CommandDescription
git config -lLists information about your git configuration
git config –global user.name “[firstname lastname]”Set the name that will be attached to your commits and tags
git config –global user.email “[email address]”Set the e-mail address that will be attached to your commits and tags
git config –global color.ui autoSet automatic command line coloring for Git for easy reviewing

Setup a project and clone to local

CommandDescription
git initInitialize a new local repository in the current directory
git init “[project name]”Initialize a new local repository in a new directory with the project name
git clone “[project url]”Downloads a remote repository with all the history into the current directory.

Add changes

CommandDescription
git add [file]Add a file to staging
git add .Adds all files to staging
git add -pWalks through all unstaged changes and prompts if you want to stage or not
git add fil*Adds files starting with fil to staging

Show differences

CommandDescription
git diffDisplay unstaged changes
git diff –stagedDisplay staged changes

Commit changes

CommandDescription
git statusList which files are staged, unstaged, and untracked
git commitCommits files to staging but opens a text editor for the comment
git commit -m “[comment”]Commits all staged files with a comment
git commit -a -m “[comment]”Add and commit in one go (skips staging) with a comment

Authentication

CommandDescription
ssh -vT git@github.comTest your SSH keys

Work with remote repositories

CommandDescription
git add remote [repository URL]Adds a remote repository to your local repository
git remote -vList all remote repositories
git remote show originDisplay more details about the remote repository
git pushUpload all staged changes to the remote repository
git pull[Download all changes from the remote repository
git remote set-url origin git@github.com:yourusername/yourrepository.gitswitch to using the SSH URL

Remove changes

CommandDescription
git checkout [filename]Revert an unstaged file
git reset HEAD [filename]Revert a staged file
git reset HEAD -pWalks through all staged files and asked if you want to revert

Revert a commit

CommandDescription
git revert HEADCreate a new commit that is the opposite of everything in teh given commit
git revert [commit id]Revert to an old commit using its commit iD

Delete files

CommandDescription
git rm [filename]Deletes a file from the current working tree

Change existing file

CommandDescription
git mv [old file] [new file]Renames the file

Branches

CommandDescription
git branchList all of the branches in your repo
git branch [new_branch]Create a new branch
git checkout [different_branch]Use a new or different branch
git checkout -b [new_branch]Create a new branch and use it immediately
git branch -d [branch]Delete a branch
git merge [branch]Merge the history of the current branch with the given branch

Logs

CommandDescription
git logDisplay all logs
git log [-n]Display all logs limited by [-n]
git log -pDisplays all logs and files and changes
git log –statDisplays all logs and statistics

References

50 Commands you should know

Last modified July 21, 2024: update (e2ae86c)