Git Cheat Sheet

superior_hosting_service

Git

A cheat sheet for uncommon Git commands.

Configuration

CommandDescription
git config --global user.name "foo"Set user name
git config --global user.email "foo@example.com"Set user email

Branches

CommandDescription
git branch fooCreate a new branch
git branch -d fooDeletes a branch
git switch fooSwitch to a branch
git switch -c|--create fooCreate and switch to a branch
git restore foo.jsUndo all changes on the foo.js file
git checkout foo.jsUndo all changes on the foo.js file
git checkout fooUse git switch instead
git checkout -b fooUse git switch -c instead
git merge fooMerge branch into current branch

Pulling

CommandDescription
git pull --rebase --pruneGet latest, rebase any changes not checked in and delete branches that no longer exist

Staged Changes

CommandDescription
git add file.txtStage file
git add -p–patch file.txt`
git mv file1.txt file2.txtMove/rename file
git rm --cached file.txtUnstage file
git rm --force file.txtUnstage and delete file
git reset HEADUnstage changes
git reset --hard HEADUnstage and delete changes
git clean -f|--force -dRecursively remove untracked files from the working tree
git clean -f|--force -d -xRecursively remove untracked and ignored files from the working tree

Changing Commits

CommandDescription
git reset 5720fdfReset current branch but not working area to commit
git reset HEAD~1Reset the current branch but not working area to the previous commit
git reset --hard 5720fdfReset current branch and working area to commit
git commit --amend -m "New message"Change the last commit message
git commit --fixup 5720fdf -m "New message"Merge into the specified commit
git revert 5720fdfRevert a commit
git rebase --interactive [origin/main]Rebase a PR (git pull first)
git rebase --interactive 5720fdfRebase to a particular commit
git rebase --interactive --root 5720fdfRebase to the root commit
git rebase --continueContinue an interactive rebase
git rebase --abortCancel an interactive rebase
git cherry-pick 5720fdfCopy the commit to the current branch

Compare

CommandDescription
git diffSee difference between working area and current branch
git diff HEAD HEAD~2See difference between te current commit and two previous commits
git diff main otherSee difference between two branches

View

CommandDescription
git logSee commit list
git log --patchSee commit list and line changes
git log --decorate --graph --onelineSee commit visualization
git log --grep fooSee commits with foo in the message
git show HEADShow the current commit
git show HEAD^ or git show HEAD~1Show the previous commit
git show HEAD^^ or git show HEAD~2Show the commit going back two commits
git show mainShow the last commit in a branch
git show 5720fdfShow named commit
git blame file.txtSee who changed each line and when

Stash

CommandDescription
git stash push -m "Message"Stash staged files
git stash --include-untrackedStash working area and staged files
git stash --stagedStash staged files
git stash listList stashes
git stash applyMoved last stash to working area
git stash apply 0Moved named stash to working area
git stash clearClear the stash

Tags

CommandDescription
git tagList all tags
git tag -a|--annotate 0.0.1 -m|--message "Message"Create a tag
git tag -d|--delete 0.0.1Delete a tag
git push --tagsPush tags to remote repository

Remote

CommandDescription
git remote -vList remote repositories
git remote show originShow remote repository details
git remote add upstream <url>Add remote upstream repository
git fetch upstreamFetch all remote branches
git rebase upstream/mainRefresh main branch from upstream
git remote -vList remote repositories
git push --tagsPush tags to remote repository

Submodules

CommandDescription
git submodule statusCheck status of all submodules
  • Pull submodules
    1. git submodule sync
    2. git submodule init
    3. git submodule update
  • Change branch
    1. cd /submodule
    2. git fetch origin <branch-name>
    3. git checkout <branch-name>
    4. cd /

The Git is a github repository by Muhammad Rehan Saeed