[Go to site: main page, start]

Clone branch into another with git

How to completely replace all branch content with commits from another branch.

Let's say:

  • your project has an experimental branch for testing;
  • you have some fixes in branch fix-345;
  • you want to overwrite content and history from fix-345 to experimental branch;
  • you don't need to save any changes in experimental.

Then you can reset experimental branch with the following commands:

git checkout experimental git reset --hard fix-345 git push --force origin experimental

Steps description

git checkout — checkout to the branch to be overwritten.

git reset --hard — get all changes from source branch to the target.

git push --force origin — force push your local changes for target branch

stackoverflow.com