跳到主要内容

Git

git branch

How to delete a local branch?

git branch -D {branch-name}

How to delete all local branches except for the main branch?

git branch | grep -v "main" | xargs git branch -D

The following is the detailed explanation according to ChatGPT:

quote

This command uses a pipe (|) to chain together several commands:

  1. git branch lists all of the local branches in the repository.
  2. grep -v "main" filters out the "main" branch from the list of branches. The -v flag inverts the match, so it shows all branches that do not match the pattern main.
  3. xargs git branch -D takes the remaining branches and passes them as arguments to the command git branch -D, which deletes the branches.

Please be careful when running this command as it will delete all branches except main.

git clone

How to create a shallow clone?

--depth is used to specify the number of commits to be cloned. When cloning a large repository, you can use this option to reduce the time required to clone it.

git clone --depth 1 {YOUR_REPO_URL}

And then, you can get only the latest commit.

cd {YOUR_REPO_NAME}
git log

For more information about how to fetch the remaining commits, refer to How to fetch the remaining commits after creating a shallow clone.

git fetch

How to fetch the remaining commits after creating a shallow clone?

After creating a shallow clone, you can use the following command to fetch the remaining commits.

git fetch --unshallow

The git pull --unshallow command is equivalent to the preceding command.

git log

How to show the commit logs of a specific file?

git log {FILE_PATH}

How to show the latest commit log of a specific file?

To limit the number of commits to output, use the -<number>, -n <number>, --max-count=<number> option:

git log -1 {FILE_PATH}
# git log -n 1 {FILE_PATH}
# git log --max-count=1 {FILE_PATH}

How to show the commit logs without paging?

git log --no-pager

How to show commit logs in a custom format?

To pretty-print the commit logs in a given format, use the --format=<format> option. For more details, refer to Pretty Formats.

git log -1 --format=format:"%at,%H,%an,%as,%ar%n"

The description of the format is as follows:

  • %at: The author date, UNIX timestamp.
  • %H: The commit hash.
  • %an: The author name.
  • %as: The author date in the YYYY-MM-DD format.
  • %ar: The author date, relative format.

How to find which commit introduced/deleted a specific text?

Refer to this answer.

git log -c -S "text" {file-path}

The following is an example of finding the commit that introduced Reading List in the website/sidebars.js file.

git log -c -S "Reading List" website/sidebars.js

How to find which commit deleted a specific file?

git log --diff-filter=D -- {FILE_PATH}

The following is an example of finding the commit that deleted frontend/src/types/sqlReviewConfig.yaml file in the bytebase/bytebase repository.

git log --diff-filter=D -- frontend/src/types/sqlReviewConfig.yaml

How to find which commit changed a specific file?

git log --full-history -- {FILE_PATH}

Refer to this answer.

The following is an example of finding the commit that changed frontend/src/types/sqlReviewConfig.yaml file in the bytebase/bytebase repository.

git log --full-history -- frontend/src/types/sqlReviewConfig.yaml