跳到主要内容

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 commit

How to create an empty commit?

To create an empty commit, you can use the --allow-empty option.

git commit --allow-empty -m "init commit"

You can also use the --allow-empty-message option to create an empty commit with an empty commit message.

git commit --allow-empty-message --allow-empty

This is useful in the following scenarios:

  • to create the first commit of a new project (initial commit)
  • to trigger a CI/CD pipeline

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 --no-pager log

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

git rebase

How to retain the commit signature after interactive rebase?

If you sign your commits using GPG, you can add the --gpg-sign option or -S option to retain the commit signature in the git rebase -i command.

git rebase -i {COMMIT_HASH} --gpg-sign

If you sign your commits using the Signed-off-by line, you can add the --signoff or -s option to retain the Signed-off-by line in the git rebase -i command.

git rebase -i {COMMIT_HASH} --signoff

For more information, see git rebase --gpg-sign and git rebase --signoff.

How to modify the initial commit?

To modify the commit message or author information of your project's initial commit, use the git rebase -i --root and git commit --amend commands.

Scenario: the initial commit message contains a typo and an incorrect author.

  1. Set up the project:

    mkdir test
    cd test
    git init
    git commit -m "iinit" --allow-empty
    git log
  2. Start an interactive rebase and change the word "pick" to "edit" in your text editor:

    git rebase -i --root
    # edit abcdefg iinit # empty
  3. Amend the commit to fix the message and author:

    git commit --amend -m "init" --author="test <[email protected]>" --allow-empty
  4. Continue the rebase:

    git rebase --continue
    # git push --force

git remote

How to remove multiple remotes except for the origin and upstream remotes?

git remote | grep -vE '^(origin|upstream)$' | xargs -L1 git remote remove
  1. git remote lists all of the remote names.
  2. grep -vE '^(origin|upstream)$' filters out the origin and upstream remotes.
  3. xargs -L1 git remote remove: for each remote, xargs runs the git remote remove command with the remote name as the argument.