【发布时间】:2022-01-02 05:00:02
【问题描述】:
将相同的文件重新提交到远程分支
我正在从事我大学的一个项目。
我已经将项目的损坏文件(degrees.py,small.csv ...等)提交到remote branch 1,这是**GitHub repository的受保护分支(无法使用强制推送),现在我想再次将更新后的相同项目文件(degrees.py、small.csv ... 等)重新提交到分支 1。
我无法将更新后的相同文件重新提交到我的分支 1。
我正在使用git fetech 和git checkout 命令,我正在尝试这样做
git init
git remote add origing <origin>
git fetch origin
#create branch branch1 locally switch to branch1 and copy from origin/branch1
git checkout -b branch1 origin/branch1
git add --all
git commit -m "comments"
git push
即:
dir
__pycache__ degrees.py large small util.py
Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees
$ git init
Initialized empty Git repository in C:/Users/Sbariz/Desktop/CS50ai_search/s_degrees/.git/
Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
git remote add origin https://github.com/me50/shjee1063.git
Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
git fetch origin**
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 29 (delta 1), reused 26 (delta 1), pack-reused 0
Unpacking objects: 100% (29/29), 21.60 MiB | 1.56 MiB/s, done.
From https://github.com/me50/shjee1063
* [new branch] ai50/projects/2020/x/degrees -> origin/ai50/projects/2020/x/degrees
* [new branch] ai50/projects/2020/x/tictactoe -> origin/ai50/projects/2020/x/tictactoe
* [new branch] main -> origin/main
Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
$ git branch -a
remotes/origin/ai50/projects/2020/x/degrees
remotes/origin/ai50/projects/2020/x/tictactoe
remotes/origin/main
Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
$ git checkout -b ai50/projects/2020/x/degrees origin/ai50/projects/2020/x/degrees**
error: The following untracked working tree files would be overwritten by checkout:
__pycache__/degrees.cpython-37.pyc
__pycache__/util.cpython-37.pyc
degrees.py
large/movies.csv
large/people.csv
large/stars.csv
small/movies.csv
small/people.csv
small/stars.csv
util.py
Please move or remove them before you switch branches.
Aborting
【问题讨论】:
-
这里显示的主要问题是某些提交特别确实包含其他提交特别(并且正确地)省略的文件。为了检查错误提交,您需要将未跟踪的工作树文件移开,以便检查/切换到错误提交可以放入不需要的文件,之后您可以删除他们再次放回好的(想要的)文件。注意我在这里一直提到commits。
-
之所以先谈commits,后谈files,是因为Git存储和推送,commits,而不是文件。然后提交包含文件:每个提交都有一个每个文件的完整快照。您的存储库中的某些提交包含太多文件,例如
*.pyc文件,而其他正确不包含这些文件。 -
实际上不可能更改任何现有的提交。因此,那些确实有文件(但不应该)的提交是损坏的提交,并且将永远是损坏的提交,因为它们无法修复。你必须允许他们继续被破坏的提交。小心使用它们,知道它们已经坏了!
-
我可以将新的提交(包含相同的文件)发送到同一个分支,意思是 branch1。
-
你总是可以进行 new 提交。每个新提交都有自己的所有文件的新快照(加上自己的元数据)。任何 new 提交都可以在您喜欢的任何分支上。在某些情况下,您还可以将提交 off 踢出某个分支,尽管这有点棘手。
标签: git file github branch protected