29

Let's say I have a develop branch. I create a feature branch from this to develop a feature. Once the feature is developed, it is merged back into develop. Pretty much like shown here:

enter image description here

Is there a way I can freeze the feature branch so that no further commits can be made to it?

The reason for not outright deleting the branch is so that viewing the history can still show the feature branch and that if there needs to be a tweak made to the feature then it is possible for someone to create a new feature branch from the last commit of the previous feature.

millie
  • 2,642
  • 10
  • 39
  • 58
  • Related post - [What is a “stale” git branch?](https://stackoverflow.com/q/29112156/465053) – RBT Jan 14 '19 at 10:36

5 Answers5

23

Christopher is right, tagging will help you do this. I recommend deleting the branch name too to make it a little harder for someone to checkout the branch and make edits.

First, merge the branch into develop

git checkout develop
git merge --no-ff feature_1 

Then checkout the branch

git checkout feature_1

Then create a tag, with a comment.

git tag -a -m "Freezing a feature branch that fixes.." feature_1_frozen

Then delete the branch

git checkout develop
git branch -d feature_1

After doing this, you won't be able to checkout the branch by name. Instead you'll be able to checkout the tag by name, this will put you into a detached head state which will deter changes to the code.

Now to wrap things up and sync with origin...

Push the update and new tag

git push --tags origin develop

Delete the remote feature branch

git push origin :feature_1
Community
  • 1
  • 1
gjcamann
  • 621
  • 4
  • 14
  • It's worth noting this will only delete it from your local repository. To prevent others from grabbing it in their local repos, you'd need to delete it on the remote with `git push :feature_1`. You'd most definitely want a tag name that includes the branch name in this case, or else you're going to cause confusion at some point in the future when someone wants to find this state. – Christopher Jun 12 '12 at 14:18
  • So just to be clear, the command to delete the branch doesn't delete the commits from that branch, it just deletes the branch name? – millie Jun 12 '12 at 15:48
  • Branches are pointers to specific commits. There's a good explanation here: http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is. As long as the tag commit exists in gjcamann's example, you should be able to reach that commit state. I'm ambivalent about deleting the branch as I think it'd get confusing, "Wait, what was that branch called? Hmmm. Can't remember the tag name. That was two months ago. Better go digging." but that's just personal preference. Whether or not you delete the remote branch depends on your comfort level and workflow. – Christopher Jun 12 '12 at 16:40
  • 1
    Millie, you're right. Deleting a branch only deletes the branch name, all the history is still there - it's most obvious when using gitk. – gjcamann Jun 12 '12 at 16:56
10

Just tag it.

git tag -a frozen -m "Feature branch frozen here."
git push <remote> frozen

Sure, someone could come along later and push to the branch, but the tag shouldn't change unless it's forcibly overrode. You could configure your remote to reject force pushes if you're concerned about it, or even sign the tags with a GPG key to ensure authenticity.

Getting the state of the feature branch when it was frozen is as simple as git checkout frozen. Developers can branch from this point at will using one command: git checkout -B <new_branch> frozen.

Christopher
  • 42,720
  • 11
  • 81
  • 99
  • 1
    Just to be clear, if someone does come along and make commits on the "frozen" branch, it's trivial to throw those changes away and set it back to where it was supposed to be. Just `git reset --hard frozen` while on the branch. – wadesworld Jun 12 '12 at 13:06
2

You could use something like gitolite or gerrit for access controls and permission along branches, tags and repos.

Have a look here:

Community
  • 1
  • 1
Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55
1

Consider git-freeze as mentioned in Git - Branch status (frozen, inactive, etc.).

Community
  • 1
  • 1
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
-2

I am using the "Git Bash" console to freeze the branch:

[Solution worked Best during Oct 2018]

Don't have Git Bash?

Here is how to install and use the Git Bash console:

Reference:

https://github.com/msysgit/msysgit/releases/

https://help.github.com/articles/set-up-git/

How to freeze a branch

git checkout {branch-to-keep-alive}
git merge --no-ff {branch-to-freeze} 

If git asks for a merge message, type it, then use [Esc] key then type ":wq" command to save and exit.

You have to go to visual studio and make sure that you can build the solution successfully (with {branch-to-keep-alive}).

git checkout {branch-to-freeze} 

git tag -a -m "{your-description}" {tag-for-the-branch-to-freeze}

Convention: create the tag like this: {branch-name}_frozen

git checkout {branch-to-keep-alive}

git branch -d {branch-to-freeze} 

git push --tags origin {branch-to-keep-alive}

git push origin :{branch-to-freeze} 

How to merge a branch with master:

git checkout {your-working-branch} 

Git merge master

Open vs and resolve merge conflicts if there is any. Always rebuild the whole things.

git checkout master
git merge development

There won't be any conflicts now and everything is set to go.

Git Bash Console:

enter image description here

BHUVANESH MOHANKUMAR
  • 2,747
  • 1
  • 33
  • 33