Do you use Git literally every day? Me too.
I've been using the basics of Git for years, but only recently did I learn some new powerful tricks.
If you want to move past add, commit, and push, keep reading.
- Praise for Boost Your Git DX by Adam Johnson
- Make a dotfiles Repository
- Use Shell Aliases for Git Commands
- Automatically Create Upstream Branches
Praise for Boost Your Git DX by Adam Johnson
All of these tips have come from Adam Johnson's book, Boost Your Git DX. I'm only sharing (with permission) three tips from the book. He's written almost 300 more pages than what I outline here.
This book transformed my day-to-day operations. As a self-taught engineer, I have found myself starved for mentorship, and Boost Your Git DX has been my most influential mentor in 2023.
If you're looking to maximize the usefulness of Git past basic version control, you will love this book.
Make a dotfiles Repository
The dotfiles repository serves as a place to save your configuration files, allowing you to git clone it down to new computers or accounts. This is a huge plus for your developer experience if you work on multiple machines.
I can now use my optimized shell on Windows Subsystem for Linux for every version of Ubuntu I have installed, my home Ubuntu server, my powerhouse desktop computer, and my more portable laptop. You could even clone it into your web servers!
Check if you have files to save:
If so, set up a dotfiles repository:
Set up a config directory to store config files and move in your Git config:
mv ~/.config/git config/git
Removing the prepended . from .config means the folder won't be hidden by default.
Then, make a script to create a symlink for the config files back to their expected location inside the ~/.config directory:
chmod +x setup.sh
Inside setup.sh, add the following:
# Enable shell script strictness
# Show each command when running script
# Ensure config directory exists
mkdir -p ~/.config
# Link Git config if it doesn’t exist
[ ! -e ~/.config/git ] && ln -s "$PWD/config/git" ~/.config/git
If the target ~/.config/git file already exists, it will not be overwritten.
If it doesn't exist, the script will create a symbolic link or "symlink" with ln -s.
Run the script to link your config files from your dotfiles directory into your home directory:
Check if it's working, commit the files to your repo, and push them up to your preferred Git host.
When you want to pull your config down to a new server
git clone <GITHUB_REPO_LOCATION>
If you've made updates to your config from one machine, add + commit + push them to your dotfiles repo, then pull them down to your other machines.
Use Shell Aliases for Git Commands
Though I believe the dotfiles repo has saved me the most headache when working on multiple machines, these shell aliases are the change that I use the absolute most.
Aliases allow you to type abbreviations for longer commands. Some examples:
- g for git
- ga for git add
- gc for git commit
To enable these, you can type them directly into a single shell session, or you can add a line to your shell's startup file (Bash: ~/.bashrc, Zsh: ~/.zshrc) to persist them across all interactive login shell sessions.
# ex: alias <name>=<command>
alias ga=git add
alias gc=git commit
alias gp=git push
Make sure to restart your shell to load the new configuration if you put it in your .bashrc or .zshrc file.
gc -m "added new config"
gp --set-upstream origin main
Automatically Create Upstream Branches
Once a project is more established, I prefer to work on new features in their own respective Git branches.
The problem is that when I push my changes to GitHub, the branch does not yet exist on the remote, so I have to type this wordy gp -u origin new-feature-1.
But what I really want to type is gp.
To automatically create an upstream branch and link it to your local branch, you can use the push.autoSetupRemote option.
git config --global push.autoSetupRemote true
This will add a new setting to your global Git configuration file:
autoSetupRemote = true
This Git feature was added in Git v2.37, so you may check your git --version to see if you need to upgrade.
There you have it! Three Git tips I use all the time since discovering them in Adam Johnson's book, Boost Your Git DX.
May your contribution streak be long and light green 🙏.