Git has different levels of configuration that apply to different “scopes”:
- system (almost never needed; not covered here)
- global (which is “global for a user” scoped)
- local (which is specific to one local clone)
- worktree (which only applies to the current worktree; only relevant if you work with
worktree
s) - file (not covered here but you can set a git configuration option, when relevant at the file level, to one file)
The precendence of these configuration scopes is as follows (highest precedence first):
- file
- worktree
- local
- global
- system
As an example, if I have a local (“clone” scoped) configuration option where foo.bar
is set to false
but my global (“user” scoped) configuration option is set to true
then when I run git config --get foo.bar
in that overrided local clone repository, I will get back false
.
# preconditions/setup
cd ~/
git config --global --add foo.bar true
git config --get foo.bar true
mkdir -p ~/tmp/test-repo
cd ~/tmp/test-repo
git init
git config --local --add foo.bar false
git config --get foo.bar # should show 'false'
git config --get-all foo.bar # should show 'true' followed by 'false' on a new line; showing precedence in reverse order
cd ~/
git config --get-all foo.bar # should show 'true'
You can set a new value of the configuration like the following examples.
Set committer and author name for commits made across user’s repositories locally
git config --global --add user.name "Susan Potter"
Automatically setup tracking to the remote branch whenever you create branch from it
git config --global --add branch.autosetupmerge always
Automatically setup local branches to always rebase on git-pull from tracking branch
git config --global --add branch.autosetuprebase always
You can get the value of a configuration option like so:
git config --global --get branch.autosetuprebase
List all your configuration options including overrides:
git config --list
- Your user’s
--global
config can be found at$HOME/.gitconfig
. - It looks something like this:
[user]
name = "Your name"
email = "[email protected]"
[color]
diff = auto
status = auto
[apply]
whitespace = strip
[pager]
color = true
[alias]
lsbr = branch -vv
# the rest of your ~/.gitconfig
- Configuration that only applies to the current local clone of the repository
- Find configuration inside the
$(git root)/.git/config
file - Override configuration for the local clone/repository scope with:
git config --local --add foo.bar true
- Only if you are inside a worktree
- Find configuration inside the
$(git root)/.git/worktree/<name>/config
(I think)
Configuration | Value | Reason |
---|---|---|
branch.autosetupmerge |
always |
local branches automatically track remote branch they were created off of |
branch.autoseetuprebase |
always |
local branches will automatically rebase on top of the tracked branch on git pull , i.e. it expands to git pull --rebase ${remote} ${branch}
|
push.default |
nothing |
when running git push do not push any default branch to any default remote; nothing is assumed. Also consider current or upstream . |
rebase.autoStash |
true |
stashes uncommitted changes to a local stash to rebase |
user.name |
You name at work | sometimes we go by different names |
user.email |
Your work email address | Gitlab has options to check committer and author emails upon receive; git defaults to ${USER}@${MACHINE_NAME} which is probably not what you want. |
rerere.enabled |
true |
enables rerere as I suggested in #dev after Grant’s presentation two Friday’s ago when he talked about fearing rebasing. |
- When you don’t want to commit yet because you are still working code changes out, but want to remember where you were at later.
- Stash uncommitted changes like so:
git stash
- To pull back the last stash off the stash stack on top of the current branch:
git stash apply
- Means you can stash local only tool configurations and apply to different branches.
HEAD^
- 1 commit before headHEAD^^
2 commits before headHEAD~5
5 commits before head
git reflog
directories can be found at .git/logs/refs/heads/
, .git/logs/HEAD
, and also .git/logs/refs/stash
if the git stash has been used on the repo.
DEMO: CLI
git checkout -
switches branch to prior branchgit fetch remote branch && git rebase -i remote/branch
- fetch latest from remote for branch branch and then rebase your local branch on top of those changes from remote/branch you just fetchedgit branch -vv
- snazzier output on the CLI
if you want to work jointly on a branch:
git pull --rebase remote branch
git push remote branch --force-with-lease
ensures you have at least pulled down pair’s contributions locally (but need to rebase too)
— END OF FIRST SESSION on 06/03/2020