Various useful command / scripts and information that I keep forgetting and are potentially incompatible with git-for-windows alias configurations.
git push origin --delete feature/login
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.
-
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]>"'
*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
Requires that you set user values within ~/.gitconfig
file.
git rebase -i {DESIRED_COMMIT} -x "git commit --amend --reset-author -CHEAD"
git rebase -i --root -x "git commit --amend --reset-author -CHEAD"
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
.
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)'