GIT:无法将更改暂存提交,直到我提交更改之前也无法拉取

3

我使用git status命令得到以下结果:

git status
On branch master
Your branch and 'origin/master' have diverged,
and have 3 and 1 different commits each, respectively.
    (use "git pull" to merge the remote branch into yours)
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:   application/models/Usuario_model.php
    modified:   application/views/Autor/listar.php
    modified:   application/views/Libro/modificar.php
    modified:   application/views/Libro/perfil.php
    modified:   application/views/ListaLibros/listar.php
    modified:   application/views/Valoracion/crearOk.php

no changes added to commit (use "git add" and/or "git commit -a")

git add <file> 命令无效

git pull 命令输出如下:

git pull
: Your local changes to the following files would be overwritten by merge:
    application/models/Usuario_model.php
    application/views/Autor/listar.php
    application/views/Libro/perfil.php
    application/views/ListaLibros/listar.php
    application/views/Valoracion/crearOk.php
commit your changes or stash them before you merge.
Aborting

我无法推送,因为“当前分支的末端落后于其远程对应分支”。
因此,在拉取之前我不能推送,也不能在提交之前执行拉取,并且由于我无法暂存更改,所以无法提交。
发生了什么情况,我该如何解决它?

在执行 git commit -m "Your Message" 命令之前,您可以尝试使用 git add * 命令。 - Vinit Divekar
当你运行 git add application 然后运行 git status 会发生什么?这两个命令的输出是什么? - Code-Apprentice
@Code-Apprentice 在执行 git add application 后没有任何反应,而且 git status 显示的内容和之前一样。 - Juan Antonio Rodríguez Gabriel
这非常奇怪。检查你的 .gitignore 文件,看看是否有任何规则匹配这些文件。确保你在正确的目录下。最后,如果所有其他方法都失败了,你可以使用我回答中描述的 git stash 命令来拉取更改。不幸的是,你仍然需要解决提交更改的问题。 - Code-Apprentice
3个回答

2
如果这些文件是第一次提交,您可以尝试使用 git add *git add .。接下来,您可以通过 git commit -m "您的消息" 来提交更改。(如果文件以前已经提交过,您可以将上述两个命令合并为一个命令,将它们作为暂存区,即 git commit -a -m "您的消息")。稍后,您可以使用 git pull <远程名称> <分支名称> 进行拉取。这应该可以正常工作。

1
在这种情况下,听起来你想要提交(commit)代码,但由于某些原因无法提交。如果你可以放弃提交(commit)直到以后,你可以运行git stash将更改暂时提交到"stash"中。这也会清除你的本地副本。在执行git pull并解决任何合并冲突后,你可以使用git stash apply将暂存的更改应用到你的本地副本,或者使用git stash pop从stash中删除更改。

0

您需要在 git add 中指定要添加的内容。可以使用 git add --all 或指定具体文件,例如 git add application/models/Usuario_model.php(支持通配符)。

或者您可以使用 git commit -a 同时进行暂存和提交。

请参阅 git addgit commit


我尝试过使用 "git add -A" 和指定文件的方式,例如 "git add application/models/Usuario_model.php",但都没有成功。后来我尝试了 "git add --all",它奏效了!这不和 "git add -A" 是一样的吗? - Juan Antonio Rodríguez Gabriel
@JuanAntonioRodríguezGabriel 我认为它们是相同的。你在执行 'git add -A' 时遇到了什么错误? - arghtype
完全没有错误。按下回车键后,控制台只显示了一个空提示符。 - Juan Antonio Rodríguez Gabriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接