Seven Git Configs to Set When Moving to a New Machine

I wrote this article back in 2022 when I moved to another company and had to set up git configurations on a new laptop. Two weeks back, I bought a new laptop, and had to do it again, so I created a Bash script to automate the process. This article is a summary of the configurations I set up, and the script is available on my GitHub.


TL;DR

To use my Bash script, clone the repository and run the script:

git clone https://github.com/aerabi/git-init.git
cd git-init
bash git-init.sh "John Doe" "john@doe.com"

This will set up the following configurations:

  • Set the default branch name
  • Make the core editor vim
  • Set the default pull method to rebase
  • Enable auto-stash
  • Create an alias for push --force-with-lease
  • Set the default branch name upon push
  • Create an alias lg for log --oneline --graph --decorate

The article will explain each of these configurations in detail, and some of the configurations are not included in the script.


0. Show Branch

Main article: Git: Determine Branch

This is not technically a git configuration but rather a Bash customization. If you're not using Bash, you can probably skip this section. For example, Zsh displays git branches out-of-the-box, and I don't have much experience with other shells.

To display the current branch in your prompt when inside a git repository, add the following line to the end of your .bashrc file:

PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\e[91m\]\$(__git_ps1 ' [%s]')\[\e[00m\]\$ "

Then, next time you open a terminal, the git branch will be displayed in the prompt, whenever you navigate to a git repository.


1. Set Name and Email

This is an essential step, but often overlooked when working on multiple repositories that require different email addresses. For example, you might commit with your work email to your company's repositories but use your personal email for open-source contributions.

To set your most-used email globally, run:

git config --global user.name "Mohammad-Ali A'râbi"
git config --global user.email "my-name-at-work@employer.com"

For repositories requiring a different email, configure it locally:

git config user.email "my-personal-id@gmail.com"

2. Set the Default Branch Name

Recent versions of git no longer default to master as the initial branch name. To avoid specifying the default branch each time, configure your preferred branch name globally:

git config --global init.defaultBranch <name>

Replace <name> with your choice, such as main, master, trunk, or even something fun like on-branch-to-rule-them-all. In the script, I set it to master, but you can change it to whatever you prefer.


3. Set Rebase as Default Pull Method

Related article: Git Merge vs Rebase: The Three Types of Merge

I dislike pull operations that create merge commits unnecessarily. This often happens when developing directly on master, making it impossible to push without resolving conflicts first. To avoid this, set rebase as the default pull method:

git config --global pull.rebase true

4. Auto-Stash

Rebasing is even more efficient when you enable auto-stashing. This feature eliminates the need to manually stash work-in-progress (WIP) changes before pulling. Git will handle the stashing, pulling, rebasing, and unstashing automatically:

git config --global rebase.autoStash true

With this setup, you can pull changes even with uncommitted work in progress, and git will seamlessly handle the rest.


5. Create an Alias for Force with Lease

Main article: Git Force vs Force with Lease

As someone who rebases frequently, I often need to use the --force-with-lease option when pushing changes. Typing this command repeatedly is tedious, so I create a shorter alias:

git config --global alias.enforce "push --force-with-lease"

Now, the command becomes much simpler:

git enforce

Note. Never use --force when pushing. It will overwrite any changes made by others on the remote branch. Always use --force-with-lease to ensure you're not overwriting someone else's work.


6. Default Branch Name Upon Push

When pushing a new branch to a remote repository for the first time, git typically requires you to set the upstream manually:

git push -u origin the-same-name-as-my-current-branch

This workflow can be streamlined by configuring git to automatically set the upstream for the current branch:

git config --global push.default current

With this configuration, git will automatically link the local branch to a remote branch of the same name during the first push.


Conclusion

These are the configurations I set up when moving to a new machine. The script that I mentioned in the beginning basically runs most of these commands for you. Here is its source code:

# Setup Git client for a new machine

# Usage: ./git-init.sh <git_user_name> <git_user_email>
# Example: ./git-init.sh "John Doe" "john@doe.com"

# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <git_user_name> <git_user_email>"
    exit 1
fi

# Assign arguments to variables
GIT_USER_NAME=$1
GIT_USER_EMAIL=$2
# Check if git is installed
if ! command -v git &> /dev/null; then
    echo "Git is not installed. Please install Git and try again."
    exit 1
fi

# Set up global Git configuration
git config --global user.name "$GIT_USER_NAME"
git config --global user.email "$GIT_USER_EMAIL"
git config --global init.defaultBranch master
git config --global core.editor "vim"
git config --global pull.rebase true
git config --global rebase.autostash true
git config --global alias.enforce "push --force-with-lease"
git config --global push.default current
git config --global alias.lg "log --oneline --graph --decorate"

# Add a message to indicate that the setup is complete
echo "Git has been configured with the following settings:"
git config --list | grep -E 'user.name|user.email|init.defaultbranch|core.editor|pull.rebase|rebase.autostash|alias.enforce|push.default|alias.lg'

Other Posts in the Series

Post thumbnail
A guide to using Git merge tools, including how to resolve conflicts using the command line and IDEs.
Issue: #17
Series: Git Weekly
Intermediate
Published: 8/3/2022