简体   繁体   English

Git提交然后从master中拉入分支。 放松一切?

[英]Git commit and then pull in branch, from master. Loosing everything?

I am working on a completely different structure from the one on master branch, on a On Rails app in a local branch. 我正在开发一个与主分支上的一个完全不同的结构,在本地分支的On Rails应用程序上。 I am comiting everything and then pushing my changes to a remote branch as well. 我正在推荐所有内容,然后将我的更改推送到远程分支。 At some point in future, I would like to integrate my changes back into master, but I want to make it even first, so I won't be behind it, just ahead by n commits. 在将来的某个时刻,我想将我的更改集成到master中,但是我想先将它变成master,所以我不会支持它,只需要提交n。 If I am doing a 如果我在做

git pull origin master

into my branch and then push that, would I loose anything from my previous commits, or would those stay on top of the master branch ones? 进入我的分支,然后推动它,我会从我以前的提交中丢失任何东西,还是那些留在主分支的顶部? In theory I should be able to merge my branch, as I will only be ahead of master, but I am not entirely sure if that's how git is working 理论上我应该能够合并我的分支,因为我只会领先于大师,但我不完全确定git是否正常工作

you will not lose data when you do a PULL but you will get merge issues which you will need to fix. 当您执行PULL时,您不会丢失数据,但是您将遇到需要修复的合并问题。

While technically you can push without pulling (with --force flag), you should not do that, because it discards other people's work. 虽然从技术上讲你可以在没有拉动的情况下推动(使用--force标志),但你不应该这样做,因为它会丢弃其他人的工作。 First pull their changes, resolve conflicts and only then you can push. 首先拉动他们的变化,解决冲突,然后才能推动。

Piece of advice, try to make your units of work small, as in don't spend too much time away from master especially if master gets updated frequently. 建议,尝试使你的工作单位小,因为不要花太多时间远离主人,特别是如果主人经常更新。 The reason is that you will have a very difficult time merging. 原因是你将很难合并。

Never do significant work on the master branch. 永远不要在主分支上做大量的工作。 The only time you are really working on the master branch is when you are merging or if you are the lone programmer on a project (even then its questionable). 你真正在主分支上工作的唯一一次是当你合并或者你是一个项目的唯一程序员时(即使那时它是有问题的)。

When working on a feature or taking care of a bug you want to be working on a separate branch. 处理功能或处理您希望在单独分支上工作的错误时。

For example when I take an issue from the product tracker I create a topical branch based on the name of the issue: 例如,当我从产品跟踪器中提出问题时,我会根据问题的名称创建一个主题分支:

git checkout -b 136-fix-something-important

If the work on a topical branch takes a long time you keep up to date with the remote by pulling the changes into the master branch and rebasing your topical branch off master. 如果局部分支工作通过拉动改变成主分支,需要很长的时间,你跟上与远程基础重建的局部分支关老爷。

git status # make sure you have a clean slate otherwise stash or commit
git checkout master
git pull
git checkout 136-fix-something-important
git rebase master 

This would fast-forward 136-fix-something-important if there are no conflicts or you would have to resolve the conflicts. 如果没有冲突或者您必须解决冲突,这将快速转发136-fix-something-important However taking care of conflicts is better done early that later in a mega merge. 然而,在大型合并之后的早期,最好先处理冲突。

When you are done with the feature in your topical branch you either merge it (if you are a maintainer) or send a pull request (if you are a contributor). 当您完成主题分支中的功能时,您要么合并它(如果您是维护者),要么发送拉取请求(如果您是贡献者)。

So what do I do now? 那我现在该怎么办?

Move your work to a separate branch. 将您的工作移至单独的分支。 And reset your local master branch from origin/master: 并从origin / master重置本地主分支:

git branch mybranchname
git reset --hard origin/master
git checkout mybranchname

You can the rebase your work in mybranchname off master. 你可以通过mybranchname脱离主人的工作。

git rebase master

Other answers aren't wrong, but it would be cleanest to push to a remote branch, pull to local master from remote master, then locally merge master into your local feature branch. 其他答案没有错,但最简单的方法是推送到远程分支,从远程主服务器拉到本地主服务器,然后将主服务器本地合并到本地功能分支。

Then, your changes wouldn't be lost, merge conflicts could easily be resolved, and you won't rewrite any other potentially collaborative history with rebase. 然后,您的更改不会丢失,可以轻松解决合并冲突,并且您不会使用rebase重写任何其他潜在的协作历史记录。

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

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