git 如何在不拉的情况下将更改推送到 github

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19303962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:07:42  来源:igfitidea点击:

How to push changes to github without pull

gitgithub

提问by Matrosov Alexander

I developed a project that was been developed by another developers. I sent my code to the previous developers to make changes in their code but they said that they have never worked with git, so they added changes to the project and it is now the actual version but not under version control.

我开发了一个由其他开发人员开发的项目。我把我的代码发给了以前的开发人员,让他们修改他们的代码,但他们说他们从来没有用过 git,所以他们对项目进行了更改,现在是实际版本,但不受版本控制。

So, I already have commits in github and I need to push this new version.

所以,我已经在 github 中提交了,我需要推送这个新版本。

What I did:

我做了什么:

  1. git init
  2. git remote add origin
  3. git add .
  4. git push origin master
  1. git init
  2. git remote add origin
  3. git add .
  4. git push origin master

But there is an problem: I can't push new changes before updating the local repo. If I update my repo it will return all the files that the previous developers have deleted.

但是有一个问题:在更新本地 repo 之前,我无法推送新的更改。如果我更新我的 repo,它将返回以前开发人员删除的所有文件。

How do I push the current version without pulling data from git?

如何在不从 git 中提取数据的情况下推送当前版本?

采纳答案by manojlds

It seems like the other people worked on your project outside version control i.e upon bare files and folder.

其他人似乎在版本控制之外在您的项目上工作,即在裸文件和文件夹上工作。

I would suggest that you clone the repo, copy (and hence replace) the cloned content with the new content, commit and push these changes.

我建议您克隆 repo,使用新内容复制(并因此替换)克隆的内容,提交并推送这些更改。

The current commands of git initetc you are using create completely new repos, which is not what you want.

git init您正在使用的etc当前命令创建全新的存储库,这不是您想要的。

回答by u1860929

You can do a forced push.

您可以进行强制推送。

git push -f origin branch_name

The forced push will erase all commit history of the remote repository's branch, and replace it to your branch.

强制推送将删除远程存储库分支的所有提交历史记录,并将其替换到您的分支。

Check the answers for doing forced pushes in "How do I properly force a Git push?". Forced push can have unintended consequences as mentioned in "Git: How to ignore fast forward and revert origin [branch] to earlier commit?", so check for the same.

在“如何正确强制 Git 推送?”中查看强制推送的答案。强制推送可能会产生意想不到的后果,如“ Git:如何忽略快进并将原点 [branch] 恢复到较早提交?”中所述,因此请检查是否相同。

The forced push will be an incorrect way to push stuff in your case, since you already have previous commits on GitHub, and this will erase the commit history for previous commits.

在您的情况下,强制推送将是一种不正确的推送内容的方式,因为您已经在 GitHub 上进行了先前的提交,这将删除先前提交的提交历史记录。

Hence, to preserve your commit history, you can do the following things:

因此,为了保留您的提交历史,您可以执行以下操作:

Remove all the files from the git repository, and then add the new files here, and then commit the updated files:

从git仓库中删除所有文件,然后在此处添加新文件,然后提交更新的文件:

git rm -rf .
cp -r path/to/updated/code/* .
git add .

Doing a git statusnow will tell you which files the other developers modified, and a git diffwill show what modifications are there.

执行git statusnow 将告诉您其他开发人员修改了哪些文件,并且 agit diff将显示那里进行了哪些修改。

If a file has remain unchanged, then git rmand git addwill nullify the effect of each other.

如果一个文件一直保持不变,那么git rmgit add将互相抵消效果。

The files which those developers deleted remain deleted since you ran the git rmfor them but no git add.

这些开发人员删除的文件仍然被删除,因为您git rm为他们运行了git add.

Once you are satisfied that these indeed are the changes, you can commit using

一旦您对这些确实是更改感到满意,您就可以使用

git commit -m "Merged new code"

Potential gotchas:

潜在的问题:

  1. Only the file mode has changed (755 <=> 644) - depending on what code the other developers sent you.
  2. You will lose your .gitignore file with git rm -rf .(and similarly .gitattributesand other such files). Reset HEAD for each such files using git reset HEAD .gitignorebefore the commit.
  3. Different line terminating characters (in case different development environments are being used), So check for them appropriately.
  1. 只有文件模式发生了变化 (755 <=> 644) - 取决于其他开发人员发送给您的代码。
  2. 您将丢失 .gitignore 文件git rm -rf .(以及类似的.gitattributes其他此类文件)。git reset HEAD .gitignore在提交之前为每个此类文件重置 HEAD 。
  3. 不同的行终止符(以防使用不同的开发环境),因此请适当检查它们。