git登台并在多个分支之间进行提交 [英] git staging and committing between multiple branches

查看:119
本文介绍了git登台并在多个分支之间进行提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然完全不理解git。这是我得到的:

pre $ g $ git分支(输出,我在主)
git checkout - b foo
回显下一行>>文件(文件是现有文件)
git添加文件(阶段)
git checkout master
git status(显示该文件具有下一行并且正在上演!!)
git commit(提交在分支foo上执行的更改)
git checkout foo

这是踢球者。 foo现在不会显示任何对工作目录OR中的文件所做的更改。



看起来像 - 任何更改(包括修改文件和分段)都会发生所有分支。当你提交到一个特定的分支时,除了你提交的分支之外的其他所有分支都会抛弃这些更改。



这实际上是怎么回事?有人能让这对我有意义吗?这听起来像是完全扭曲的行为,显然我没有得到让这是明智的事情的设计理念。



编辑显式示例:

  $ mkdir元素
$ cd元素
$ git init
在/ home / dan /元素/ .git /
$ echoone>>一个
$ git添加一个
$ git commit -m msg
[master(root-commit)36dc8b0] msg
1个文件已更改,1个插入(+),0个缺失( - )
创建模式100644 one
$ git checkout -b fire
切换到新分支'fire'
$ echonext line>>一个
$ git结帐大师
M一个
转换到分支主
$猫一个
一个
下一行
$
$ / code>

这与git pro书籍明显矛盾:

这是一个需要记住的重要事项:Git重置您的工作目录,使其看起来像您签出的分支所指向的提交快照。它会自动添加,删除和修改文件,以确保您的工作副本是分支在您上次提交时的样子。

解决方案

当您添加文件时,只有当您 提交文件时,才会使用哪个分支。所以如果你这样做的话:

$ g $ g $ g $ g $ g $ g $ g $ b $ git checkout master $ b $ git commit

您已将文件提交给主分支。



这是一个完整的例子,带有输出。我们从一个新的仓库开始:

$ $ $ $ g $ init
在/ home / lars / tmp / so / repo / .git /

此时,我们在 master 分支,我们还没有添加任何文件。让我们添加一个文件:

  $ date> file1 
$ cat file1
5月11日星期五13:05:59 EDT 2012
$ git add file1
$ git commit -m'添加了一个文件'
[master (root-commit)b0764b9]添加一个文件
1个文件改变,1个插入(+),0个删除( - )
创建模式100644 file1

太棒了,我们现在有一个提交的分支( master )。让我们来创建一个新的分支:

$ $ $ $ $ git checkout -b foo
转换到一个新分支'foo'
$ git branch
* foo
master
$ ls
file1

现在我们将添加一行到 file1

  $ date>> file1 
$ git status
#在分支上foo
#没有为commit提交更改:
#(使用git add< file> ...更新将会提交)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#修改:file1

没有更改添加到提交(使用git add和/或git commit -a)

这表明该文件已被修改,但尚未暂存。让我们对文件进行处理并提交:

  $ git add file1 
$ git commit -m'进行了更改'
[foo 761bed9]更改了
1个文件,1个插入(+),0个删除( - )

并重新运行 git status

 在分支foo 
没有提交(工作目录干净)

此时,文件如下所示:

  5月11日13:05:59 EDT 2012 
5月11日13:07:36 EDT 2012

如果我们切换回 master 分支,我们将看到没有第二行的文件的早期版本:

 转换到分支'master'
$ cat file1
5月11日13:05:59 EDT 2012

对文件的更改会被分离到它们所在的分支。



在更新例如,这... ...

pre $ $ g code $ git checkout master

...不会产生错误,因为此时, master 和<$ c中的'one'版本$ c> fire 是相同的。工作目录中的更改同样适用于任一版本。


I clearly do not understand git at all. This is what I'm getting:

git branch  (outputs that I'm on master)
git checkout -b foo
echo "next line" >> file (file is an existing file)
git add file (stages)
git checkout master
git status (shows that file has "next line" and is staged!!)
git commit (commits the changes that were staged on branch foo!!)
git checkout foo

Here is the kicker. foo now doesn't show any changes made to file in the working directory OR staged.

So looks like - any changes you make, including modifying files and staging, happen to ALL branches. and when you COMMIT to a specific branch, those changes are discarded on all other branches except the one you committed on.

Is this actually what is going on? Can someone make this make sense to me? It sounds like completely screwy behavior and clearly I don't get the design idea that makes this a sensible thing to do.

Edit for explicit example:

$ mkdir element
$ cd element
$ git init
Initialized empty Git repository in /home/dan/element/.git/
$ echo "one" >> one
$ git add one
$ git commit -m msg
[master (root-commit) 36dc8b0] msg
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 one
$ git checkout -b fire
Switched to a new branch 'fire'
$ echo "next line" >> one
$ git checkout master
M       one
Switched to branch 'master'
$ cat one
one
next line
$

Which patently contradicts this from the git pro book:

This is an important point to remember: Git resets your working directory to look like the snapshot of the commit that the branch you check out points to. It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.

解决方案

It doesn't matter what branch you're on when you add a file, only when you commit it. So if you do this:

git add file
git checkout master
git commit

You have committed the file to the master branch.

Here's a complete example, with output. We start with a new repository:

$ git init
Initialized empty Git repository in /home/lars/tmp/so/repo/.git/

At this point, we're on the master branch and we haven't yet added any files. Let's add a file:

$ date > file1
$ cat file1
Fri May 11 13:05:59 EDT 2012
$ git add file1
$ git commit -m 'added a file'
[master (root-commit) b0764b9] added a file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file1

Great, we now have a branch (master) with one commit. Let's create the new branch:

$ git checkout -b foo
Switched to a new branch 'foo'
$ git branch
* foo
  master
$ ls
file1

Now we'll add a line to file1.

$ date >> file1
$ git status
# On branch foo
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   file1
#
no changes added to commit (use "git add" and/or "git commit -a")

This shows that the file has been modified, but not yet staged. Let's stage the file and commit it:

$ git add file1
$ git commit -m 'made a change'
[foo 761bed9] made a change
 1 files changed, 1 insertions(+), 0 deletions(-)

And re-run git status:

$ git status
# On branch foo
nothing to commit (working directory clean)

At this point, the file looks like this:

Fri May 11 13:05:59 EDT 2012
Fri May 11 13:07:36 EDT 2012

If we switch back to the master branch, we'll see the earlier version of the file without the second line:

$ git checkout master
Switched to branch 'master'
$ cat file1
Fri May 11 13:05:59 EDT 2012

Changes to a file are isolated to the branch on which they were committed.

In your updated example, this...

$ git checkout master

...does not generate an error because at this point, the version of 'one' in both master and fire is identical. The changes in the working directory would apply equally well to either version.

这篇关于git登台并在多个分支之间进行提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆