When you buy a car or a bicycle, you first adjust the seat position and saddle height to suit your body size. It is the same with git configuration.
In this article, I will share the git setup I use all the time.
User name & Mail address
git config --global user.name "<name>" && \
git config --global user.email "<email>"
- Replace
<name>
with my name and<email>
with my mail address.
Alias for existing command
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch
Global ignore setting
git config --global core.excludesfile ~/.gitignore_global
- This allows you to put your own specific ignore settings in
~/.gitignore_global
in addition to the.gitignore
for each project, which will be applied to all git operations across all projects.
Push only the branch you are now working on
git config --global push.default simple
Make --rebase
the default behavior during git pull
git config --global pull.rebase true
- This prevents unintentional creation of a merge commit if the branch being pulled has been modified locally.
Make --prune
the default behavior during git fetch
git config --global fetch.prune true
- This will remove local branches that were deleted remotely when
git fetch
orgit pull
was performed.
Set the width of indentation for tab characters
git config --global core.pager 'less -x4'
- In this example, the pager (
less
command) option specifies tab indent width as 4
Use nvim
as the editor to be used when committing
git config --global core.editor 'nvim'
- I use several different text editors for different purposes, but I prefer to use
nvim
withgit commit
.
Do not fast-forward when merging
git config --global --add merge.ff false
git config --global --add pull.ff only
- Fast-forward merging makes it difficult to follow the history of work on a branch. Therefore, avoid unintentional fast-forwards when merging.
- However, fast-forwarding is not a problem for
git pull
cases in most cases 1, so we enforce fast-forwarding in the case of pulls. - See also: gitのmerge --no-ff のススメ - Qiita
Output line numbers in the result of git grep
git config --global grep.lineNumber true
Visualize differences in whitespace (including newline codes)
git config diff.wsErrorHighlight all
EDIT: 2022-07-31
dotfile
As mentioned in the comments, these are stored in .gitconfig
. And I have added these settings to the dotfiles repository.
https://github.com/snaka/my-dotfiles/blob/master/.gitconfig
Thanks to all who commented.
See also
-
In my experience, this happens when you are temporarily changing code while reviewing a PullRequest created by someone else in your local environment. In that case, fast-forward is better because local changes are gathered at the top of the history, making it easier to work with when you finally undo the changes. ↩
Top comments (31)
Great tips. Feels like this could be the start of a script. In fact, I can imagine an open source CLI tool for setting up Git just right on new environments, perhaps using a user-specific config file stored online...
The essential thing for me was add gpg sign and signing key
Hey :) Great post, thanks for sharing!
How would you deal with 2 git emails on the same laptop?
one for github (perso) and one for work (pro)
The best would be to default to the email perso (and the associated GPG key), and when working from ~/Projects, always use the work email
Not sure if it is feasible ^^
Perhaps you can.
I wrote an article about it, please check it out.
dev.to/snaka/use-a-different-git-c...
Nice tips!
However I was surprised to see "Do not fast-forward when merging" with the reason of making it "difficult to follow the history".
My personal experience is the exact opposite, having multiple automatic "Merge branch..." commits makes the commit history incredibly polluted.
I understand your surprise, as I have seen several different organizations managing branches with different policies.
There isn't enough in this comment section to discuss that, so I will post a separate entry after I get more thoughts on it.
To setup my environment for Linux or Windows I usually use my dotfiles : github.com/voku/dotfiles/blob/mast...
Thanks for the link to your dotfiles! It is very helpful. 👍
Good if you find something useful there. 😊
Do you always use cli commands or do you sometimes commit/push/pull/delete branches from your IDE?
I use
tig
andgit
CLI depends on the situation.github.com/jonas/tig
I often use
tig
to see logs and diffs, or commands with a specific commit ID, such asgit cherry-pick <commit-id>
orgit rebase -i <commit-id>
.On the other hand, I use the
git
CLI for risky commands such asgit reset
, and for tasks that are faster with shell completion such asgit checkout <branch-name>
.I see. I’ll check
tig
later out of curiosity!I prefer using my IDE for most commands (I make sure to know what’s going on behind the scenes though) as I find faster (Ctrl+K to open commit window, type message, Ctrl+Shift+K to commit and push).
Thanks for the tips this is really helpful.
Great tips, thank you!
Neat...I install github desktop and click buttons.
Good article, it has its uses if you must use command line :)
Your dotfile is also helpful. Thanks for sharing ✨
git --checkout -b 'this post' 🔖✅
Great tips will gonna use in my next project.....
Some great suggestions in this list that I never thought about before, especially the global ignore outside of a project.
Thanks for sharing.
You also need to take care about this mistakes.
cmsinstallation.blogspot.com/2021/...
Thanks for the link to a helpful article 👍
I read that git push isn't supported fromthe CLI due to security reasons. Is there a nice way to get around that (other than using Github Desktop)?
I use git push from the CLI all the time. I think you may have read that where it talks about setting up ssh keys.
If you are using your IDE or the Desktop UI for git, you are likely signed in to your github account through them. No issues.
With CLI, you need to set up SSH for authenticating your pushes from the CLI:
docs.github.com/en/authentication/...
Thank you, after switching to SSH it works just fine
No problem! Glad you got it working.
Sorry, I didn't catch what you meant by "git push isn't supported formthe CLI due to security reasons".
Could you please add what you mean by that?
Yep my bad, I wasn't clear enough and I also realized I didn't read the documentation properly :(. I was trying to push some changes to my repo through the terminal. However, I was trying to push via HTTPS. According to GitHub, it no longer supports authenticating through HTTPS for the CLI (I had mistakenly assumed that pushing from CLI in all cases was no longer supported, which is pretty silly in hindsight). After I switched to SSH as suggested by Nathan (from the below response), it works just fine. Side note: great tips now that I can use CLI again!
Edit: If you're curious, you get a 'fatal authentication' error if you try to push via HTTPS. Apparently, they stopped this since August 2021.
Ah, it was clear to me what you said.
I'm pleased that your problem was resolved as a result.
git push, from the command line, to GitHub, over https does work. It's just that you don't use your password to login. Instead, you use a token, which you first need to generate in your settings somewhere.
Thank you for the additional information.
As you mentioned, it is possible to push via HTTPS with the
git
command by using a Personal access token (PAT).Here is a link to the GitHub documentation for reference.
docs.github.com/en/authentication/...