Skip to content


To Delete a local branch

git branch -d branch_name

To delete a remote branch

git push origin --delete master

To delete a local branch with --force option

git branch -D branch_name

To clone a specific branch

git clone -b <branch> --single-branch <url>

Create a remove branch

  1. Create a local branch first $ git checkout -b <BRANCH_NAME>
  2. Push it to fremote $ git push <REMOTE_NAME> <BRANCH_NAME> eg: git push origin stage1:development

To change the origin from One Git repo to another

  1. Remove the origin from local repo git remote rm origin

  2. Add the new origin git remote add origin

  3. Set the local branch to track the remote

$ git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'

Remove local branches which are no longer on remote

git remote prune origin

To initialyze a directory and push it to a remote repo

git init --initial-branch=BranchName
git remote add origin
git add .
git commit -m "Initial commit"
git push -u origin BranchName
git branch --set-upstream-to=origin/BranchName BranchName

To Delete all commit history of a branch

# Checkout to a new branch without history
git checkout --orphan latest_branch
# Add all the files and commit changes
git add -A
git commit -am "commit message"
# Delete the branch
git branch -D main
# Rename the current branch to main
git branch -m main
# Finally, force update your repository
git push -f origin main