Skip to content

Instantly share code, notes, and snippets.

@karsov
Last active April 4, 2024 07:12
Show Gist options
  • Save karsov/56604aebdd2528645f0de1e8edd649d7 to your computer and use it in GitHub Desktop.
Save karsov/56604aebdd2528645f0de1e8edd649d7 to your computer and use it in GitHub Desktop.

Table of Contents

  1. Stash
  2. Log
  3. Tags
  4. Diff
  5. Branches
  6. Misc

Stash

List stash contents

git stash list

Clear all stashes

git stash clear

Pop out of stash only specific files

git checkout stash@{0} -- <filename1> <filename2>

Show changes in a stash

git stash show stash@{6}  # show changed files only
git show stash@{6}

Delete specific stash

git stash drop stash@{6}

Apply stash without deleting it

git stash apply stash@{0}

Log

Log graph

git log --graph --oneline

Return limited number of commits from log

git log -n 2

Show changes in git log

git log -p

Show only files changed in git log

git log --oneline -n 10 --name-only

Tags

Get latest tag matching pattern

git tag --list --sort=-version:refname "my-app-1.5.*" | head -n 1

Get tag of a commit

git describe --tags commit-hash

Git tag create and push

git tag tag-name
git push origin tag-name

Show tag details

git show --name-only tag-name

Overwrite tag

git tag -f tag-name commit-hash
git push -f origin tag-name

Diff

Diff current folder with its state in the main branch

git diff main  -- .

Diff file between different branches

git diff branch1 branch2  -- path/to/file.ext

Find differences between two folders in two commits (usually after a folder rename)

git diff commit-hash1 commit-hash2 -- folder-in-commit-1 folder-in-commit-2

Branches

List local branches sorted by last change date

git branch --sort=committerdate

List local branches with a merged remote

git branch --no-contains main --merged main

Delete local branches with a merged remote

git branch --no-contains main --merged main | xargs git branch -d

Push local branch to remote with different name

git push -f origin my_branch:remote_branch

Reset local branch to remote

git fetch && git reset --hard origin/<branch_name>

Local branch alias

git symbolic-ref refs/heads/branch-alias refs/heads/original-branch-name  # Create or update alias
git symbolic-ref --delete refs/heads/branch-alias  # Delete alias

Misc

Commit parts of a file

git add --patch <filename-is-optional>

# And then there is a prompt “Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?”
#  y - yes, n - no, s - split into smaller hunks 

Commit a fixup for the latest commit

git commit --fixup=HEAD
git rebase -i main --autosquash # rebase automatically marks the previous commit as fixup

Empty commit

git commit --allow-empty -m "Trigger Build"

Undo latest commit

git reset HEAD~

Cherry pick all commits from a branch

git cherry-pick StartCommit^..EndCommit   # “^” is needed to include the StartCommit too

Abort git rebase in Vim

:cq

Reset auhtor and commit date

git commit --amend --reset-author --no-edit

Reset auhtor and commit date for multiple commits in a branch

git rebase -i <commit-hash> -x "git commit --amend --reset-author --no-edit"

Move latest commits from main to branch

git checkout -b newbranch
git checkout main
git fetch && git reset --hard origin/main
git checkout newbranch

Remove file/folder from repo without deleting locally

# For files
git rm --cached file1 file2

# For folders
git rm --cached -r folder1 folder2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment