Skip to main content
  1. Posts/

Git Learning Notes (4)

··407 words·2 mins·
Table of Contents

My Git learning notes.

Generate patch and apply it
#

Sometimes, we need to develop the same project in different places, and in some place, we may not be convenient to push directly to the remote repo. We can generate a patch instead and apply the patch somewhere else.

First, run git diff > mypatch to generate a patch for the repo. To apply the patch somewhere, run git apply mypatch.

Ref:

Git: how to get out of detached head state
#

In git, when we check out to a specific commit using git checkout <commit_id>, git warns us that:

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 132ba1b... {some commit message}

Detached head means that we are current in no branch. To return to the previous branch we are in, use git checkout - or git checkout original_branch_name if you know the branch name.

Ref:

git: show commit that changes a specific file
#

To show all commit that changes a specific file, use git log --follow -- file_path. --follow will track the commit that changes the file before its renaming, so it is highly recommended to include this option.

Ref:

use a git mirror site for cloning
#

Due to access issues, sometimes we may want to use a github mirror service like fastgit. However, manually change the repo url from github.com to fastgit is not efficient enough. Fortunately, we can utilize the git url rewrite feature to achieve this globally. Run the following command:

git config --global url."https://hub.fastgit.xyz".insteadOf https://github.com

After that, when you clone a github repo, the actually url used will be the corresponding fastgit repo.

Ref:

Ignore a specific commit when blaming?
#

Some commit may only change the format of a file, when doing git-blame, we may want to ignore such commits. We can use git blame --ignore-rev <commit-hash> to ignore a commit. Or, use git blame --ignore-revs-file ignore-file.txt, where ignore-file.txt contains a list of commits we want to ignore.

Ref:

Related

Git line ending config
·450 words·3 mins
How to Change Commit Author Info in Git
··374 words·2 mins
Push to GitHub with Personal Access Token (PAT)
··207 words·1 min