Git

Published on 30 December 2022

New commands: git restore, git switch

Import GPG

Use GPG

Open a git repo in cmd line. Do:

Commit signing would now work in smerge too.

Remove Passphrase

Run gpg --list-secret-keys --keyid-format=long to list keys, chars XXXX… in sec rsa4096/XXXX... is your key. Do

gpg --edit-key XXXX...
# gpg --edit-key 2F48AC189D93FC22
# a prompt will show up
# type
gpg> passwd
# type original passwd and hit enter, then hit enter keep hitting enter (4 or 2 times) to accept and confirm blank passwd
# not exit out of gpg prompt

If you are on macOS (Catalina), this might not work as it keeps showing you the prompt of “Enter new passphrase”. Try this instead.

Change Name and Email Across a Series of Commits

Do:

git config user.name "John Doe"
git config user.email johndoe@example.com

followed by:

git rebase -r <some commit before all of your bad commits> --exec 'git commit --amend --no-edit --reset-author'

to use these new name and email values.

Source

See Changes Done in a Merge Commit to Resolve Conflicts

Use git show <merge-commit-id> to see what changes were made to resolve conflicts.

Source

Get Line(s) History

This is better than blame:

git log -L110,110:/lib/client.js

Or specify a series of lines like git log -L110,115:/path/to/file. That / at start (before lib) is also not necessary to put if you’re at root of git repo. Also use --reverse to make the very oldest commit appear at the top, as that’s what you’re looking for.

Source: https://stackoverflow.com/a/27108677/7683365

Listing Git Branches Containing the Given Commit

To list the branches containing the given commit, you should run the git branch command with the --contains option as follows:

git branch --contains <sha1-commit-hash>

If you want to track the remote-tracking branches, you should add the -r option. For tracking both the local and remote-tracking branches, use the -a option.

Git Bisect

Start with

git bisect start bad-commit good-commit

then keep doing

git bisect bad
# or
git bisect good

until git tells you where it found first bad commit.

Copy the hash somewhere, then do git bisect reset to get back where you were.

If there is a script/cmd that can return non-zero code, you can use it to automatically bisect. See https://stackoverflow.com/a/71422806 answer quoting another answer from 2014

https://stackoverflow.com/questions/4545275/vim-close-all-buffers-but-this-one
https://stackoverflow.com/questions/33666191/how-to-bufdo-only-on-modifiable-buffers-in-vim

Subl

To walk to a branch commit (while other branches are being shown alongside of it), use alt+up/down


Change file and tell git to ignore the changes https://stackoverflow.com/a/39776107
^ skip-worktree is preferred over assume-unchanged (see why)
Alternatively, if the file is fresh you can use a local .gitignore file called .git/info/exclude

Change gitignore and automatically delete ignored files https://stackoverflow.com/a/43883886


Interactive Rebase

git rebase -ir <commit-hash-before-the-starting-point>

r tries to preserve merge commits (i.e. it doesn’t tries to make a linear history). Your branch commits always appear at the bottom, in the order:

oldest
...
...
most recent

--abort and --continue both apply to git rebase and to git merge.

Using Ssh Keys for Different Accounts on the Same Domain

You can have different SSH keys for different accounts. Here I’ve one for work and other for personal.

Host github.com
	HostName github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

Host gh
	Hostname github.com
	AddKeysToAgent yes
	IdentityFile ~/.ssh/gh

This Host github.com and Host gh matters. This is what you pass when cloning using git clone git@github.com:ajitid/fzf-for-js.git. Rather than using github.com after @, I can use gh instead.

You would notice that cloning works but pushing doesn’t. Run ssh-add -D and try again.

FZF Stable Releases Merge Strategy

Checkout to dev and do:

git merge origin/stable-releases --strategy=ours

This creates a merge commit and in it essentially overrides stable-releases with dev contents (source). It also does not produce any merge conflict or left empty files. Next step is to create a PR for stable-releases in which I make a squash commit.