3

I have a project not by git and want to merge it with other one under git. Wat will be steps to do this?

git init
git remote add origin git@bitbucket.org:projectname/reponame.git
git pull origin master

And then git wants me to commit anything to create local master branch.

git add something
git commit -m 'Initial'

But I want do this like a new branch, ok make new branch

git branch newbranch
git checkout newbranch

So what next? I want keep my master and merge it to newbranch. When i try pull again i got this:

git pull origin master
From bitbucket.org:projectname/reponame
 * branch            master     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
        .gitignore
        .htaccess
        ...
Please move or remove them before you can merge.
Aborting

How to tell git - ok do it! At the end I want to have two branches: master (as it was at origin) and merged newbranch (my existed code + origin/master)

kowsky
  • 12,647
  • 2
  • 28
  • 41
Pavel Zorin
  • 331
  • 6
  • 17

2 Answers2

1

As far as I understood, you are trying to merge your new branch with the master branch.In that case, you need to first checkout your master rather than pulling it first,

git checkout master
git pull origin master
git merge <your new branch name>
git push origin master

Also, what can be done is the latest changes done on the branch can be merged with master using git rebase option

git checkout <branch name>
git rebase master

What it will do is, will apply the changes done recently on the top of master. After this, you can checkout the master branch and do a quick merge.

git checkout master
git merge <branch name>
NightOwl19
  • 419
  • 5
  • 24
0

First, you have to commit all your code.

Once the code is committed checkout the branch which you want to keep the content of. Execute git merge and you have any conflicts checkout the required files which you want to keep.

For example:

# Checkout the required branch (local or remote)
git checkout master

# merge the branches
git pull origin master

# in case you have conflicts:
# If you want to keep the version of your master use the --ours flag
git checkout --ours <file>

# second option if to merge use the merge strategy 
git merge -s recursive -X ours <branch>
CodeWizard
  • 128,036
  • 21
  • 144
  • 167