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 --interactive base-to-start-anew
# followed by the detailed instructions on how to handle each commit

Footnotes: