Git Cheatsheet


revert last commit

git reset HEAD~

local overrides

overwrite the complete local history of the current branch from its remote

git reset --hard origin/<current-branch>

reset the current-branch to remote HEAD

(equivalent to deleting and recreating the current branch)

git reset --hard origin/main

branch

show all

git branch

create and switch to new

git checkout -b <name>

switch back to the previous local branch you've been working on

git checkout -

rename

git branch -m <oldName> <newName>

delete

git branch -d <name>

add branch to upstream repository

git push --set-upstream origin <name>

stage all changes

git add .

unstage all changes

git reset .

stash

save

git stash

what is stashed?

git stash list

delete

git stash drop

release

git stash pop

rebase

(while on target branch)

git rebase develop

if you want to push to existing repository, force is needed

git push --force

undo all uncommited chages

git checkout .

restore deleted file

git checkout HEAD <file>

redo last commit with new added files and rename it

git commit --amend

soft reset

remove last commit by keeping all changes staged

git reset --soft HEAD~1

remove last "n" commits by keeping all changes staged

(e.g. n = 4)

git reset --soft HEAD~4

delete all untracked files and directories

git clean -di

delete all local branches, if there are deleted origin

git remote prune origin git gc

delete all local tags, if there are deleted origin

git tag -l | xargs git tag -d git fetch --tags

rename tag local and origin

git tag <newName> <oldName> git tag -d <oldName> git push origin :refs/tags/<oldName> git push --tags