tj / git-extras

GIT utilities -- repo summary, repl, changelog population, author commit percentages and more
MIT License
17.31k stars 1.21k forks source link

git delete-branch deleted wrong branch #638

Open hallzy opened 7 years ago

hallzy commented 7 years ago
$ git delete-branch test
Deleted branch test (was e76dbe9).
error: remote-tracking branch './test' not found.
To .
 - [deleted]         work

As you can see above, I wanted to delete my test branch which I created with:

git checkout --track -b test

After running that command I went back to master branch and did the git delete-branch test as above. I Believe that because I did not create a remote it must have confused it somehow.

Luckily, I was able to retrieve my stuff back from my work branch though, so that is good at least.

I realize now that what I was doing doesn't really make any sense, but I still feel like it probably shouldn't and gone to delete a completely different branch

spacewander commented 7 years ago

The work is deleted because git delete-branch will delete the upstream of given branch automatically. It seems that feature make a mess sometimes...

spacewander commented 7 years ago

@hemanth @nicolaiskogheim Should we add an new option, to avoid deleting the upstream branch?

hallzy commented 7 years ago

I should specify that I am not against this command deleting remote branches, as that is what I expect.

In this particular case test branch did not have a remote so delete-branch deleted the local test but failed to find th remote tracking branch of test since it didn't exist.

It then went and deleted my LOCAL work branch... hence, this command actually went and deleted 2 local branches, one of which (work) I did not want it to delete....

My local work branch was tracking origin/work.

so test and work are not related at all, and both the branches that were deleted were local... no remotes were deleted when I ran this command this time.

hallzy commented 7 years ago

This is exactly what happened, note the output of some of the git config commands that are used in the script itself:

Currently in master

$ git checkout --track -b test
M   .submodules/git-radar
Branch test set up to track local branch master.
Switched to a new branch 'test'

currently in test

$ git config branch.test.merge
refs/heads/master

Still in test

$ git config branch.test.remote
.

Still in test

$ git checkout work
M   .submodules/git-radar
Switched to branch 'work'
Your branch is up-to-date with 'origin/work'.

Now in work

$ git delete-branch test
Deleted branch test (was 08a1cbc).
error: remote-tracking branch './test' not found.
To .
 - [deleted]         master

At this point my local test branch is gone, and my local master branch is gone (again, I was able to recover the master branch from origin, so all is good.

hallzy commented 7 years ago

Note that the output of git config branch.test.remote has a ., and is not empty, and that was not a typo

spacewander commented 7 years ago

@hallzy Because it is created with --track, test is tracking master. So in git's term, master is the upstream of test, though it isn't a branch existed in remote repo. This explains why git config branch.test.remote has a ., because test doesn't track a regular remote.

The fact is usually counterintuitive. Maybe we should skip deleting local 'upstream' branch?

tuminoid commented 6 years ago

delete-branch just deleted my origin/master instead of origin/feature :face_with_head_bandage:

Does #639 fix this BS?

spacewander commented 6 years ago

@tuminoid Sorry for the loss caysed by our tool... Could you try the code from the master branch to see if the thing works well?

tuminoid commented 6 years ago

It appears that delete-branch does absolutely nothing, not even warning or error, when trying to delete branch was pushed with git push. Does not delete local or remote branch.

If branch is pushed with git push -u, ie. it tracks feature branch, then it works as expected.

$ git clone git@github.com:tuminoid/test
Cloning into 'test'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.
$ cd test
$ ll
total 8
-rw-r--r-- 1 tumi staff 1072 May  4 07:52 LICENSE
-rw-r--r-- 1 tumi staff   12 May  4 07:52 README.md
$ git checkout -b fea-foo
Switched to a new branch 'fea-foo'
$ vi LICENSE
$ git commit -a -m "foo"
[fea-foo 7df8596] foo
 1 file changed, 3 insertions(+)
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 334 bytes | 334.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:tuminoid/test
 * [new branch]      fea-foo -> fea-foo
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git delete-branch fea-foo
$ git branch -la
  fea-foo
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/fea-foo
  remotes/origin/master
$ git checkout fea-foo
Switched to branch 'fea-foo'
$ git delete-branch fea-foo
$ git push -u
Branch 'fea-foo' set up to track remote branch 'fea-foo' from 'origin' by rebasing.
Everything up-to-date
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git delete-branch fea-foo
Deleted branch fea-foo (was 7df8596).
Deleted remote-tracking branch origin/fea-foo (was 7df8596).
To github.com:tuminoid/test
 - [deleted]         fea-foo

Not exactly what I'd expect the command to do, but at least its not deleting master anymore.

spacewander commented 6 years ago

@tuminoid Interesting. I notice that when you try to git push the newly created fea-foo, it successed. But when I tried it, I got:

¥ LC_ALL=C git push         
fatal: The current branch fea-foo has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin fea-foo

What's your version of git? (I am using git version 2.14.1)

tuminoid commented 6 years ago

I have git config push.default = current, thats why I don't need to specify the traditional git push origin fea-foo, as it becomes the default action.

I'm using 2.17.0, though it is not relevant.

spacewander commented 6 years ago

@tuminoid I installed git with 2.17.0 and configure push.default = current. But the git-sed in the master branch works well in my machine. Probably I missed something.

Would you add set -x in bin/git-sed before installing git-extras and paste the output here?