Let's say we develop a feature in branch, think it's complete, merge it to master and delete the branch. Later bug is discovered in this specific feature and someone goes and resets the HEAD on global repository (which you shouldn't do). Now, we could have fixed that bug but don't have the branch anymore and whole feature is lost. Is there a way to restore those changes, and is it overall good idea to preserve feature branches after merge for a while?
Assume before the merge the commit history was like this.

After merging feature to master, it became this.

And now the branch feature has been deleted and it's been like this.

As we can see, the commit history of feature is still there though the ref feature has gone. A branch ref is just a variable that stores the sha1 hashes of the commits that it points to from then to now.
M is the merge commit(only if it was a true merge, a non-fast-forward merge) that was created when merging feature to master. It has two parents, D the first parent, which master was pointing to, and F the second parent, which feature was pointing to. If we merged master to feature and created M, then F is the first parent, and D the second.
The first parent can be referred to by M^ and the second parent via M^2.
Now if you want to recreate the branch ref feature, supposing M's value is af2343242, you could run git branch feature af2343242^2. And it's going to be like this again.

is it overall good idea to preserve feature branches after merge for a while
It depends. Some believe it's good to keep all the changes that were once there, including all the commits and all the branches, tags, etc. After all, these are the precious legacy of a project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With