Skip to content

Instantly share code, notes, and snippets.

@daephx
Last active November 12, 2022 14:09
Show Gist options
  • Save daephx/6c59d2a4f9b42d071b77b41d763861c3 to your computer and use it in GitHub Desktop.
Save daephx/6c59d2a4f9b42d071b77b41d763861c3 to your computer and use it in GitHub Desktop.
[Git Commands] Common commands for managing git version control #Git #Examples

Git Commands

Various useful command / scripts and information that I keep forgetting and are potentially incompatible with git-for-windows alias configurations.

Table of Contents


Deleteing a remote branch

git push origin --delete feature/login

Amend Author & Username

Sometimes you messed up and used the wrong configuration or something of the sort. Either way, you somehow got the wrong information embedded within your commit log and would like to change it.

  • Be mindful of which commit you choose as to not cause accidental overwrites; especially of work that might not be yours.

  • Do your best to spot these sort of issues before merging to shared / public repositories as it's more trouble than it's worth to update all the commit hashes with a rebase.

Explicit Overwrite

  • Checkout the desired branch or specify the branch with:
    -i origin/branch

  • Execute a command with:
    -x 'git <command>'
    --exec 'git <command>'

git rebase --root --exec "`"git commit --amend --author '$(git config user.name) <$(git config user.email)>' --no-edit`""
git rebase --root --exec "git commit --amend --author "$(git config user.name) <$(git config user.email)>" --no-edit"
git rebase -i --exec 'git commit --amend --no-edit --author="NewName <[email protected]>"'

filter-branch approach

*Be aware of: WARNING: git-filter-branch has a glut of gotchas generating mangled history*

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='yourname'; GIT_AUTHOR_EMAIL='[email protected]'; GIT_COMMITTER_NAME='yourname'; GIT_COMMITTER_EMAIL='[email protected]';" HEAD;
echo "\nGit fliter-branch :: Replace Email\n"
echo "  This will replace the author in number of commits filtered by the provided email."
echo "  It is useful for replacing a private email address that has been commited with a public / noreply."
echo "\n\n  WARNING: This rewrites history.\n"
echo "What is the email you wish to replace?: "
read OLD_EMAIL
echo "Filter commits containing: '$OLD_EMAIL'"

SAMPLE=5
echo "Sample Commits: ($SAMPLE)"
FMT_STRING='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an> %Creset'
LOG=(git --no-pager log --color --pretty=format:$FMT_STRING --abbrev-commit); $LOG ; echo "\n\nTotal number of commits: $($LOG | wc -l)\n"

read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
  exit 1
fi

echo "\nRemoving and replacing all references of private email address"
git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then
  GIT_AUTHOR_EMAIL=$(git config --global user.email);
  GIT_AUTHOR_NAME="$(git config --global user.name)";
  GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
  GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

Preconfigured Overwrite

Requires that you set user values within ~/.gitconfig file.

git rebase -i {DESIRED_COMMIT} -x "git commit --amend --reset-author -CHEAD"

Alternatively: User all commits

git rebase -i --root -x "git commit --amend --reset-author -CHEAD"

Example: Rebase Hash

Replace {DESIRED_COMMIT} with last commit hash before the you intend to edit.

  • d0a228c - Commit Message #1 (9 minutes ago) <MY_USERNAME>
  • 9f16454 - Commit Message #2 (9 minutes ago) <MY_USERNAME>
  • 75968b3 - Commit Message #3 (10 months ago) <NOT_MY_USERNAME>
  • 51d385d - Commit Message #4 (10 months ago) <NOT_MY_USERNAME>

You'll want to select #3 75968b3 as your {DESIRED_COMMIT} because rebase will start from there and move through all the commits that follow to the current HEAD.

Bonus: Log Format

Put this under [alias] in your git config and run with git ls for a nicer log format.

ls = !git --no-pager log --color --abbrev-commit --pretty=format:'%C(red)%h%C(r) - %s %Cgreen(%cr)%C(r) %C(bold blue)<%an>%C(r)%C(yellow)%d%C(r)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment