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
master
# list all local branches git branch # list all local and remote branches git branch -a
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 to the previous selected working branch
git switch -
git branch -D branch_name
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
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 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
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
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
git pull
, it fetched the commit, invokes git merge
, merging the FETCH_HEAD
into the current branchorigin/main
to local commitgit reset --hard old_commit_sha git push -f origin
Reset the current branch to an old commit SHA