5

I am working on project alone (though some other people may use this repo) and for some time, I was making all my commits into master branch (commits are pushed to GitHub).

But at this point, I decided that master branch became too cluttered with small commits and, to make things look prettier, I want to move all my commit history into wip branch, and then only merge with master on new version.

The end result should be a wip branch with contents and commit history identical to the current master branch, and a master branch with a single commit with contents identical to the current wip branch.

What is the safest way to do that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Agr
  • 83
  • 1
  • 4
  • What do you want `master` to look like when you "merge with `master` on a new version"? Will it be identical to `wip` with all the small commits? Or do you intend to have a single squashed commit for that version? – M. Justin Jun 27 '21 at 07:21
  • The second, i want `master` to have single commit identical to latest `wip` stage at this moment. – Agr Jun 27 '21 at 07:44
  • I updated your question to add your desired end result, based on this comment. (If I'm interpreting it wrong, obviously please feel free to fix it). – M. Justin Jun 27 '21 at 08:03

2 Answers2

3

You can start by creating wip from your current master history:

git switch -c wip master

From there, you can work on wip branch, then merge on master when ready.

I also want to clear history on master

You can then reset --hard your master to an older commit, or all the way back to the first commit, and force push it (assuming you have notified other users of the repository).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for answer. I also want to clear history on master. – Agr Jun 27 '21 at 06:27
  • @Agr OK, I have edited my answer accordingly. – VonC Jun 27 '21 at 06:30
  • Are there any possible side-effects for others, if i do hard reset and then merge from my wip branch? – Agr Jun 27 '21 at 06:42
  • 1
    @Agr See this answer to [What are the practical consequences of rewriting Git history?](https://stackoverflow.com/a/1491022/6868543) – j6t Jun 27 '21 at 06:46
  • 1
    @j6t Yes: other needs to fetch and reset their own master branch to yours. They also need to isolate their work in their own branch. – VonC Jun 27 '21 at 07:08
1

One approach would be to rename the current master branch to wip, then create a new empty master branch. Groups of commits could then be copied over to the new master branch from wip, combined into single commits in the new master.

# Rename the master branch to "wip" locally
git branch -m master wip

# Rename the remote master branch to "wip"
git push origin -d master
git push origin -u wip

# Check out a new, empty branch as master
git checkout --orphan master
git reset --hard

# Create a squashed commit matching the current state of wip
git checkout wip -- .
git add .
git commit

# Push the new master
git push origin -u master

Detailed explanation

Use the branch move option to rename the branch locally:

git branch -m master wip

Delete the remote branch using the push delete option, then add a new remote branch matching the local wip branch using the push upstream option:

git push origin -d master
git push origin -u wip

Create a new empty branch with no commit history. This is done by creating an orphan branch (one without any commits) using the checkout orphan option. The existing files will remain (uncommitted), so delete them using the reset hard option:

git checkout --orphan master
git reset --hard

Copy the current state of the wip branch to the master branch. First, copy all files from the wip branch by using the checkout with pathspec option with the current working directory pathspec (.) to recursively include all files in the current working directory. These files are all added to the index using the add with pathspec option with the current working directory pathspec. Finally, a single commit with all these files is created using the commit option:

git checkout wip -- .
git add .
git commit

Finally, the new master branch is pushed to the origin using the push upstream option:

git push origin -u master
M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • I thought about orphan branch but decided to not recommend that approach. That way, wip and master keep a common history (from at least the first commit) – VonC Jun 27 '21 at 07:10
  • @VonC True, but the asker appears to not want a common history, but instead have different, larger-grained commits. There are definitely benefits to the common history approach, though. – M. Justin Jun 27 '21 at 07:13
  • Though I could be misinterpreting. I'll ask. – M. Justin Jun 27 '21 at 07:19