Git: Branches & remotes
10 Git commands: Branches & remotes. Click a command to see what each part does.
Git: Branches & remotesIndex
Branches & remotes10
git switch -c <branch>Create and switch to a branchWhat each part does
switchchange branches-c <branch>create it first
git branch --merged main | grep -vE '^\*|main' | xargs -r git branch -dDelete local branches already merged into mainCautionWhat each part does
git branch --merged mainbranches fully merged into maingrep -vE '^\*|main'-v: drop lines matching; -E: extended regex: the current branch (starts with *) and main itselfxargs -r git branch -ddelete each remaining branch; -d refuses unmerged ones
git fetch --pruneRemove stale remote-tracking branchesWhat each part does
fetchdownload from the remote--prunedelete local refs to branches that no longer exist on the remote
git pull --rebase origin mainRebase your branch on the latest mainWhat each part does
pullfetch + integrate--rebasereplay your commits on top instead of creating a merge commitorigin mainremote and branch
git push -u origin <branch>Push a new branch and track itWhat each part does
-u--set-upstream: later git push / git pull need no arguments
git push --force-with-lease origin <branch>Force-push safely after a rebaseCautionWhat each part does
--force-with-leaseoverwrite the remote branch only if nobody pushed since your last fetch
git push origin --delete <branch>Delete a remote branchDestructiveWhat each part does
--deleteremove that branch on the remote
git cherry-pick <commit>Copy one commit onto the current branchWhat each part does
cherry-pick <commit>apply the changes of <commit> as a new commit here
git tag -a <version> -m "<message>" && git push origin <version>Tag a release and push itWhat each part does
tag -a <version>annotated tag (has author, date, message)-m "<message>"tag messagegit push origin <version>tags are not pushed with branches; push it explicitly
git clean -nd; git clean -fdPreview / remove untracked filesDestructiveWhat each part does
cleanremove untracked files-ndry run: list what would be removed-dinclude untracked directories-factually delete
Nothing matches.
My DevOps Playbook by Angelo John Calleja