Post

How to quickly and easily set up Git on Windows

Setup Git with sensible configurations on Windows very easily

How to quickly and easily set up Git on Windows

Install

Check how to install scoop (and Winget if used) if you have not installed it first

To install Git you have different options.

Install it using a package manager:

  • Scoop - Check scoop.sh to see how to install it.
1
scoop install main/git
  • Winget - Check Install WinGet to see how to install it.
1
winget install -e --id Git.Git

Configuration

Git has three locations for configurations:

ScopeLocationDescription
SystemC:\Users\username\.gitconfigUsually not touched
User/Global$HOME\.gitconfigCommon config: username, email, ssh key, etc.
Repository<git-repo>/.git/configRepo specific configuration
PortableC:\ProgramData\Git\configOnly applies when using a Portable Git

User gitconfig file

Anything added directly on the .gitconfig file can be added by using the command line

For example:

1
2
3
git config --global user.name "John Doe"

git config --global user.email johndoe@example.com
  • Location: $HOME\.gitconfig or C:\Users\<your-username>\.gitconfig

The simplest configuration you need for Git to work properly is:

1
2
3
4
5
6
7
8
9
[user]
   name = <your-name>
   email = <your-email>

[init]
   defaultBranch = main

[core]
   sshCommand = 'C:\\Windows\\System32\\OpenSSH\\ssh.exe'

Let’s go deeper and have a nicer configuration:

1. Alias: create aliases for your common operations like status, checkout, branch, or commit

1
2
3
4
5
6
7
8
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit -v
    unstage = reset HEAD --
    last = log -1 HEAD-
    aa = add --all

2. Diff tool: Install difftastic or delta

To visualize changes more easily from the terminal, configure a diff tool.

Install with Scoop:

1
2
scoop install main/difftastic
scoop install main/delta

Add delta or difftastic as your diff tool:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[interactive]
    diffFilter = delta --color-only --features=interactive

[diff]
    tool = difftastic

[difftool]
    prompt = false

[difftool "difftastic"]
    cmd = difft "$LOCAL" "$REMOTE"

[pager]
    difftool = true

3. Merge tool: Use IntelliJ Idea merge tool or another like Meld or vimdiff

I love IntelliJ idea merge tool but you can use any, for example for IntelliJ Idea

1
2
3
4
5
[merge]
    tool = intellij

[mergetool "intellij"]
    cmd = idea merge $LOCAL $REMOTE $BASE $MERGED

4. Other useful configurations

  • Auto git pull rebase
  • Set git push auto setup remote
1
2
3
4
5
[pull]
    rebase = true

[push]
    autoSetupRemote = true

5. For Windows, it might be recommendable to support long file name paths

1
git config --global core.longpaths true

6. Complete configuration example

If we put everything together we will result on a ~/.gitconfig file that looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[user]
    name = <your-name>
    email = <your-emai>

[init]
    defaultBranch = main

[core]
    pager = delta
    sshCommand = 'C:\\Windows\\System32\\OpenSSH\\ssh.exe'
    longpaths = true

[alias]
    st = status
    co = checkout
    br = branch
    ci = commit -v
    unstage = reset HEAD --
    last = log -1 HEAD-
    aa = add --all

[interactive]
    diffFilter = delta --color-only --features=interactive

[diff]
    tool = difftastic

[difftool]
    prompt = false

[difftool "difftastic"]
    cmd = difft "$LOCAL" "$REMOTE"

[pager]
    difftool = true

[merge]
    tool = intellij

[mergetool "intellij"]
    cmd = idea merge $LOCAL $REMOTE $BASE $MERGED

[pull]
    rebase = true

[push]
    autoSetupRemote = true

Gitignore Files

Install gitignore utils for fetching gitignore file templates:

1
scoop install main/gitignore

Then for example:

1
gitignore java

Git ignore files specify files not to track. Usually configured at the repository level, but you can have a global one for common ignores.

SSH Key

Using HTTPS for Git is not recommended. Use SSH.

  1. Generate a new SSH Key
1
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. Name it something like github_ed25519.
  2. Enter a secure passphrase.

If you need to avoid typing the passphrase every time, use ssh-agent:

1
2
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent

You will need to add your key to the agent

1
ssh-add c:/Users/<username>/.ssh/github_ed25519

For more info:

GUI Git Clients

If you prefer to use a GUI Client there are popular apps you can take a look

Others

GitHub CLI

If you use GitHub it might be convenient to use the Github CLI

Install gh by running

1
scoop install main/gh

Login into your account by running

1
gh auth login

Manage pull request within a git repository by using:

1
2
3
4
5
6
7
8
9
10
11
# list
gh pr list

# create
gh pr create --fill

# view
gh pr view --web

# review
gh pr review 123 -r -b "needs more ASCII art"

Open the git repository on GitHub

1
gh browse

Gitea CLI

If instead of GitHub, you are using Gitea then you also have a cli tool available

Gitea CLI (tea) allows interactions with Gitea from the terminal.

Install with Scoop:

1
scoop install main/tea

Create a Gitea API token at User/Settings/Applications.

  1. Login
1
tea login add --name my-gitea --url https://my-gitea.com --user {username} --password {your_password} --token {generated_token}
  1. Check options
1
tea --help
  1. Configure PowerShell autocomplete
1
tea autocomplete powershell

Then append to $PROFILE:

1
& $home\.config\tea\tea.ps1
  1. Open Web Browser from terminal
1
tea open
  1. Review PR locally

List pulls:

1
tea pulls

Review a pull request:

1
tea pull review {pull_number}

Learn git

Although using git basic operations is easy to learn, when doing more complex operations Git can become a bit overwhelming. Mastering Git is very challenging.

It is recommended to learn how branches, commits and blobs work in order to understand what you are doing. The reason why is that even if you know how to perform an operation, in order to understand what you are doing you need to map somehow how the operation will perform.

It is recommended to do the following training:

This post is licensed under CC BY 4.0 by the author.