Sub topic 9
Settings for pull, push, fetch, rebase and commit
This chapter assumes you can clone, commit, branch, push and pull, and that you know conceptually what a merge and a rebase do. It also assumes you know how git config writes to the global file. The settings below change the daily commands you run most often.
What you will configure
Each setting removes a repeated flag or a common annoyance. You can set them with git config --global, or paste them into ~/.gitconfig.
[init]
defaultBranch = main
[pull]
rebase = true
[push]
autoSetupRemote = true
[fetch]
prune = true
[rebase]
autoStash = true
[commit]
verbose = trueHow the settings fit together
git pull is a fetch followed by a merge or a rebase. So the fetch and rebase settings also apply when you pull.
Pull: rebase instead of merge
By default, git pull merges when your branch and the remote branch have both moved. This creates a merge commit each time you pull, which clutters history.
git config --global pull.rebase trueWith this set, Git replays your local commits on top of the fetched ones. History stays linear. If you want Git to refuse anything but a fast-forward, use pull.ff only instead. Then you choose merge or rebase explicitly when the branches have diverged.
Push: create the upstream automatically
The first push of a new branch normally fails with a message telling you to run git push --set-upstream origin <branch>. The setting below does that for you.
git config --global push.autoSetupRemote trueAfter this, git push on a new branch creates the remote branch with the same name and links the two. This setting needs Git 2.37 or later.
Fetch: prune deleted branches
When a branch is deleted on the remote, your copy of the remote-tracking branch (for example origin/feature-x) stays until you remove it. Pruning removes those stale references.
git config --global fetch.prune trueEvery fetch and pull now cleans up references to branches that no longer exist remotely. It only touches remote-tracking branches. Your local branches are not deleted.
Rebase: stash around the operation
A rebase refuses to start when you have uncommitted changes. rebase.autoStash stashes them, runs the rebase, and restores them afterwards.
git config --global rebase.autoStash trueCombined with pull.rebase, this means git pull works even when your working tree is dirty. If reapplying the stash conflicts, Git tells you and leaves the stash entry in place, so nothing is lost.
Commit: see the diff while writing the message
git config --global commit.verbose trueThe commit message editor now shows the full staged diff below the message template. Lines in that diff are not saved in the message. This helps you write an accurate message and spot leftovers like debug statements.
Check the result
git config --get-regexp '^(pull|push|fetch|rebase|commit)\.'This lists the five settings with their values. Add --show-origin if a value is not what you expect.
Exercise
You set push.autoSetupRemote true, create a branch called fix-typo, commit, and run git push. What happens, and what would have happened without the setting?
Show answer
With the setting, Git creates origin/fix-typo and links your local branch to it as its upstream, so later git push and git pull work without arguments. Without it, the push fails with a message saying the branch has no upstream, and you must run git push --set-upstream origin fix-typo once.
Exercise
A teammate deletes the remote branch feature-x. You still see origin/feature-x in git branch -r. Which setting makes the next fetch remove it, and does it delete your local feature-x branch?
Show answer
fetch.prune true removes the stale origin/feature-x reference on the next fetch or pull. It does not delete your local feature-x branch. You remove that yourself with git branch -d feature-x.