Clone a repo
git clone repo-link# or using gh
Check status (What are in stage or unstage)
git status
Show list of commit
# local, current branchgit log --graph --decorate --oneline# press "q" to quit
# remote branch b1 (local may have different commits)git fetch origingit log origin/b1 --graph --decorate --oneline
Working with branches
# create a new branch from a commitgit checkout -b new_branch_name commit_hash
# create a new branch based on another branchgit checkout -b new_branch_name existing_branch_name
# push to remotegit push -u origin branch_name
# force to pushgit push -u origin branch_name --force
# rename currently local branchgit branch -m old_branch new_branch
# verify branchgit branch
# remotegit branch -r
push renamed branch to remote and delete the one on remotegit push origin -u new_branchgit push origin --delete old_branch
# remove a local branchgit branch -d branch_name
# forcegit branch -D branch_name
# remove a remote branchgit push origin --delete branch_name
Stage and Unstage
# stage filegit add file
# stage multi-filesgit add file_1 file_2
# foldergit add folder
# stage all changesgit add .
# unstage filegit restore --staged file
# orgit reset file
Discard changes
# discard changes in a filegit restore file
# discard all changes (unstaged)git restore .
# discard all staged and unstagedgit rest --hard # don't apply for newly created files
# clean new filegit clean -df
Pull
# fetch allgit fetch --all
# pull latest commit of branch_namegit pull origin branch_name
Merge
# merge current branch (b1) in (b2)git checkout b2git merge b1
# if there are conflicts -> modify manually and thengit add conflict_file_namegit commit
# check the commit historygit log --graph --decorate --oneline
Reset current branch to a commit
# Soft - Keep all changes# (all files in commits that come after the chosen commit)# will be kept as staged, all unstagged changes will be kept as they are)git reset --soft commit_hash
# uncommit (undo the recent commit)git reset --soft HEAD~1
# hard - discard all changesgit reset --hard commit_hash
# mixed - keep working copy and reset index# (all files in commit that come after the chosen commit)# will be kept as unstagged, all unstagged will be kept as they are)git reset --mixed commit_hash
Revert commit
git revert commit_hash
Cherry-pick commit
# cherry pick a commit to the current branchgit cherry-pick commit_hash
# to another branchgit checkout another_branchgit cherry-pick commit_hash
# resolve conflicts if anygit add file_namegit cherry-pick --continue
# abortgit cherry-pick --abort
to be continued…