Skip to content

Git: Branches & remotes

10 Git commands: Branches & remotes. Click a command to see what each part does.

Index
10 items
Click a command to see what each part does.

Branches & remotes10

  • git switch -c <branch>Create and switch to a branch

    What each part does

    1. switchchange branches
    2. -c <branch>create it first
  • git branch --merged main | grep -vE '^\*|main' | xargs -r git branch -dDelete local branches already merged into mainCaution

    What each part does

    1. git branch --merged mainbranches fully merged into main
    2. grep -vE '^\*|main'-v: drop lines matching; -E: extended regex: the current branch (starts with *) and main itself
    3. xargs -r git branch -ddelete each remaining branch; -d refuses unmerged ones
  • git fetch --pruneRemove stale remote-tracking branches

    What each part does

    1. fetchdownload from the remote
    2. --prunedelete local refs to branches that no longer exist on the remote
  • git pull --rebase origin mainRebase your branch on the latest main

    What each part does

    1. pullfetch + integrate
    2. --rebasereplay your commits on top instead of creating a merge commit
    3. origin mainremote and branch
  • git push -u origin <branch>Push a new branch and track it

    What each part does

    1. -u--set-upstream: later git push / git pull need no arguments
  • git push --force-with-lease origin <branch>Force-push safely after a rebaseCaution

    What each part does

    1. --force-with-leaseoverwrite the remote branch only if nobody pushed since your last fetch
  • git push origin --delete <branch>Delete a remote branchDestructive

    What each part does

    1. --deleteremove that branch on the remote
  • git cherry-pick <commit>Copy one commit onto the current branch

    What each part does

    1. 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 it

    What each part does

    1. tag -a <version>annotated tag (has author, date, message)
    2. -m "<message>"tag message
    3. git push origin <version>tags are not pushed with branches; push it explicitly
  • git clean -nd; git clean -fdPreview / remove untracked filesDestructive

    What each part does

    1. cleanremove untracked files
    2. -ndry run: list what would be removed
    3. -dinclude untracked directories
    4. -factually delete

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…