Skip to content

Instantly share code, notes, and snippets.

@JohannesFischer
Last active October 12, 2022 10:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesFischer/490b6f400b44b545758545f79d2b5773 to your computer and use it in GitHub Desktop.
Save JohannesFischer/490b6f400b44b545758545f79d2b5773 to your computer and use it in GitHub Desktop.
List of useful git commands
# Delete remote branch
git push origin :branch-name
# Set upstream
git branch --set-upstream-to=upstream/branchname
# Shorthand for diff of git commit with its parent
git diff COMMIT^!
# or
git diff-tree -p COMMIT
# Compare branch to remote
git diff upstream/BRANCH YOUR_BRANCH --name-only
# Change last commit message (not pushed yet)
git commit --amend
# Fetch without tags
git fetch upstream -n
# Alter previous commit
git reset --soft HEAD^
# reset the unwanted files in order to leave them out from the commit
git reset HEAD path/to/unwanted_file
# commit again, you can re-use the same commit message
git commit -c ORIG_HEAD
# Clean rebase
# on feature branch
git reset --soft HEAD~1
git checkout master
git pull
git checkout -
git rebase master
# rebase / resolve ... make alterations
git add . && git commit -c ORIG_HEAD
# Create Tag
git tag -a v1.0 -m "My version 1.0"
git push --tags
# Delete Remote Tag
git push --delete origin tagname
# Rename Tag
git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags
# Restore deleted file
git rev-list -n 1 HEAD -- <file_path>
git checkout <deleting_commit>^ -- <file_path>
# List of changes files only
git diff --name-only --diff-filter=M
# List files with merge conflicts
git diff --name-only --diff-filter=U
# Switch to previous branch
git switch -
# Track all remote branches
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
# Prune tracking branches not on remote
git remote prune origin
# Fancy log
git log --graph --oneline --all --decorate
# Rename branch
# on branch
git branch –m new-name
# on other branch
git branch –m old-name new-name
# Create a new branch
git switch -c <new_branch_name>
# Git Stash
# stash with message
git stash save “Your stash message”.
# apply stash with specific hash
git stash apply stash@{1}
git stash pop stash@{1}
# show stash diff
git stash show // latest stash
git stash show stash@{1}
# reset file from another branch
git checkout branchname -- filename.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment