mans
mans

Reputation: 18148

How to make a branch like another branch in git

I have two branches say master and dev.

Master is old and I want to put dev into master so effectively:

  1. Delete all files in master,
  2. Copy all files from dev into master
  3. so at that time, master and dev became the same, without merging dev into master.

I can do this in the following way:

  1. Checout dev branch.
  2. Make a copy of dev branch some where on my hard disk.
  3. delete .git from the copy of dev branch on my hard disk.
  4. checkout master branch
  5. Delete all files other than .git in working dir.
  6. Copy all files from dev into working dir (all files are the same as dev branch other than what is in .git directory, but it is in master brach now)
  7. Commit master branch.

But I am wondering what is the correct way to do this in git?

Upvotes: 0

Views: 300

Answers (1)

Anshul Goyal
Anshul Goyal

Reputation: 76837

This is pretty simple to solve - use branch rename to rename the existing master branch, and then checkout a new branch named master from the dev branch

git checkout dev
git branch -m "master" "master_old"   #rename existing master branch
git checkout -b master                #recreate master from dev

Additionally, push your new master branch upstream using

git push upstream -f master

If now you do a git branch, you will see

$) git branch
  dev
* master
  master_old

Upvotes: 1

Related Questions