git - 在我的第一次提交中,我无法推送更改,因为我的 github 中已经有 .gitignore 和 README.md,与本地不同

标签 git github git-commit git-merge-conflict

我对 git 很陌生,正在自学。

我在 github 中创建了一个存储库,其中包含 .gitignore 和 README.md 这两个文件。

现在我本地的文件夹中有一个 Angular 项目。

我想将我的项目推送到新创建的github存储库, 所以我按顺序使用了以下命令--

之后我收到以下错误---

To https://github.com/Arpan619Banerjee/test-git-commands.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/Arpan619Banerjee/test-git-commands.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我知道我的 github 存储库中存在的 .gitignore 和 README.md 文件与我在 local 中的文件不同,所以我必须先提取更改,然后 merge 它,然后我可以推送它。 但我尝试这样做—— 我按顺序执行了以下命令,从一开始就开始,因为我不知道在收到错误后如何继续,所以我想在推送之前先 pull 出 dmerge 更改。

然后我收到以下消息。我不知道之后如何继续-

error: The following untracked working tree files would be overwritten by merge:
        .gitignore
        README.md
Please move or remove them before you merge.
Aborting

我在 Google 中搜索得到了这个答案--

The problem is that you are not tracking the files 
locally but identical files are tracked remotely so
 in order to "pull" your system would be forced to 
overwrite the local files which are not version controlled.

Try running

git add * 
git stash
git pull

但是这里本地文件被覆盖,我不希望这样。我希望 push 我的本地更改。如何解决此处的 merge 冲突以保留我的本地更改。

通过 Vs code git GUI 我能够 merge 我的本地更改,但我想通过 cmd 来完成。 请帮助我。

最佳答案

如果您确定不想将文件保留在远程存储库上,您可以使用git push -f

-f 表示强制,它将覆盖您的远程存储库(因此请小心使用)

选项二是使用 git reset HEAD^ 取消提交(它将重置您的提交,但不会重置您修改的文件,就像您在 git commit 之前一样) ,然后使用 git stash stash 您的更改。您现在可以git pull,然后使用git stash pop取消存储,处理冲突,然后提交和推送。

最后,最简单的方法是不要在 Github 上创建这些文件并完全按照您所做的操作,这样就不会有任何冲突。

关于git - 在我的第一次提交中,我无法推送更改,因为我的 github 中已经有 .gitignore 和 README.md,与本地不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61223842/

相关文章:

git - 使用 IntelliJ 修改 git 提交信息

github - 如何从 Gitlab 的提交表中删除任何提交?

git - .gitignore 与 .gitattributes

git - 我们如何在 git 中 merge 两个存储而不提交任何存储?

git - 为什么 git 不进一步减小存储库的大小?

github - 通过评论过滤GitHub拉取请求

git - 致力于掌握分离的 HEAD

git - 如何在多次推送后重新设置 git 历史记录,然后推送更新的历史记录?

git - ansible:git 模块挂起

git - "git status"是否实际检查上游 repo 是否与我的 repo 匹配?