Everything about using Git from the command line - from practical tips and tricks to clever bash snippets.
Only clone a single branch
git clone <url> --branch <branch> --single-branch <folder>Change the author of the last commit in git
git commit --amend --author "New Author Name <correct-email-address@domain.com>"Update user email address in all commits
git filter-branch -f --env-filter '
OLD_EMAIL="liwenkz@asos.com"
CORRECT_NAME="Liwen Knight-Zhang"
CORRECT_EMAIL="development@liwen.name"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tagsthen
git push --force --tags origin 'refs/heads/#'Using git-log to get repo stats
# number of releases (git tags based) in a given date range
git log --tags --since=2020-07-01 --until=2021-06-30 --simplify-by-decoration --pretty="format:%ai %d" | grep "tag: v" | wc -l
# line of code changed
git log --since=2020-07-01 --until=2021-06-30 --format= --numstat | awk '{s+=$1; s+=$2} END {print s}'Copy all branches to a different repository
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git push --allGit worktree
Git worktree lets you check out multiple branches from the same repository into separate directories. Each worktree has its own working directory with isolated files, while sharing the same Git history.
Create a new worktree:
# Create a new worktree with a new branch
# Assume .worktrees is the directory you keep the worktrees
git worktree add .worktrees/feature-a -b feature-a
# Or create a worktree with an existing branch
git worktree add .worktrees/bugfix-slugs bugfix-123Manage worktrees:
# List all worktrees
git worktree list
# Remove a worktree when done
git worktree remove ../project-feature-a
# Don't forget to delete the branch after removing the worktree
git branch -D feature-adiff against a tag to see accumulated changes
git diff scope-statement-v1.1 -- policies-processes-governance/scope-statement.md
git diff statement-of-applicability-v1.1 -- policies-processes-governance/statement-of-applicability.mdWorking with GitHub
Using multiple GitHub accounts with SSH keys
To use two separate GitHub accounts with two different SSH keys on the same computer, you can define host aliases in ./ssh/config to distinguish them.
Host github-lwkz
HostName github.com
User lwkz
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host github-spd
HostName github.com
User lwkz-spd
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa_SPDAdd SSH private keys to your SSH agent as usual:
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_SPDTest connections:
ssh -T git@github.com
ssh -T git@github-spdYou should see something like this:
Hi lwkz! You've successfully authenticated, but GitHub does not provide shell access.
Hi lwkz-spd! You've successfully authenticated, but GitHub does not provide shell access.
Clone your repositories using aliases defined earlier:
git clone git@github.com:lwkz/repo.git
git clone git@github-spd:news-lwkz/repo.git # Notice the @github-spd host