Git cheat sheet

Marina Haack
Passei Direto Product and Engineering
3 min readJan 26, 2018
Photo by Goran Ivos on Unsplash

Update your branch

git pull origin master

Change the branch

git checkout develop

Create a new branch

git checkout -b yourBranchName

Commit your changes

// adds your changes
git add .
// create the commit with a message
git commit -m "Commit message"
// send the commit to remote branch
git push origin yourBranchName

Commit your changes without untracked files

// add files and create the commit with a message
git commit -am "Commit message"
// send the commit to remote branch
git push origin yourBranchName

Add part of the code to the commit

git add path -p

Save changes without commit without untracked files

git stash

Save changes without commit with untracked files

git stash -u

Get stashed changes

git stash pop

Preview the stashed changes

git stash show -p

Drop the stashed changes

git stash drop

Remove local branch

git branch -D yourBranchName

Undo changes

git checkout .

Merge

// Merge the branch1branch2
git checkout branch2
git merge origin branch1

Change commit message (remote)

// x is the number of commits you want to change
git rebase -i HEAD~x
// change pick for r
// change the message
// -f will force your changed commit
git push origin youBranchName -f

Make the log show the branches

git log --graph

Create a new tag to the repository

git tag yourTag git push origin yourTag

Remove local tag

git tag -d yourTag

Remove remote tag

git push origin :refs/tags/yourTag

Commit to an existing tag

// Create a branch with the tag
git branch yourTag-new-branch yourTag
git checkout yourTag-new-branch
// Commit the change
git add .
git commit -m "Message"
// Delete and recreate the tag locally
git tag -d yourTag
git tag yourTag
// Delete and recreate the tag remotely
git push origin :yourTag
git push origin yourTag

Undo commits and brings back to your staging

/ you can change 1 to the number of commits that you want to revert
git reset --soft HEAD~1

Undo commits and delete the changes

// you can change 1 to the number of commits that you want to revert
git reset --hard HEAD~1

Send commit to another branch (Cherry-pick)

// Make sure you are on the branch you want apply the commit to
git checkout yourBranchName
// use git log to find the commitHash
git cherry-pick commitHash

Local Branch Cleanup

// Delete local branches that have been merged into origin/master
git branch --merged | egrep -v "(^\*|mainline|develop)" | xargs git branch -d

Blank commits

// Create a commit without any changes
// You can use to trigger a build without the need of a change
git commit --allow empty -m "Empty commit"

Bonus:

Create shortcuts for git

// in your terminal
nano ~/.bash_profile
// add this shortcuts
alias gst"git status"
alias gc="git commit"
alias gco="git checkout"
alias gpom="git pull origin mainline"
alias gp="git push"
alias gd="git diff"
alias gb="git branch"
alias gel="git branch -D"
alias gl="git log --graph"
// load shortcuts
source ~/.bash_profile

You can check this article and others on my website: https://www.marinahaack.com/articles/Git-Cheat-Sheet

--

--

Marina Haack
Passei Direto Product and Engineering

I’m a Software Engineer, technology lover that wants to make a better world through it.