Skip to content

Instantly share code, notes, and snippets.

@qin-yu
Last active May 31, 2024 05:30
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 2024. I hope this will save time for everyone.

First-Time Git Setup

Three config files of different levels

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

Each level overrides values in the level above. You can all of your settings and where they are coming from using:

$ git config --list --show-origin

This should print nothing if git has not been used.

Use terminal to set identity and editor

  1. Set identity

    $ git config --global user.name "John Doe"
    $ git config --global user.email johndoe@example.com

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

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

    $ git config --global core.editor emacs

    If not configured, Git uses your system’s default 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

    $ ls -alhF ~/.ssh/

    if no files like ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub found then

  2. Generate key pair

    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  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

    $ ssh -T git@github.com

How to really commit in the past with Git for GitHub

The three keys are argument --date, and two environment variables GIT_AUTHOR_DATE & 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"

Note that the last timestamp of modification can be printed by ls -lv --time-style="+%Y-%m-%dT%H:%M:%S", I recommend you alias ll as my favourite:

$ ls -ahlv --color=auto  --group-directories-first --time-style="+%Y-%m-%dT%H:%M:%S"
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
...

Reference

@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!

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