Skip to content

Instantly share code, notes, and snippets.

@barijaona
Last active July 19, 2025 19:25
Show Gist options
  • Save barijaona/32c590ae180e197bd439a8a5b48c103b to your computer and use it in GitHub Desktop.
Save barijaona/32c590ae180e197bd439a8a5b48c103b to your computer and use it in GitHub Desktop.
Git power tricks

Git power tricks

These commands may help when dealing with old and complicated Git repositories. I used them to build a new branch to streamline a more readable history for a 20-year-old project.

Compare two directories at different times of history

git difftool -d <commit-a>:path/to/dir1 <commit-b>:path/to/dir2

Have a quick view of differences

git diff --stat <commit-a> <commit-b>

Compare two directories, with at least one of them not being Git-managed

git diff --no-index <path1> <path2>

Find initial commit of current head

git cherry master -v | head -n 1

Get the date of a commit…

Committer date

git show -s --format="%cd" <commit>

or

git show -s --format="%ci" <commit>

Author date

git show -s --format="%ad" <commit>

or

git show -s --format="%ai" <commit>

Modify last commit (dates and author)

GIT_COMMITTER_DATE="<committer-date>" git commit --amend --author="Author Name <[email protected]>" --date "<author-date>"

Merge branches when Git is unable to find a common ancestor

git merge --allow-unrelated-histories <other-branch>

Rebase keeping initial author date

git rebase --committer-date-is-author-date --onto <newbase>

Rebuild current head after rebase or cherry picks, resetting author names and dates

Replace <startcommit> with SHA or short SHA of the commit which should be considered as stable foundation.

git filter-branch --commit-filter 'export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"; git commit-tree "$@"' -- <startcommit>..HEAD

Once you've inspected the results, and you're really sure that you have what you want, you can remove the backed up ref…

git update-ref -d refs/original/refs/heads/<branchname>

… or add -f option to the git filter-branch command

git filter-branch -f --commit-filter ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment