By Qin Yu, last updated in Feb 2025. I hope this will save time for everyone.
- First-Time Git Setup
- Connect to GitHub with SSH
- How to really commit in the past with Git for GitHub
- References
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.
-
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. -
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.
-
Set colour Also, enable coloured output in terminals:
git config --global color.ui true
-
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: -
Generate key pair
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-
Add key to ssh-agent
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
-
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 -
Test connection
ssh -T [email protected]
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"
Thanks for this, very useful - worked first time for me!