git
Git usually comes pre-installed with most Linux distros, but you if not in yours, you can install it via you package manager (apt
, dnf
etc) as instructed on Git download page:
# For Fedora:
sudo dnf install git
Referring the First-Time Git Setup page:
To set name and email across all your repos, set it in global Git config as:
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"
To verify, you can view your global git config as: git config list --global
. Your global git config is stored in ~/.gitconfig
file
You can also refer my git config in the .gitconfig
file
The local config is in the .git/
folder of your project repository. To make changes to local config, just remove the --global
flag (or you can pass the --local
flag to be more explicit). You can have multiple lines setting the same variable in the config file; the last i.e. lowest line setting the variable is the final value of that variable
# Add a variable value
git config --add someSection.someKey "someValue"
# List config
git config --list
# Get value of specific variable
git config --get someSection.someKey
# Remove the variable
git config --unset someSection.someKey
# Remove all lines setting the variable
git config --unset-all someSection.someKey
# Remove an entire section
git config --remove-section someSection
In total there are 4 levels where the Git config can be stored. For the same variable name, the higher-precedence one replaces the lower-precedence one. The locations are as mentioned below, in increasing order of specificity (and thereby precedence). :
/etc/gitconfig
, it’s for ALL USERS on your system~/.gitconfig
, it is the global config for ALL REPOS of THAT USER.git/config
in your project repository, it’ defined for that SPECIFIC REPO.git/config.worktree
it’s defined for that PART of the project repoIn most cases, you’ll just be using the Global config for all your projects and sometime maybes the Local config
Generate new key pair (passing parameters: type -t
and comment -C
):
# You will be prompted for the key's storage location and passphrase
ssh-keygen -t ed25519 -C "john.doe@example.com"
Your keys would be saved in the ~/.ssh/
folder and the pair would be named like:
keyname
for private keykeyname.pub
for the public keyYou can notice it in the output or previous command as:
Your identification has been saved in /home/kumar/.ssh/id_ed25519
Your public key has been saved in /home/kumar/.ssh/id_ed25519.pub
Start ssh-agent
in the background:
eval "$(ssh-agent -s)"
Add your SSH private key to the ssh-agent
:
ssh-add ~/.ssh/id_ed25519
View your SSH public key and copy the output contents
cat ~/.ssh/id_ed25519.pub
Log into your GitHub account. Go to Settings
> SSH and GPG keys
. Click New SSH key
. Give it a title and paste the previous noted output into the Key
textarea box. Finally click Add SSH key
Test if the SSH connection method works:
ssh -vT git@github.com
In the output:
...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
...
Hi datkumar! You've successfully authenticated, but GitHub does not provide shell access.
Also try cloning any GitHub repo via the SSH method url: git@github.com:USERNAME/REPONAME.git
References:
SSH identities are stored in ~/.ssh
folder and SSH manages these identities
ssh-add -l
or ssh-add -L
ssh-add -D
.pub
file of that identity in ~/.ssh
folderLet’s call the derived child branch you’re working on as DEV
and the base branch you’re syncing it with as MAIN
# Before starting work, sync DEV with MAIN
git switch DEV
git fetch origin
git rebase origin/MAIN
# Do work, Commit and Push to your DEV
git add .
git commit -m "your message"
git push origin DEV
# Create a PR from DEV → MAIN
# Squash and Merge (cleans up commit history)
# After merge, sync DEV with updated MAIN
git switch DEV
git fetch origin
git rebase origin/MAIN
#
# If rebase rewrote DEV's history, your local branch will now differ from origin/DEV
# VSCode and `git status` may show "diverged" (such as 1 ahead, 1 behind)
# This is because rebasing rewrites commit hashes, even if content is identical
# To resolve this and sync your local DEV with origin/DEV, overwrite the remote with:
git push --force-with-lease origin DEV