Reputation: 25
I currently have a master
and features
branches in AWS Code Commit. I need to create more branches for the project.
Supposing that, I will create the new ui-improvement
branch and a few days later, I deleted the branch locally and remotely. Afterwards, Is it possible to create same branch name before used ui-improvement
?
Upvotes: 0
Views: 938
Reputation: 45649
While it is somewhat true that git doesn't care if you re-use a branch name, in practice it is not without potential consequence. tl;dr: just because you remove it from your local and the remote, doesn't mean it's gone from every repo.
Why you can do it: To git a branch is a ref. That is, it's a name for a pointer to a commit. If you delete a ref, git doesn't try to keep track of the fact that the particular name was used before. (In fact even the reflog for the branch is discarded, which probably has decent reasons but is in a way unfortunate.)
Why you probably shouldn't: Among refs, what's special about branches is that they are expected to move, and have a default rule for how they are expected to move. (This is in contrast to refs like tag, which are simply expected to typically not move.)
Specifically, a branch is expected to move from parent to child as new commits are created. If at each of two different moments there exists a branch with a given name, then it is expected that the commit it pointed to at the earlier moment is "reachable" (by parent pointers) from the commit it points to at the later time.
Now, if you've removed all traces of a branch (removed from local and remote), it would seem safe enough to then re-use the branch name. But since you have a remote - or for that matter, since you're using a distributed version control system - we should at least entertain the possibility that yours isn't the only clone of the remote.
Suppose you start your project up.
A -- B -- C <--(master)
and you create a branch
A -- B -- C <--(master)
\
D -- E <--(fixes)
and you've pushed this to origin, and another developer has pulled all of this to their local. So they have
A -- B -- C <--(master)(origin/master)
\
D -- E <--(fixes)(origin/fixes)
Now you continue work and soon you have
H -- I <--(a_branch)
/
A -- B -- C ------------ M<--(master)
\ /
D -- E -- F -- G <--(fixes)
and so far that's all good, because every branch has only moved forward. The other dev pulls and is up to date.
H -- I <--(a_branch)(origin/a_branch)
/
A -- B -- C ------------ M<--(master)(origin/master)
\ /
D -- E -- F -- G <--(fixes)(origin/fixes)
But now you delete fixes
, since it's merged. And something comes up on a_branch
so you decide you need a new fixes
branch.
K <--(fixes)
/
H -- I <--(a_branch)
/
A -- B -- C ------------ M<--(master)
\ /
D -- E -- F -- G
So the other dev does a fetch and now has
K <--(origin/fixes)
/
H -- I <--(a_branch)(origin/a_branch)
/
A -- B -- C ------------ M <--(master)(origin_master)
\ /
D -- E -- F -- G <--(fixes)
And now from their repo's point of view, it seems that the fixes
branch has moved in an unexpected way. This is not hard to fix, but the most "obvious" ways out of the errors they'll get are wrong and will cause weird results. It's annoying, and your workflow shouldn't routinely create this condition.
Upvotes: 4
Reputation: 2837
Yes, it is possible to use the same branch name in a new branch (branch name of an already erased one - both locally and remotely removed).
Once the branches are wiped out, there will be no name conflicts, even if you used the erased branches in merges before.
Upvotes: 2