Git-branch cheatsheet

How to manage multiple branches in a single repo with git

view on github

Git-branch cheatsheet

  • Git commands for managing multiple branches in a single git repository :
# add $remote_repository as 'origin' remote for current repository
git remote add origin "git@github.com:$user/$remote_repository.git"

# display remotes urls for current repository
git remote -v

# list all local and remote branches
git branch -la

# create local new branch $newbranch by copying 'master' (or any other branch)
git branch -c master "$newbranch"

# switch to branch $newbranch (do not use git switch)
git checkout "$newbranch"

# pushes local branch $newbranch commits to remote 'origin' ($newbranch is created in the remote if it's missing)
git push -u origin "$newbranch"

# display HEAD, all local and remote branches and their current commits as well as commit history for each branch
git log --oneline --decorate

# display HEAD, all local and remote branches / commits arborescence (if history is divergent)
git log --oneline --decorate --graph --all

# incorporate all the commits made to $sourcebranch into $targetbranch
# all commits since $sourcebranch commits history diverged from $targetbranch's will be merged
# useful when creating a new branch from $targetbranch for debugging purposes
# merge it back into $targetbranch when debugging is done
git merge "$targetbranch" "$sourcebranch"

# undo all changes and deletions on current branch since last commit (preserves newly created files)
git reset HEAD --hard