Git cheatsheet

LinkIconSet Git username and email

# set
git config --global user.name ""
git config --global user.email "your.email@example.com"
 
# set current repo
git config user.name "Repository Specific Name"
git config user.email "repo-specific.email@example.com"
 
# check
git config --global user.name
git config --global user.email
 
# all config
git config --list

LinkIconCombine two commits

  1. Modify last commit directly
git add .
git commit --amend
# If original commit was pushed to remote -> conflicts, use this:
git push --force-with-lease # Safely update remote branch to match local branch
  1. Undo last commit: set files from last commit to unstage
git reset --soft HEAD~1
git add .
git commit -m "new updated commit message"
# If original commit was pushed to remote -> conflicts, use this:
git push --force-with-lease

LinkIconWorkflow to rename a branch on remote (e.g from branch2 to main)

(Optional) if you want to keep the branch "main" then

git branch -m main temp_branch

Rename branch2 branch to main branch

git branch -m branch2 main

Reflect changes on remote

git push --force origin main

Link local main branch with remoteorigin/main for push/pull (e.g. no need check out branch).

git branch -u origin/main main

(Optional) Delete temp_branch on remote

git push origin --delete temp_branch

(Optional) Delete temp_branch on local

git branch -d temp_branch

LinkIconSet remote

Connect your local repo to your GitHub repo

git remote add origin https://github.com/path/to/myrepo.git

Connect your fork to the original project

git remote add upstream https://github.com/path/to/forkedrepo.git