Delete Git Branch | Virtual Species

Delete Git Branch

Git Branch

1. Checkout "MyBranch" before deleting {OldBranch}. It makes "MyBranch" current/working branch.
     $ git checkout {MyBranch}
2. Delete local "MyBranch". This is the safest option for deleting a local branch since branch doesn't get deleted if there is any unmerged changes exist.
     $ git branch -d {OldBranch}
3. This is for force delete. It deletes the local branch with any existing unmerged changes. IMPORTANT: It can make you cry if you don't pay attention!
     $ git branch -D {OldBranch}
4. It deletes the remote branch and easy to remember.
     $ git push {remote_name} --delete {OldBranch}
5. Following command also to delete the remote branch.
     $ git push {remote_name} :{OldBranch}
6. Once we are done deleting remote branch, we can prune to get rid of remote tracking branches by executing any of the following commands.
     $ git remote prune origin
     $ git fetch origin --prune
     $ git fetch origin -p
7. Once we are done deleting remote branch, we need to execute one of the following commands on other machine to remove obsolete tracking branches.
     $ git fetch --all --prune 
     $ git fetch -a -p
     This command lists branches that can be deleted/pruned on the local.
     $ git remote prune origin --dry-run
8. Execute this command to see a list of all your references in the repository.
     $ git for-each-ref
9. Execute any of these commands to see just remote-tracking branches.
     $ git branch --remotes
     $ git branch -r
10. Execute any of these commands to see both local and remote tracking branches.
     $ git branch --all
     $ git branch -a
 TIP: Run this command to delete all branches on remote that are already merged into master.
     $ git branch -r --merged | grep -v master | sed 's/origin\///' | xargs 
     -
n 1 git push --delete origin

Check out Git Branches.

No comments:

Post a Comment