搞乱了Git分支,如何修复

搞乱了Git分支,如何修复,git,github,branching-and-merging,pull-request,Git,Github,Branching And Merging,Pull Request,我在使用git(github)时遇到了一些问题,因为我是新手,我的思维是在SVN中思考的,我想我只是没有得到这个。。。请帮忙 所以我在做一些branch_1的更改和提交, 然后执行git push origin分支_1(并对该分支发出pull请求) 然后再次执行一些更改和提交 然后我决定切换到其他功能,所以我做了 git签出-b分支机构2 我更改了一些文件,单独提交了该文件,然后: git推送源分支2 但当我试图对分支2发出拉请求时,它同时包含来自分支2的提交和来自分支1的一些提交 我的问题基本

我在使用git(github)时遇到了一些问题,因为我是新手,我的思维是在SVN中思考的,我想我只是没有得到这个。。。请帮忙

所以我在做一些branch_1的更改和提交, 然后执行git push origin分支_1(并对该分支发出pull请求) 然后再次执行一些更改和提交

然后我决定切换到其他功能,所以我做了 git签出-b分支机构2

我更改了一些文件,单独提交了该文件,然后: git推送源分支2 但当我试图对分支2发出拉请求时,它同时包含来自分支2的提交和来自分支1的一些提交

我的问题基本上是,我做错了什么(这通常是如何正确处理的?) 最重要的是,我现在如何解决这个问题?我需要让branch_2只提交我对它所做的最后更改中的一个,并用这一个提交为它执行一个pull请求

基本上,我需要的是在不同的分支上来回切换,相应地执行提交,为每个分支执行拉请求,只在适当的分支上完成工作


非常感谢

让我们用一个例子看看这里发生了什么。以下是我认为您所做的复制:

$ git init && touch README && git add README && git commit -m 'Initial commit'
Initialized empty Git repository in /home/peter/ex/.git/
[master (root-commit) 56c1728] Initial commit
 0 files changed
 create mode 100644 README
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit       
* 56c1728 (HEAD, master) Initial commit
$ git checkout -b branch1 
Switched to a new branch 'branch1'
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* 56c1728 (HEAD, master, branch1) Initial commit
git checkout-b
将在
HEAD
所在的任何位置创建分支。查看
branch1
现在如何指向与
HEAD
相同的提交

现在让我们做出一些承诺

$ touch A
$ git add A
$ git commit -m 'Add A'
[branch1 298c3f9] Add A
 0 files changed
 create mode 100644 A
$ touch B
$ git add B
$ git commit -m 'Add B'
[branch1 24ffff3] Add B
 0 files changed
 create mode 100644 B
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* 24ffff3 (HEAD, branch1) Add B
* 298c3f9 Add A
* 56c1728 (master) Initial commit
现在,如果我们在
头部创建一个分支,就会发生这种情况

$ git checkout -b branch2
Switched to a new branch 'branch2'
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* 24ffff3 (HEAD, branch2, branch1) Add B
* 298c3f9 Add A
* 56c1728 (master) Initial commit
这不是您打算做的,但您继续在
branch2
上工作

$ touch C
$ git add C
$ git commit -m 'Add C'
[branch2 2cdb51b] Add C
 0 files changed
 create mode 100644 C
$ touch D
$ git add D
$ git commit -m 'Add D'
[branch2 db7fa2b] Add D
 0 files changed
 create mode 100644 D
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* db7fa2b (HEAD, branch2) Add D
* 2cdb51b Add C
* 24ffff3 (branch1) Add B
* 298c3f9 Add A
* 56c1728 (master) Initial commit
因此,现在
branch2
master
提前了4次提交,但实际上您只希望
branch2
比master提前2次提交('Add C'和'Add D')。我们可以用git-rebase
解决这个问题

$ git rebase --onto master branch1 branch2
First, rewinding head to replay your work on top of it...
Applying: Add C
Applying: Add D
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* c8a299f (HEAD, branch2) Add D
* b9325dc Add C
| * 24ffff3 (branch1) Add B
| * 298c3f9 Add A
|/  
* 56c1728 (master) Initial commit
下次创建分支时,可以使用
git checkout-b
表单

$ git checkout -b branch3 master
Switched to a new branch 'branch3'
$ git log --decorate --graph --all --pretty=oneline --abbrev-commit
* c8a299f (branch2) Add D
* b9325dc Add C
| * 24ffff3 (branch1) Add B
| * 298c3f9 Add A
|/  
* 56c1728 (HEAD, master, branch3) Initial commit