How can restore deleted file and re-push into github?

huangapple go评论114阅读模式
英文:

How can restore deleted file and re-push into github?

问题

几天前,我删除了一个文件:

  1. git log

检查日志信息并获取ID以进行恢复:

  1. git checkout 424a74fba6fbf6ad53e59e7d579427b7acdfde 文件名

已在我的本地工作区中恢复了删除的文件。我在添加了一些新文件后执行了推送命令,并希望将它们全部拉取(使恢复的文件也在GitHub上)。

  1. git add .
  2. git commit -m '恢复删除的文件'
  3. git push https://github.com/xxxx/xxxx.git
  4. ! [rejected] master -> master (non-fast-forward)
  5. error: failed to push some refs to 'https://github.com/xxxx/xxxx.git'
  6. hint: Updates were rejected because the tip of your current branch is behind
  7. hint: its remote counterpart. Integrate the remote changes (e.g.
  8. hint: 'git pull ...') before pushing again.
  9. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

根据提示,我执行了git pull,然后再次推送,但是恢复的文件又丢失了。如何发出新的推送并保留恢复的文件?或者说如何将恢复的文件和其他新添加的文件一起推送到GitHub而不出现任何错误。

按照geeky01adarsh的建议操作:

  1. git checkout 424a74fba6fbf6ad53e59e7d579427b7ab10f8c7 source/os/testing-file-deleted.rst
  2. 26e7e5c更新了1个路径
  3. git add .
  4. git pull --rebase origin master
  5. error: cannot pull with rebase: Your index contains uncommitted changes.
  6. error: please commit or stash them.
  7. git status
  8. 在分支master
  9. 要提交的更改:
  10. (使用“git restore --staged <file>...”取消暂存)
  11. 新文件: source/os/testing-file-deleted.rst
英文:

Several days before, i deleted a file :

  1. git log

Check the log info and get the id to restore

  1. git checkout 424a74fba6fbf6ad53e59e7d579427b7acdfde file_name

The deleted file restored in my local workspace. I issue command to push after adding some new files, , and want to pull them all(make the restored file in the github also).

  1. git add .
  2. git commit -m &#39;restore deleted file&#39;
  3. git push https://github.com/xxxx/xxxx.git
  4. ! [rejected] master -&gt; master (non-fast-forward)
  5. error: failed to push some refs to &#39;https://github.com/xxxx/xxxx.git&#39;
  6. hint: Updates were rejected because the tip of your current branch is behind
  7. hint: its remote counterpart. Integrate the remote changes (e.g.
  8. hint: &#39;git pull ...&#39;) before pushing again.
  9. hint: See the &#39;Note about fast-forwards&#39; in &#39;git push --help&#39; for details.

I git pull according to the notes, then push, but the restored file lost again. How can issue a new push and keep the restored file? Or say the restored file and other new added file push into github together without any error.

Do as geeky01adarsh's suggestion:

  1. git checkout 424a74fba6fbf6ad53e59e7d579427b7ab10f8c7 source/os/testing-file-deleted.rst
  2. Updated 1 path from 26e7e5c
  3. git add .
  4. git pull --rebase origin master
  5. error: cannot pull with rebase: Your index contains uncommitted changes.
  6. error: please commit or stash them.
  7. git status
  8. On branch master
  9. Changes to be committed:
  10. (use &quot;git restore --staged &lt;file&gt;...&quot; to unstage)
  11. new file: source/os/testing-file-deleted.rst

答案1

得分: 1

你可以尝试使用以下命令将远程分支与本地分支进行变基:

  1. git pull --rebase origin master
  2. git push origin master

或者可以尝试强制推送:

  1. git push -f origin master
英文:

You may try to rebase your remote base with the local base by the following command:

  1. git pull --rebase origin master
  2. git push origin master

or force pushing may help:

  1. git push -f origin master

huangapple
  • 本文由 发表于 2023年7月27日 15:41:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定