Skip to content

Instantly share code, notes, and snippets.

@qin-yu
Last active April 19, 2025 22:37
Show Gist options
  • Save qin-yu/bc26a2d280ee2e93b2d7860a1bfbd0c5 to your computer and use it in GitHub Desktop.
Save qin-yu/bc26a2d280ee2e93b2d7860a1bfbd0c5 to your computer and use it in GitHub Desktop.
Configure Git and setup GitHub on new machine

How to configure Git and setup GitHub on new machine

By Qin Yu, last updated in Feb 2025. I hope this will save time for everyone.

First-Time Git Setup

Three config files of different levels

Git uses three configuration levels, each overriding the previous one:

Scope File
system level /etc/gitconfig
user level ~/.gitconfig or ~/.config/git/config
repository level [repository]/.git/config

To view all active settings and where they are coming from:

git config --list --show-origin

This should print nothing if git has not been used.

Use terminal to set identity and editor

  1. Set Git name and email

    git config --global user.name "John Doe"
    git config --global user.email "[email protected]"

    If you want to override this with a different name or email address for specific projects, run the command without the --global option when you’re in that project directory.

  2. Set Git editor Now set emacs as the default editor:

    git config --global core.editor emacs

    If not configured, Git defaults to your system editor.

  3. Set colour Also, enable coloured output in terminals:

    git config --global color.ui true

Connect to GitHub with SSH

  1. Check keys to reuse List existing SSH keys:

    ls -alhF ~/.ssh/

    If no files like ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub are found, then:

  2. Generate key pair

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
  3. Add key to ssh-agent

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
  4. Paste public key to GitHub

    i.e. copy the content of ~/.ssh/id_rsa.pub into GitHub -> Personal Settings -> SSH and GPG keys -> New SSH Key

  5. Test connection

How to really commit in the past with Git for GitHub

The three keys are the argument --date and two environment variables: GIT_AUTHOR_DATE and GIT_COMMITTER_DATE.

GIT_AUTHOR_DATE="2024-01-01T00:00:00" GIT_COMMITTER_DATE="2024-01-01T00:00:00" git commit --date="2024-01-01T00:00:00"

You can view the last modification timestamp using:

ls -lv --time-style="+%Y-%m-%dT%H:%M:%S"

I recommend my favourite alias for ll:

$ alias ll='ls -ahlv --color=auto --group-directories-first --time-style="+%Y-%m-%dT%H:%M:%S"'
$ ll

total 4220
...
drwxr-sr-x   8 qyu hpc-088    4096 2024-02-01T18:51:03  datasets
drwx------   6 qyu hpc-088    4096 2024-02-05T20:24:59  downloads
drwxr-sr-x   2 qyu hpc-088    4096 2022-05-03T22:31:54  environments
...

To avoid pasting three times, I added another alias to ~/.bashrc:

alias gitpast='f(){ GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1" git commit --date="$1"; }; f'

Then, I can commit in the past with a single command:

gitpast "2024-01-01T00:00:00"

References

@footflaps
Copy link

Thanks for this, very useful - worked first time for me!

@TeePlunder
Copy link

TeePlunder commented Oct 30, 2023

Works fantastic, thanks for your work! 😄

@kanikash4
Copy link

On the point, thanks!

@pushkarsingh019
Copy link

Thank you so much !!

@Moibe
Copy link

Moibe commented Feb 20, 2025

Maybe at the step 3, it would be useful to specify that it it is only needed if your key has a passphrase.

@qin-yu
Copy link
Author

qin-yu commented Feb 22, 2025

Maybe at the step 3, it would be useful to specify that it it is only needed if your key has a passphrase.

Thanks for your feedback! But let's not change it: even if your key doesn't have a passphrase, adding it to ssh-agent allows the agent to manage multiple keys and automatically provide the correct one when needed. Am I right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment