Agr
Agr

Reputation: 83

How can I move all commits to another branch?

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?

Upvotes: 5

Views: 1186

Answers (2)

M. Justin
M. Justin

Reputation: 21074

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

Upvotes: 1

VonC
VonC

Reputation: 1323343

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).

Upvotes: 3

Related Questions