-1

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?

Weijing Jay Lin
  • 2,991
  • 6
  • 33
  • 53

1 Answers1

0

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 F-G-H, 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).

torek
  • 448,244
  • 59
  • 642
  • 775