Asked  6 Months ago    Answers:  5   Viewed   58 times

I have two branches in my Git repository:

  1. master
  2. seotweaks (created originally from master)

I created seotweaks with the intention of quickly merging it back into master. However, that was three months ago and the code in this branch is 13 versions ahead of master.

It has effectively become our working master branch as all the code in master is more or less obsolete now.

Very bad practice I know, lesson learned.

Do you know how I can replace all of the contents of the master branch with those in seotweaks?

I could just delete everything in master and merge, but this does not feel like best practice.

 Answers

40

You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

The result should be your master is now essentially seotweaks.

(-s ours is short for --strategy=ours)

From the docs about the 'ours' strategy:

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

Update from comments: If you get fatal: refusing to merge unrelated histories, then change the second line to this: git merge --allow-unrelated-histories -s ours master

Tuesday, June 1, 2021
 
Sauleil
answered 6 Months ago
69

How do we merge the master branch into the feature branch? Easy:

git checkout feature1
git merge master

There is no point in forcing a fast forward merge here, as it cannot be done. You committed both into the feature branch and the master branch. Fast forward is impossible now.

Have a look at GitFlow. It is a branching model for git that can be followed, and you unconsciously already did. It also is an extension to Git which adds some commands for the new workflow steps that do things automatically which you would otherwise need to do manually.

So what did you do right in your workflow? You have two branches to work with, your feature1 branch is basically the "develop" branch in the GitFlow model.

You created a hotfix branch from master and merged it back. And now you are stuck.

The GitFlow model asks you to merge the hotfix also to the development branch, which is "feature1" in your case.

So the real answer would be:

git checkout feature1
git merge --no-ff hotfix1

This adds all the changes that were made inside the hotfix to the feature branch, but only those changes. They might conflict with other development changes in the branch, but they will not conflict with the master branch should you merge the feature branch back to master eventually.

Be very careful with rebasing. Only rebase if the changes you did stayed local to your repository, e.g. you did not push any branches to some other repository. Rebasing is a great tool for you to arrange your local commits into a useful order before pushing it out into the world, but rebasing afterwards will mess up things for the git beginners like you.

Thursday, June 3, 2021
 
Akarun
answered 6 Months ago
46

You really don't need this complex approach.

Simply add your own repo as a submodule (of itself!), submodule following the gh-pages branch (since a submodule can follow the latest commit of a branch)!

git checkout master
git rm -r gh-pages # save your data first
git submodule add -b gh-pages -- /remote/url/of/your/own/repo
git commit -m "ADd gh-pages branch as submodule"
git push

That way, you can change files either in your main branch, or in the gh-pages folder (which is actually a submodule)

Whenever you are making changes in gh-pages folder, don't forget to commit there, and in the main folder of your repo in order to record the new gitlink (special entry in the index) reprsenting the new SHA1 of the gh-pages submodule.

cd myrepo/gh-pages
# modify files
git add .
git commit -m "modify gh-pages files"
cd .. # back to myrepo folder
git add gh-pages
git commit -m "record gh-pages new SHA1"
git push

With git 2.7+, you can set:

cd /path/to/main/repo
git config push.recurseSubmodules on-demand

Then a single git push from your main repo will also push the gh-pages submodule to the gh-pages branch.

Later, a single git clone would clone both your main repo and its gh-pages branch (in the gh-pages submodule folder).

That way, you always have both content visible.

And you don't need a complex "synchronization/copy" mechanism.


Update August 2016: Simpler GitHub Pages publishing now allows to keep your page files in a subfolder of the same branch (no more gh-pages needed):

Now you can select a source in your repository settings and GitHub Pages will look for your content there.

You don't need the submodule approach anymore, since your pagers can be in a subfolder within the same branch.

Friday, July 23, 2021
 
jonboy
answered 5 Months ago
22

You should checkout the other branch:

git checkout origin 2-0-stable

Git will automatically detect that you don't have a local branch with that name, but you have a remote branch with that name. It will create a local branch 2-0-stable which will be the same as origin/2-0-stable and will have tracking configured.

If you want to be in this state from the beginning you should git clone with the --branch option.

git clone --branch 2-0-stable <git url>

This will clone the repo, but will directly check out the specified branch.

Saturday, October 23, 2021
 
truemp
answered 1 Month ago
35

I would make use of git log with some colours to do this:

git log --graph --full-history --all --color  
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"

This will colour each branch and the merges. It will also label the head of each branch.

You can add relative dates and committer names with this:

git log --graph --full-history --all --color 
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s 
%Cgreen(%cr) %C(bold blue)<%an>%Creset'"

For more info see: http://git-scm.com/book/en/Git-Basics-Viewing-the-Commit-History

Thursday, November 4, 2021
 
smiggle
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share