Skip to content

Instantly share code, notes, and snippets.

@gabrielseco
Last active December 1, 2021 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielseco/bad24a79bde317ebccf8ed5a92ce008c to your computer and use it in GitHub Desktop.
Save gabrielseco/bad24a79bde317ebccf8ed5a92ce008c to your computer and use it in GitHub Desktop.
Git commands

Clean files modified and not staged

git checkout -- .

Create branch from commit

git checkout -b <name-branch> <commit-sha>

See diff commit

git diff <commit-sha>^!

See git log in reverse

git log --reverse

Go to the previous branch

 git checkout -

Resolve file using theirs

 git checkout --theirs <name-of-file>

Clean all external files

 git clean -xdf

Remove old branches && fetch all

git fetch --all --prune && git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d

Create tag and push

 git tag <name-of-the-tag>
 git push --tags

Rename local branch

 git branch -m <name-of-the-branch>

Rename local branch && remote

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Abort merge

 git merge --abort

Revert file to commit

git checkout <commit> -- file1/to/restore file2/to/restore

Logs history file

git log --full-history -- <file>

Remove all branches except master

git branch | grep -v "master" | xargs git branch -D

Merge a commit from another branch

git cherry-pick <commit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment