Undo pushed merge cos I meant to rebase
- checkout (detached) the last commit before the merge commit. (I don’t know how to do it in bash yet—I use VS Code GUI)
- create new branch from it
git checkout -b <new branch name> - checkout my main branch (in this case
v4notmain(yes I was fixing this repo—turns out I don’t likenpx quartz sync, I want to commit granularly)) and reset to the last commit I still want to includegit reset --hard <the commit I want to go back to> # or usually `HEAD~2` - rebase the new branch onto the main branch
git checkout main git rebase <new branch name> - force push to origin
git push --force origin v4
I’m still not sure about the reset in step 3. It might not be generally applicable. Well, I will know more next time this happens.
Rebase to main
git checkout main
git rebase <new changes branch>I need to write this down because I never remember whether it’s that or the other way around
Delete local branch
git branch -D <branch name> # -D means --delete --force
git branch -d <branch name> # will error if branch hasn't been mergedMulti-account Git config essentials
a.k.a. what I do when setting up multiple git accounts on a new OS
-
generate an ssh key for each account following this GitHub tutorial. give it a name that represents the associated account, not just ”id_ed25519“
-
create ssh config file:
~/.ssh/config # study Host study.github.com Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_ed25519-study # play Host play.github.com Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_ed25519-play -
create folder for each account, create separate config files for each account
~/.gitconfig-study [user] name = student email = student@users.noreply.github.com signingkey = ~/.ssh/id_ed25519-study.pub [url "git@study.github.com"] insteadOf = git@github.com~/.gitconfig-play [user] name = fun email = fun@users.noreply.github.com signingkey = ~/.ssh/id_ed25519-play.pub [url "git@play.github.com"] insteadOf = git@github.com -
assign each config to its respective directory
~/.gitconfig [includeIf "gitdir:~/study/"] path = ~/.gitconfig-study [includeIf "gitdir:~/play/"] path = ~/.gitconfig-play [gpg] format = ssh [init] defaultBranch = main [core] editor = code --wait autocrlf = input [pull] rebase = true
Change commit author name and email on past commits
Sometimes, for some reason, I have my config email set to automation@github.io or whatever and forget to change that for the new project I’m working on. This can change commit author name and email on past commits with certain email without modifying the timestamp. (Source)
$ git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "automation@whatever.io" ];
then
GIT_AUTHOR_NAME="Correct Name";
GIT_AUTHOR_EMAIL="correctemail@example.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD