Skip to content

Instantly share code, notes, and snippets.

@wdullaer
Last active September 27, 2024 02:03
Show Gist options
  • Save wdullaer/6168a67f134f36a9572f9ff182bb1ebb to your computer and use it in GitHub Desktop.
Save wdullaer/6168a67f134f36a9572f9ff182bb1ebb to your computer and use it in GitHub Desktop.
Useful git scripts / onelines
#!/bin/bash
# Checkout remote branch to new local branch
git checkout -b newbranch remotename/remotebranch
# Reset a file to a version from a few commits ago
# https://stackoverflow.com/questions/9751928/git-best-way-to-remove-all-changes-from-a-given-file-for-one-branch
BASE_REV=$(git merge-base old-branch current-branch)
git filter-branch --tree-filter "git checkout ${BASE_REV} -- path-to-restore" old-branch..current-branch
# Roll back last commit
git reset HEAD~
# Remove an accidentally committed large file using bfg https://rtyley.github.io/bfg-repo-cleaner/
bfg --delete-files id_{dsa,rsa} my-repo.git
# Rewrite all commits author information
# See https://stackoverflow.com/questions/2919878/git-rewrite-previous-commit-usernames-and-emails for a script with selective updates
git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='newemail'; \
GIT_COMMITTER_NAME='committed-name'; GIT_COMMITTER_EMAIL='committed-email';" HEAD
# Hard reset to remote version of a branch
git fetch origin
git reset --hard origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment