Git Recipes

2025-05-29, Thu

Everybody seems to to have their own Git recipes. This post is yet another one that record solution to common problems I have encountered while using Git.

1. Git on Windows

The reason why I even bother Windows is because my current employer demands it, along with following problems when neither WSL nor Cygwin is available.

First, to store credential so that we don't need to input username and password every time git pull is called1:

git config --global credential.helper store

Second, in order to accommodate long file paths, we need to adjust the value of core.longpaths as well2:

git config --system core.longpaths true

Then comes this file mode problem I have encountered the other day: some files with permission of 755 have been changed to 644, which results into git status reporting them without actual content difference. To ignore mere permission udpate, run3:

git config core.fileMode false

Another common warning that shows up is this "LF will be replaced by CRLF" message, which could be dealt with4 through:

git config core.autocrlf true

2. Large File Storage

Git LFS5 introduces .gitattribute and stores files that are not text into dedicated large file storage, such as JAR, ZIP, PDF, PNG, MP4 files, etc. The problem is when we download source from remote repository as ZIP file (instead of git clone the whole repository), those files are all empty. In this case, use git lfs to manually retrieve the actual files' content6:

git lfs fetch --all
git lfs checkout

Other recipes will be added when new problems are encountered.

3. Rewrite Branch History

When the time comes to re-write the commit history of a branch, it's often caused by the necessity to select, correct or remove some commit. In either case, the steps typically boil down to

  1. Create a new base branch, i.e. git branch base-branch-name <commit-hash>, and
  2. Rebase current branch to this new base, i.e. git rebase <base-branch-name>

A typical example looks like this7, 8:

git checkout branch-that-needs-clean-up
git branch base-to-start-anew HEAD^5
git rebase base-to-start-anew
# or git rebase --interactive base-to-start-anew
# followed by the detailed instructions on how to handle each commit
git rebase --continue
git rebase --abort
git commit -m "fresh commit message"
git push --force-with-lease

4. Multiple Remote Origins To a Single Repo

There are two approaches to get this done: push to multiple URLs with one command, use separate remote names. The first approach could be done like this:

git remote add origin <URL-1>
git remote set-url --add --push origin <URL-2>
# verify the multiple-push config
git remote -v

This configuration pulls only from the primary URL, but pushes to multiple servers at once.

The second approach applies when we want to pull or push to different remote servers independently.

git remote add origin <URL-1>
git remote add backup <URL-2>
# now push to different remote servers
git push origin main
git push backup main

More infos could be found in "Git Basics - Working with Remotes"9.

5. GitHub Recipes

5.1. Add SSH key to multiple accounts

GitHub typically allows one public key being used for only one account. If we need to connect to multiple accounts at the same time, a simple solution is to generate multiple key pairs. e.g.

ssh-keygen -t ed25519 -C "some_new_mail@example.com" -f ~/.ssh/id_ed25519_some_new_name"

Now we can used the new ~/.ssh/id_ed25519_some_new_name.pub in GitHub.

Footnotes: