Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I destroy and recreate a Git remote branch in one command?

Tags:

git

In Git, I sometimes work on long-running branches. I like to rebase on master from time to time to make merging easier when I'm ready.

After rebasing, I can't push a previously-pushed branch to a remote, because my branch's history no longer agrees with the remote's history of that branch. So I have to delete it first.

This is my current workflow:

git checkout my_branch
git rebase master
git push origin :my_branch  # Delete remote version of the branch
git push origin my_branch   # Push up my new version of history on this branch

Is there a single, atomic command that could replace the last two commands?

like image 263
Nathan Long Avatar asked Jun 01 '12 14:06

Nathan Long


People also ask

Can we delete remote branch in git?

Instead of using the git branch command that you use for local branches, you can delete a remote branche with the git push command. Then you specify the name of the remote, which in most cases is origin . -d is the flag for deleting, an alias for --delete . remote_branch_name is the remote branch you want to delete.

How do you clear a remote branch?

To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .

How do I delete a branch without merging?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.


1 Answers

If you are allowed to rewrite the remote branch, you can use git push --force my_remote my_branch.

like image 141
Michael Wild Avatar answered Oct 07 '22 02:10

Michael Wild