简体   繁体   中英

how can I git checkout and overwirte branch by force?

I want to checkout branch and delete current exiting branch.

eg git checkout -b --by-force $branch , so the branch will be based on current checkout branch; I always got $branch name is exist.

I don't want to merge, I just want to have a clear checkout new with existed name.

I down't want to have several steps to accomplish this action. aka, I don't want to delete/merge the branch and checkout the branch.

Checkout and delete/merge should be one command statement.

What should I do?

I think you want:

git checkout -B <branch-name>

To be clear, remember that git checkout never makes any new commits, but it can create, or reset, a branch name . That is, suppose you have:

          C--D--E   <-- branch1 (HEAD)
         /
...--A--B
         \
          F--G--H   <-- branch2

You can make the name branch1 point to commit H , and in effect check out commit H , now with the command:

git reset --hard branch2

which discards your current work-tree and index contents by replacing them with those from commit H and gives you:

          C--D--E   [abandoned]
         /
...--A--B
         \
          F--G--H   <-- branch1 (HEAD), branch2

But I think you instead want to make the name branch2 point to existing commit E , abandoning commits FGH , and also attach HEAD to the name branch2 . That's what:

git checkout -B branch2

will do, resulting in:

          C--D--E   <-- branch1, branch2 (HEAD)
         /
...--A--B
         \
          F--G--H   [abandoned]

This operation will leave your index and work-tree undisturbed, so that if you have changed various files to not match the contents stored in commit E , they remain changed in your work-tree (and if changed in the index, they remain changed in the index as well).

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