Skip to content

Commonly Used Git Commands

Change the capitalization of filenames in Git

shell
git mv hello.txt Hello.txt

Delete untracked and ignored files in Git

Delete untracked files

shell
git clean -fdn # dry-run
git clean -f  # only files
git clean -fd # files and directories

Delete ignored files

shell
git clean -fXn # dry-run
git clean -fX

Delete both untracked and ignored files

shell
git clean -fxdn # dry-run
git clean -fx # only files
git clean -fxd # files and directories

Cherry pick a commit to another branch

shell
## switch to target branch, for eaxmple: master
git checkout master

## cherry-pick
git cherry-pick -x <commitHash>

Delete local and remote branch

shell
git branch -d bug_xyz # delete local branch
git push origin --delete bug_xyz # delete remote branch

Remove local branches that not exist on remote

shell
git remote prune origin

Rename both local and remote branch

shell
# Rename the local branch to the new name
git branch -m <old_name> <new_name>

## or If you are on the branch you want to rename
git branch -m <new_name>

# Delete the old branch on remote
git push origin --delete <old_name>

# Or shorter way to delete remote branch [:]
git push origin :<old_name>

# Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of <new_name>.
git branch --unset-upstream <new_name>

# Push the new branch to remote
git push origin <new_name>

# Reset the upstream branch for the new_name local branch
git push origin -u <new_name>

Update remote origin

shell
git remote set-url origin <new_url>

Reset local to remote branch status

shell
git fetch origin
git reset --hard origin/master

Create a shallow clone with a history truncated to the lastest commit.

shell
git clone --depth 1 <remote_repo_url>
# or
git clone --depth 1 <remote_repo_url> -b <branch_name>

# back fill hisotry
git pull --unshallow

# backfill part of the history
git fetch --depth=100

Increase Git Buffer Size

When you encouter a error message like this:

error: RPC failed; HTTP 400 curl 22 The requested URL returned error: 400
send-pack: unexpected disconnect while reading sideband packet

The reason might be that the push is too large and it exceed the default buffer size. You can increase the post buffer and try again.

shell
git config http.postBuffer 524288000
git push

References