简体   繁体   English

如何使用Git提交和推送更改

[英]How to commit and push changes using Git

I just want to clarify how committing on specific branches works. 我只想澄清具体分支的提交方式。

Let's say I am working on a branch called "Metro". 假设我正在开发一个名为“Metro”的分支。 I make some changes to a few files, but I am not ready to push these up to the remote repository. 我对一些文件进行了一些更改,但我还没准备好将它们推送到远程存储库。

A hotfix comes in that I need to fix asap. 一个修复程序,我需要尽快修复。 I need to switch over to a clean branch called "Master", but I cannot because I would overwrite the files I have changed. 我需要切换到一个名为“Master”的干净分支,但我不能,因为我会覆盖我已更改的文件。 I need to commit these before I can switch. 在切换之前我需要提交这些。

My question is, if I commit these changes on the "Metro" branch, then switch to the clean "Master" branch, will the changes made in "Metro" get pushed to the remote "Master" repo because I have committed them, even though I am pushing to another branch? 我的问题是,如果我在“Metro”分支上提交这些更改,然后切换到干净的“Master”分支,“Metro”中所做的更改将被推送到远程“Master”仓库,因为我已经提交了它们,甚至虽然我正在推动另一个分支?

To make it succinct, are commits isolated to branches, or do all commits get added when pushing to the remote repo? 为了使它简洁,是提交隔离到分支,还是在推送到远程仓库时添加所有提交?

Before you switch branches to master via git checkout master , you must first git commit -m 'some message' , otherwise, git will try to take your current branch's changes along with you to the master branch. 在通过git checkout master将分支切换到master之前,必须首先git commit -m 'some message' ,否则,git将尝试将您当前分支的更改与您一起转移到master分支。

will the changes made in "Metro" get pushed to the remote "Master" repo because I have committed them, even though I am pushing to another branch? 将“Metro”中的更改推送到远程“Master”仓库,因为我已经提交了它们,即使我正在推动另一个分支机构?

Absolutely not. 绝对不。 git will only merge committed changes from Metro to master if you tell it to do that. 如果你告诉它,git只会将提交的更改从Metro合并到master。 That's by design ( Note: please read about git remotes, because using remotes, you can actually set up your pushes to do that by "default", which would be a case where you need to be careful. ). 这是设计的( 注意:请阅读git遥控器,因为使用遥控器,你实际上可以通过“默认”设置你的推动,这将是你需要小心的情况。 )。

To make it succinct, are commits isolated to branches, or do all commits get added when pushing to the remote repo? 为了使它简洁,是提交隔离到分支,还是在推送到远程仓库时添加所有提交?

To make it succinct: yes to the first part :-). 简明扼要:第一部分是:-)。

Technically speaking, a branch in git is a chain of commits, along with a name. 从技术上讲,git中的一个分支是一个提交链,还有一个名称。 If you push a branch, you will push all the commits that belong to it (unless the remote repo already has them), but nothing else. 如果你推动一个分支,你将推送属于它的所有提交(除非远程仓库已经拥有它们),但没有别的。 So no, git will not somehow push all commits. 所以不,git不会以某种方式推送所有提交。

Note however that you must take care only to push the branch that you want to push - git push can be configured to push all your branches. 但请注意,您必须注意只推动要推送的分支 - 可以将git push配置为推送所有分支。 To avoid this, set the config var push.default: 要避免这种情况,请设置config var push.default:

git config push.default current

This makes sure that git push will only push the current branch (and not all branches, which used to be the default). 这确保了git push只会推送当前分支(而不是所有分支,这是以前的分支)。

To understand how branches work in Git, I think it's important to change ones mindset to always have the commit tree in mind. 为了理解分支在Git中的工作原理,我认为改变思维方式总是要记住提交树是很重要的。 You have a acyclic tree of commits where each commit has a number of parent commits and nothing else. 你有一个非循环的提交树,每个提交都有许多父提交而没有别的。 Branches are now just simple pointers to specific commits within that tree. 分支现在只是指向该树中特定提交的简单指针。 If you make a new commit to a branch, all that really happens is that you create a new commit object with the changes that has the previous version as its parent and your branch pointer moves to the new commit that is now added to the whole tree. 如果您对分支进行新的提交,那么所有真正发生的事情是您创建一个新的提交对象,其中包含以前版本作为其父级的更改,并且您的分支指针将移动到现在添加到整个树的新提交。

So, branches are completely independent from each others, and in the same way, they don't really mean much. 因此,分支完全独立于彼此,并且以同样的方式,它们并不真正意义重大。 They are just pointers. 它们只是一个指针。

In your case, when you switch from metro to master , you are (probably) operating on a different side of the tree. 在您的情况下,当您从metro切换到master ,您(可能)在树的另一侧操作。 All commits you now create will only move the current branch pointer—master—but will not affect any other branches. 您现在创建的所有提交只会移动当前分支指针 - 主 - 但不会影响任何其他分支。

And if you push a branch, all that happens is that you tell the remote the commit the branch points at, and then you give the remote all the objects it needs to complete the tree. 如果你推动一个分支,所有发生的事情就是你告诉远程分支指向的提交,然后你给远程提供完成树所需的所有对象。

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

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