Skip to content

Instantly share code, notes, and snippets.

@JohannesFischer
Last active June 20, 2024 08:43
Show Gist options
  • 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

GIT cheatsheet

Basics

Set upstream

git branch --set-upstream-to=upstream/branchname

Fetch without tags

git fetch upstream -n

Fancy log

git log --graph --oneline --all --decorate

Branches

Switch to previous branch

git switch -

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>

Delete remote branch

git push origin :branch-name

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

Diff

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

List of changes files only

git diff --name-only --diff-filter=M

List files with merge conflicts

git diff --name-only --diff-filter=U

Alter commits

Change last commit message (not pushed yet)

git commit --amend

Reset previous commit

git reset --soft HEAD^

Reset 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

Add file to previous commit

git add the_left_out_file git commit --amend --no-edit

Rebase

clean rebase

git reset --soft HEAD~1 git checkout main git pull git checkout - git rebase main # rebase / resolve ... make alterations git add . && git commit -c ORIG_HEAD

Tags

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

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

Other

Restore deleted file

git rev-list -n 1 HEAD -- <file_path> git checkout <deleting_commit>^ -- <file_path>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment