英文:
How can restore deleted file and re-push into github?
问题
几天前,我删除了一个文件:
git log
检查日志信息并获取ID以进行恢复:
git checkout 424a74fba6fbf6ad53e59e7d579427b7acdfde 文件名
已在我的本地工作区中恢复了删除的文件。我在添加了一些新文件后执行了推送命令,并希望将它们全部拉取(使恢复的文件也在GitHub上)。
git add .
git commit -m '恢复删除的文件'
git push https://github.com/xxxx/xxxx.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxxx/xxxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
根据提示,我执行了git pull,然后再次推送,但是恢复的文件又丢失了。如何发出新的推送并保留恢复的文件?或者说如何将恢复的文件和其他新添加的文件一起推送到GitHub而不出现任何错误。
按照geeky01adarsh的建议操作:
git checkout 424a74fba6fbf6ad53e59e7d579427b7ab10f8c7 source/os/testing-file-deleted.rst
从26e7e5c更新了1个路径
git add .
git pull --rebase origin master
error: cannot pull with rebase: Your index contains uncommitted changes.
error: please commit or stash them.
git status
在分支master上
要提交的更改:
(使用“git restore --staged <file>...”取消暂存)
新文件: source/os/testing-file-deleted.rst
英文:
Several days before, i deleted a file :
git log
Check the log info and get the id to restore
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).
git add .
git commit -m 'restore deleted file'
git push https://github.com/xxxx/xxxx.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxxx/xxxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' 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:
git checkout 424a74fba6fbf6ad53e59e7d579427b7ab10f8c7 source/os/testing-file-deleted.rst
Updated 1 path from 26e7e5c
git add .
git pull --rebase origin master
error: cannot pull with rebase: Your index contains uncommitted changes.
error: please commit or stash them.
git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: source/os/testing-file-deleted.rst
答案1
得分: 1
你可以尝试使用以下命令将远程分支与本地分支进行变基:
git pull --rebase origin master
git push origin master
或者可以尝试强制推送:
git push -f origin master
英文:
You may try to rebase your remote base with the local base by the following command:
git pull --rebase origin master
git push origin master
or force pushing may help:
git push -f origin master
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论