SusyP
SusyP

Reputation: 149

Git: start over in new branch, removing commits from history

First question here!

We are using git for version control of a project. Our repo looks like this:

master: C0-C1-C2-C3-C4
                \            
    development: C5-C6-C7

When we started the project, we were advised to use gitflow workflow. Anyway, since we are a small team with no previous experience in git, working on a fairly small project, we now think that might be overkill. Instead, we'd like to start using a feature branch model.

Long story short, we'd like to have something like this:

master: C0-C1-C2-C5-C6-C7

So that from C7 and on, we can start over "from scratch" (but keeping what we've made until now), but now using separate branches for features.

As you can see, we'd also like to get rid of C3 and C4. This is because they are completely useless (those commits add some files that were added in C6 and C7), and also because we'd like to start with a single, clean master branch, without merges in our history.

I thought that would be pretty simple, but after a couple of hours googling, I'm a little bit lost. I read about creating a new orphan branch, copying the contents of C7 and then deleting my other branches, but I'm not sure that would be the right approach.

Oh, and by the way, we are not able to create a new repo. This is a private repo given to us in the context of an academic course, and it's the only one we have.

Thanks in advance.

Upvotes: 5

Views: 1347

Answers (3)

Schwern
Schwern

Reputation: 165248

You want

C0-C1-C2-C5-C6-C7 [master]

The problem becomes simpler when you look at your repository like so.

C0-C1-C2-C5-C6-C7 [development]
       \
        C3-C4 [master]

"Branches" are just labels on commits. They can be renamed and moved as you like. development is already where you want master to be. So rename master and development.

git branch -m master old/master
git branch -m development master

Ta da!

C0-C1-C2-C5-C6-C7 [master]
       \
        C3-C4 [old/master]

Since your master has diverged from the remote master you will need to force push.

git push -u --force origin master

The -u will ensure your new master branch tracks the remote master.

Finally, when you're ready, delete old/master.

git branch -D old/master

It can still be recovered, for a while, via git reflog.

Upvotes: 2

joran
joran

Reputation: 2883

You may also achive this with

git checkout master
git rebase development
git reabse -i <SHA of initial commit> master

Now you can reorder, select and edit the commits you want to keep. Do not forget to push your reorginized branches and inform your coworker to pull the "new" branches so they do not push any old commits back to your repository.

Upvotes: 0

bcoder
bcoder

Reputation: 39

I would do the following:

git checkout master
git branch master_backup # Save the older master repo locally just in case
git reset --hard C2 # This sets the branch to C0-C1-C2
git cherry-pick C5..C7 # This sets the branch to C0-C1-C2-C5-C6-C7

You may have to resolve conflicts in the last command. This won't generate any merge commits but will change some of your your SHA1's:

git mergetool

Then, replace the remote master branch:

git push origin :master
git push origin master:master

Upvotes: 0

Related Questions