如何从现有项目中创建git分支。

3

我有一个非git的项目,想要合并到其他一个git项目中。我应该怎样做呢?

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

然后 git 要求我提交任何内容以创建本地主分支。

git add something
git commit -m 'Initial'

但我希望像创建一个新的分支一样完成这个任务,好的,创建新的分支。

git branch newbranch
git checkout newbranch

接下来该怎么办呢?我想保留我的主分支并将其合并到新的分支中。 当我再次尝试拉取时,出现了以下问题:

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

如何告诉 Git 执行合并操作?
最终,我想要拥有两个分支:master 分支(与原始版本一致)和合并了新代码 newbranch 的分支(包含我已有的代码和 origin/master 的内容)。
2个回答

1
据我理解,您想将新分支合并到主分支中。在这种情况下,您需要先检出您的主分支,而不是首先拉取它。
git checkout master
git pull origin master
git merge <your new branch name>
git push origin master

另外,可以使用git rebase选项将分支上的最新更改合并到主分支中。

git checkout <branch name>
git rebase master

它的作用是将最近所做的更改应用于主分支的顶部。之后,您可以检出主分支并进行快速合并。
git checkout master
git merge <branch name>

0

首先,您必须提交所有代码。

一旦代码被提交,请检出您想要保留内容的分支。执行git merge,如果有任何冲突,请检出您想要保留的所需文件。

例如:

# 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接