HomeToolsAbout a20k

Branch

Configure Default Branch Names

Setting default branch name for all future projects

git config --global init.defaultBranch <default_branch_name>

Renaming the branch that was just created

git branch -m development
  • m is --move --force
    • It forcefully renames the branch from master

Local Branch Actions

List Branches

# list all local branches git branch # list all local and remote branches git branch -a

Create a New Branch

Create a new branch off current working branch

git checkout -b new_branch_name

Push to a new remote branch that tracks the current local branch

git push --set-upstream origin branch_name

Switch Branches Locally

Switch to the previous selected working branch

git switch -

Delete Local Branch

git branch -D branch_name

Clean up stale branches in local machine

Deletes local branches when corresponding remote branches do not exist

# list branches to be cleaned-up git remote prune origin --dry-run # clean up branches git remote prune origin

Remote Branch Actions

Check the remote repository's url that is associated with the local branch

git remote -v

Show local and remote branch hash and status of the remote branch

git branch -vv

Add/Remove Remote Origin

# add remote origin git remote add origin remote_url_string # Unlink local's link to remote branch git remote remove origin # Pull remote branch to local that does not exist locally git fetch origin branch_name

Safely pulling in changes to local

Pulling in latest changes in main safely while working locally

git stash git switch main git pull git switch local_branch git rebase main git stash pop

Fetching just one branch from remote to local

git fetch remote_branch_name local_branch_name git branch local_branch_name FETCH_HEAD git switch local_branch_name

FETCH_HEAD is a short-lived ref to keep track of what has just been fetched

  • When you use git pull, it fetched the commit, invokes git merge, merging the FETCH_HEAD into the current branch

Force Update Remote origin/main to local commit

git reset --hard old_commit_sha git push -f origin

Reset the current branch to an old commit SHA

  • Alter the remote origin to hard reset commit SHA
© VincentVanKoh