在我最近对已推送的存储库的提交中,我注意到我在文件中拼错了一个单词,现在想更改它。但是,我不想创建一个全新的提交。有没有办法修改我最新的提交并将其推送到我的存储库中,而无需重置、删除和推送?
过去我是这样做的:
git reset HEAD~1 --soft
git push -f
# correct the spelling
git add .
git commit -m "bug10 fix"
Run Code Online (Sandbox Code Playgroud)
有没有办法纠正文件的拼写,修改我最新的提交并推送,而不必重置,强制删除,然后重新提交?
我试过:
# fixed the file
git add .
git commit --amend --no-edit
git push
Run Code Online (Sandbox Code Playgroud)
! [rejected] Nov21 -> Nov21 (non-fast-forward)
Run Code Online (Sandbox Code Playgroud) 我在独立的HEAD状态下看过GIT commit --amend.这个问题要求答案比需要的要复杂得多.我想了解git commit --amend在正常HEAD情况下的工作原理.
注:类似的问题,因为这一个,但也有一些重要的变化.
在给定提交ID的情况下,我有以下函数来重写提交的日期:
rewrite-commit-date () {
local commit="$1"
local newdate="$2"
newdate="$(date -R --date "$newdate")"
echo ">>>> Rewriting commit $commit date: $newdate"
git filter-branch --env-filter \
"if test \$GIT_COMMIT = '$commit'
then
export GIT_AUTHOR_DATE
export GIT_COMMITTER_DATE
GIT_AUTHOR_DATE='$newdate'
GIT_COMMITTER_DATE='$newdate'
fi" &&
rm -fr "$(git rev-parse --git-dir)/refs/original/"
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现类似的功能rewrite-commit-message来更改提交消息.我想要的是:
rewrite-commit-message接受两个参数:和commit_id,和new_commit_messagecommit_id知道哪个提交更改git commit --amend,因为这与旧提交有关(不一定与最近的提交有关)git push -ffilter-branch它,但我不知道如何:
test在所使用的rewrite-commit-date功能是用来在env-filter,但我不会做env-filter在这里,因为我不希望改变有关提交环境中任何事物,但提交信息.如果我的第一次提交错误,但推送到(当前是私有的)远程,我如何撤消远程提交?
我猜我可以修改然后推--force?
我知道修改旧GIT提交的正确方法是使用rebase --interactive,但为了清楚这些概念,我想了解当我做什么时会发生什么
git checkout <commit>git commit . --amend当我这样做时,它不是修改提交,而是从同一提交的PARENT分支新的提交.
这只是GIT的方式告诉我,我不能修改已经有子提交的提交吗?
修改和壁球命令有什么区别?我试过这两个,发现两者都在做同样的管理.
这是发生的事情:
git commit --amend并保留与HEAD相同的提交消息git push现在我明白了
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)
我想明白:
当我写作时,git commit --amend我会得到某种编辑器,我可以在其中更改此提交的名称.如何使用键盘确认并保存我的更改?
我已经在远程分支上推送了一个提交,现在我想改变它的内容,所以我尝试了git修改.如果执行git status,则表示两个分支分别具有1个和1个不同的提交.现在,如果我使用相同的提交消息推送修改后的提交将添加新提交,或者它将更改我推送的最后一次提交?
git ×10
git-amend ×10
git-commit ×4
commit ×2
git-push ×1
git-squash ×1
push ×1
rebase ×1
squash ×1
terminal ×1