Post

Git Command Reference

Quick reference for git commands used during software releases, history inspection, and repository maintenance. For software engineers running releases or repository cleanups.

Quick Navigation

Scenario Section
Check or install semantic versioning Git Version
Inspect changes between commits Git Stats
Remove TeamCity build tags Remove TeamCity Build Tags
Set global user credentials Global Configuration
List change history for a folder Display Change History for a Folder
Rewrite commit authors Changing Git Commit Authors
Bulk-rewrite history with a script Git History Re-writing Python Script
Migrate a repository to a new URL Move a Git Repository

Git Version

Install GitVersion for more complex branch versioning.

1
2
3
4
# Install GitVersion
choco install GitVersion.Portable -y

gitversion

Git Stats

1
2
3
4
cd C:\Users\Jamie\Repos\VIPRepo

# Determine the changes between two commits (Git Version tags help find the SHA references)
git diff --stat 846044a18dd27dd4af0bc63c8360398e8403d4d4 116da05ecc711dc1612a5b57a4d453d4161d0e77

Remove TeamCity Build Tags

TeamCity build server has a build number that is often a date in reverse yyyy.mm.dd which ends up polluting a Git repository with too many tags. This style is prior to GitVersion, which uses semantic versioning and branching to provide more complex build/versioning lineage.

1
2
3
4
5
6
7
8
9
10
11
12
# List all the remotes
git remote -v

# Find all tags 
git tag -l build-*

# Remove remote tags (2 Remotes)
git tag -l build-* | foreach-object -process { git push origin --delete $_ }
git tag -l build-* | foreach-object -process { git push github --delete $_ }

# Remove local tags.
git tag -l build-* | foreach-object -process { git tag --delete $_ }

Global Configuration

Set up personal user credentials for all repositories. You can override these in each individual repository.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git config --global -i

# Setup for all repositories (These are defaults unless you have a setting in a local repository)
git config --global user.email "your-email@example.com"
git config --global user.name "Your Name"

# Setup for an individual repository (see /.git/config file to confirm the settings)
# cd C:\Users\$ENV:USERNAME\source\repos\very-important-repo\
git config user.email "your-email@example.com"
git config user.name "Your Name"

# Record and replay conflict resolutions, so you don't do the same thing multiple times (Large rebases or merges of code can be very time consuming without this option)
git config --global rerere.enabled true

# View all the recorded conflict resolutions.
git rerere status

Display Change History for a Folder

Display the last 10 changes for a folder. This can be helpful when cleaning up repositories.

1
git log -n 10 --pretty=medium -- code/*

Changing Git Commit Authors

Edit the author name and email address across a series of git commits.

1
2
3
git rebase -i <SHA-to-begin-from>
git commit --amend --author "Your Name <your-email@example.com>" --no-edit && \
git rebase --continue

Git History Re-writing Python Script

Install the Scoop package manager for Windows so Python plugins are registered correctly in the Windows environment.

1
2
3
4
5
6
7
8
9
10
11
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
Scoop install git-filter-repo 
cd scoop\apps\git-filter-repo\2.29.0\
code git-filter-repo

# Review the Shebang entry to confirm the your windows cli will execute python3 

#!/usr/bin/env python
#!/usr/bin/env python3

Install and use the python based git filter repository script.

1
git-filter-repo --email-callback "return email.replace(b'old-email@example.wrong', b'new-email@example.right')" --force

Move a Git Repository

Migrate a git repository to a new location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git clone --mirror <url to ORI repo> temp-dir

# Confirm tags and branches have been copied
git tag
git branch -a

# Remove the existing origin reference
git remote rm origin

# Add a reference to the new repository URL
git remote add origin <url to NEW repo>

# Publish to the new location
git push origin --all
git push --tags
This post is licensed under CC BY 4.0 by the author.