Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create git branch from existing project

Tags:

git

branch

merge

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 [email protected]: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)

like image 551
Pavel Zorin Avatar asked Jan 23 '26 21:01

Pavel Zorin


1 Answers

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>
like image 176
NightOwl19 Avatar answered Jan 26 '26 11:01

NightOwl19