简体   繁体   中英

How to delete GIT branch from master in remote repo and start a new branch from another branch?

I accidentally created a new branch from master branch in origin and committed my changes to that branch.

Let's call the new branch I created Accidental_Branch .

But I was supposed to create the new branch from a different branch, let's say Feature_Branch . Master branch is not up to date and my team commits all the changes to Feature_Branch from which I was supposed to create the new branch. This is what I want.

  1. Delete the branch I created, ie Accidental_Branch . I don't want to lose changes I made in my local since I want to create a new branch with that change.

  2. Create a new branch from Feature_Branch . And commit changes. Changes meaning, changes I have committed to Accidental_Branch and my uncommitted changes in local.

Please help me out. I am new to GIT.

If you don't want to lose the uncommited data, you can temporarily but safely save the data using the $ git stash command. This will store the data in a hidden place. After that, you can switch between branches or create a new branch using $ git checkout -b {new_branch_name}

Now, you will be landed at the new branch. In this branch, you can get the data that is stashed before using the $ git stash apply command.

After that, you can safely delete the previously created branch using the following commands:

For deleting the local branch use the following command:

$ git branch -d {branch_name}

You can force delete the branch using the -D option as follows:

$ git branch -D {branch_name}

You can delete the remote branch using the following command:

$ git push origin --delete {branch_name}

See if this is what you are looking for. But you had also said in your question that you had committed changes to the branch. If you had committed changes then stashing will not work. Please clear your question a bit in that case. It will be great if you can state the branch names in your question.

To save uncommitted changes, you can generate a stash (cashing the selected files) with git stash save "optional stash-message" .

So you can caching your files and so you don't lose them. After git stash you can delete the accidentally generated branch by git branch -d <branch_name> local and for remote deleting git push origin --delete <branch_name> .

Checked out the branch where you will start your new branch ( git checkout <branch_name> ) and create there your new branch with git branch <branch_name> and checked out this as explain above and then apply your generated stash with git stash apply stash@{0} .

How git stash works is simple explained on this website-blog .

EDIT, WHY QUESTION IS NOT REALLY CLEAR ENOUGH:

If the changes in your accidentally branch already pushed to remote server, so git stash doesn't work. In this case, you must revert the commit, that include your changes where you don't want to lose. The git revert command will create new changes with the opposite effect and thereby undo the specified old commit, so the changes are available again and not lost.

After git revert you have your changed files in the working directory and you can create the new branch. If the new branch starts in another branch, you must stash the uncommited files again, create and checkout the new generated branch and apply the stash in this branch as explain above.

You can just rebase the commits you created onto the location you want.

Depending on how many there are, it may be easier to manually create a new branch, cherry pick them one at a time and delete the old one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM