Git cheatsheet
Set 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 --listCombine two commits
- 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- 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-leaseWorkflow 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_branchRename branch2 branch to main branch
git branch -m branch2 mainReflect changes on remote
git push --force origin mainLink 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_branchSet remote
Connect your local repo to your GitHub repo
git remote add origin https://github.com/path/to/myrepo.gitConnect your fork to the original project
git remote add upstream https://github.com/path/to/forkedrepo.git