Git Notes
Repo Management#
Push to Multiple URLs#
Git remotes can be configured to push to multiple URLs, which is great for having backups and keeping things distributed. You can add extra addresses with git remote set-url --add --push origin git@host2:project.git or edit the project's .git/config directly:
1 2 3 4 5 | |
Remotes should look something like this now:
1 2 3 4 5 | |
Merge Two Separate Git Repos#
I create a new Git repo for every game jam project, but afterwards I merge it to a single archive repo that has all my past jam entries, preserving the original commit history of each.
Starting with the new game's repo at ~/newgamerepo, I first move everything from the project root to a subfolder:
1 2 3 4 | |
Then I merge the game repo to the archive repo at ~/gamejams:
1 2 3 4 | |
The new game lands at ~/gamejams/newgame/. The --allow-unrelated-histories flag lets us merge Git histories with no common ancestors (two different repos).
Filter Changes of a Particular File by Keywords#
This lists commits that added or removed occurences of MyKeyword in a file:
1 | |
Repo Upkeep#
Remove Untracked Files#
To find all the files in the repo path that are not tracked by Git:
1 | |
And to remove them:
1 | |
-d: remove directories too, not just files-f: allows removing without an explicit permission set in.git/config-x: remove files even if they match.gitignorerules, like build output files
Fix Issues Identified by git fsck#
I ran this command to validate the objects in an old Git repo that saw a lot of changes over the years, and it found some inconsistencies:
1 2 3 4 5 6 | |
I ran the following to put the repo back into a good state and clear all the cruft:
1 2 3 4 | |
Now everything checks clean:
1 2 3 4 | |
Stop Tracking Remote Branch#
Suppose your local branch mybranch is set to track remote branch origin/mybranch, but then you delete the remote branch. The local branch still tracks the upstream you deleted:
1 2 3 | |
To clear the upstream:
1 2 3 | |
Shell#
Show the Time and Git Branch on the Shell Prompt#
I set up my Bash prompt to show the current time, and if inside a Git repo path, to also show the name of the checked-out branch. The following goes at the end of ~/.bashrc, to override any previously set PS1 (the environment variable for the shell prompt's format string):
1 2 3 4 5 6 | |
The shell prompt should look something like this now:
1 2 3 | |
Convenient Single-Letter Aliases#
I keep a few short shell aliases in ~/.bashrc for the Git commands I use the most day to day. All of them are for read-only actions though, muscle memory can be dangerous when you're tired...
1 2 3 4 5 6 | |
Colored Text in the Terminal#
Add to ~/.gitconfig:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |