Skip to main content
  1. 🔰Posts/
  2. 🗂️Cheat Sheets/
  3. DevOps Cheatsheets/

Git Cheatsheet

·989 words·5 mins
Read My VSCode Setup post. Example use of Better Comments extension may help you creating more human-friendly comments in your code.

Basic Commands #

CommandDescription
git statusCheck status
git add [file]Add file to staging area
git add .Add all files to staging area
git add -AAdd all files to staging area
git add -uAdd all modified files to staging area
git add -pAdd all modified files to staging area interactively
git commit -m "message"Commit changes
git commit -aCommit all changes
git commit -am "message"Commit all changes with message
git commit --amendAmend last commit
git commit --amend -m "message"Amend last commit with message
git commit --amend --no-editAmend last commit without changing the commit message
git commit --amend --reset-authorAmend last commit with new author
git commit --amend --no-edit --reset-authorAmend last commit with new author and without changing the commit message
git commit --amend --no-edit --date="date -R"Amend last commit with new date
git commit --amend --no-edit --date="date -R" --reset-authorAmend last commit with new date and new author
git commit --amend --no-edit --date="date -R" --reset-author --allow-emptyAmend last commit with new date and new author and allow empty commit

Branching & Merging #

CommandDescription
git branchList all branches
git branch -aList all branches (local and remote)
git branch -rList all remote branches
git branch -vList all branches with last commit on each branch
git branch -vvList all branches with last commit and commit author on each branch
git branch -vvvList all branches with last commit, commit author and commit message on each branch
git checkout -b [branch]Create a new branch and switch to it
git merge [branch]Merge a branch into the active branch
git merge [source branch] [target branch]Merge a branch into a target branch
git branch -d [branch]Delete a branch
git branch -D [branch]Force delete a branch
git push origin --delete [branch]Delete a remote branch
git branch -m [old branch] [new branch]Rename a branch
git branch --set-upstream-to=origin/[branch] [branch]Set a local branch’s upstream branch
git branch --unset-upstream [branch]Unset a local branch’s upstream branch
git push origin [branch]Push a branch to your remote repository

Sharing & Updating Projects #

CommandDescription
git push origin [branch]Push a branch to your remote repository
git push -u origin [branch]Push changes to remote repository (and remember the branch)
git pushPush changes to remote repository (remembered branch)
git push origin --delete [branch]Delete a remote branch
git push origin :[branch]Delete a remote branch
git push origin [branch] --forceForce push changes to remote repository

Inspection & Comparison #

CommandDescription
git logView changes
git log --summaryView changes (detailed)
git log --onelineView changes (brief)
git log --statView changes (detailed)
git log --patchView changes (detailed with actual changes)
git log --graphView changes (graphical)
git log --graph --onelineView changes (graphical and brief)
git log --graph --oneline --allView changes (graphical, brief and all branches)

Undoing Things ✨ #

CommandDescription
git reset [file]Unstage a file while retaining the changes in working directory
git reset --hardDiscard all local changes in your working directory
git reset --hard HEADDiscard all local changes in your working directory
git reset --hard origin/[branch]Discard all local changes in your working directory and get the latest version from the remote repository
git reset --hard [commit]Discard all local changes in your working directory and get the specific commit from the remote repository
git checkout -- [file]Discard local changes in a specific file
git checkout [branch]Switch to a branch and discard local changes
git revert [commit]Revert a commit
git revert [commit] --no-commitRevert a commit without committing

Syncing Forks #

CommandDescription
git remote -vList all currently configured remote repositories
git remote add upstream [https://url]Specify a new remote upstream repository that will be synced with the fork
git fetch upstreamFetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master
git merge upstream/masterMerge the changes from upstream/master into your local master branch. This brings your fork’s master branch into sync with the upstream repository, without losing your local changes

Rewrite History #

CommandDescription
git rebase -i HEAD~[number]Interactive rebase
git rebase -i [commit]Interactive rebase
git rebase -i [branch]Interactive rebase
git rebase -i [SHA1]Interactive rebase
git rebase -i [tag]Interactive rebase

Stashing #

CommandDescription
git stashStash changes in a dirty working directory away
git stash save "message"Stash changes in a dirty working directory away with a message
git stash listList all stashed changesets
git stash showShow the changes in the last stashed changeset
git stash show -pShow the changes in the last stashed changeset (detailed)

Tagging #

CommandDescription
git tagList all tags
git tag -l "v1.8.5*"List all tags matching a pattern
git tag [tag]Annotate a tag
git tag -a [tag] -m "[message]"Annotate a tag with a message

Setting up Git #

Git Configuration #

CommandDescription
git configCheck all configuration options
git config --listCheck all configuration options with name and email
git clone [https://url]Clone source code from a remote repository
git config --global user.name "Your name"Configure username
git config --global user.email "Your email"Configure email
git config --global core.editor vimConfigure editor

Getting & Creating Projects #

CommandDescription
git initInitialize a local Git repository
git clone [https://url]Clone source code from a remote repository
git clone [https://url] [folder]Clone source code from a remote repository into a specific folder
git clone --bare [https://url]Clone source code from a remote repository without a working directory
git clone --mirror [https://url]Clone source code from a remote repository without a working directory and without the remote repository

» Sources « #

» References « #

Better Comments Options

RobK
Author
RobK
DevOps | Agile | AWS | Ansible | Terraform | PowerShell | Windows | Linux | Git