简体   繁体   English

搞砸了Git分支,如何修复

[英]Messed up Git branching, how to fix

I am having some trouble with git (github), because I am new to this, my mind is thinking in SVN, and I guess I am just not getting this... please help. 我在使用git(github)时遇到了一些麻烦,因为我对此很陌生,我的想法是在SVN中思考,我想我只是没有得到这个......请帮忙。

So I was working on some branch_1 doing some changes and commits, then doing git push origin branch_1 (and making a pull request on that branch) then again doing some changes and commits. 所以我正在做一些branch_1进行一些更改和提交,然后执行git push origin branch_1(并在该分支上发出pull请求)然后再做一些更改和提交。

then I decided to switch to other feature, so I did git checkout -b branch_2 然后我决定切换到其他功能,所以我做了git checkout -b branch_2

I changed some file, did commit of that alone file, then: git push origin branch_2 But when I tried to make a pull request on branch_2 it was containing commits both from branch_2 and some commits from branch_1 我更改了一些文件,提交了那个单独的文件,然后:git push origin branch_2但是当我尝试在branch_2上发出pull请求时,它包含来自branch_2的提交和来自branch_1的一些提交

My question is basically, what did I do wrong ( how is this usually correctly handled? ) And most importantly how do I fix this now? 我的问题基本上是,我做错了什么(通常如何正确处理?)最重要的是我现在如何解决这个问题? I need to have branch_2 with just one commit of last changes I did on it, and to do a pull request for it with that one commit. 我需要让branch_2只提交一次我在其上做的最后一次更改,并使用那一次提交对它进行拉取请求。

Basically what I need is to work on different branches switching back and forth, doing commits accordingly and doing pull requests for each branch with work done only on appropriate branch. 基本上我需要的是在不同的分支上来回切换,相应地进行提交并对每个分支执行pull请求,只在适当的分支上完成工作。

Thanks a lot! 非常感谢!

So let's see what happened here with an example. 让我们看一下这里发生的事情。 The following is a replication of what I think you did: 以下是我认为你做的复制:

$ 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 <new_branch> will create a branch wherever HEAD is. git checkout -b <new_branch>将在HEAD所在的位置创建分支。 See how branch1 is now pointing to the same commit that HEAD was. 了解branch1现在如何指向HEAD所在的同一提交。

Now let's make some commits. 现在让我们做一些提交。

$ 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

So now, if we create a branch at HEAD , this is what happens. 所以现在,如果我们在HEAD创建一个分支,就会发生这种情况。

$ 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

That's not what you intended to do, but you continued to work on branch2 . 这不是你打算做的,但你继续在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

So now branch2 is ahead of master by 4 commits, but you really only want branch2 to be 2 commits ahead of master ('Add C' and 'Add D'). 所以现在branch2在4次提交之前领先于master ,但是你真的只希望branch2在master之前提交2次('Add C'和'Add D')。 We can fix that with git rebase . 我们可以用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

Next time you create a branch, you can use the git checkout -b <new_branch> <start_point> form. 下次创建分支时,可以使用git checkout -b <new_branch> <start_point>表单。

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM