Article directory
Situation 1: Not yet pushed or unable to push
Situation description: This situation often occurs when commits are submitted locally many times, but they have not been pushed.
Usually two conditions are met:
- There is an error commit that cannot be pushed/unnecessary among the commits that have not been pushed, and this commit needs to be deleted at this time.
- Need to keep the current code.
As shown below.
Solution:
First, check the commit id by git log
thinking about which commit state you need to switch to:
# 查看git记录(能看到每次操作的id):
git log --oneline
The second step is to restore the state of the warehouse to the last commit state of the state that can be submitted normally/the state that needs to be deleted (the --soft
parameters will ensure that the contents of your folder will remain unchanged after restoration):
git reset --soft <无法上传的那次的上一个的id>
Note: It is almost enough to enter more than 6 digits for the id, and you can also enter a few more digits, you don’t need to select all
The third step is to check the current status of Git and try to push:
# 可以看到完整的修改记录:
git status
# 尝试push
git push
If you can't understand the text description, you can also watch the video description. To be more specific, this video helped me solve this problem satisfactorily when I was a novice: "Git life from scratch" Lesson 8 Regret medicine in Git.bilibili .Full stack port .
Case 2: already pushed
Note: It is best not to use
git revert
undo submission, it is a bit difficult to use.
Situation description: This situation often occurs when a commit has been pushed, but a bug is suddenly discovered. In order to ensure the purity of the commit, I don't want to push another commit, so I plan to overwrite the last pushed commit.
It may also be the case that A wrote a version a long, long time ago. After B took over, he decided to refactor. After C took over, he felt that B was not as good as A, so he decided to continue to develop on the basis of A and discard all commits of B.
Solution:
Consistent with the first case, just return to the required commit state. It's just that you need to use force push when you push at the end:
git push -f
Case 3: want to delete all commit records
Reference: Git | Clear all commit records
Situation description: Deleting the library and running away/obliterating the development process/necessary to protect privacy.
method one:
1.删除.git
git init
git remote add origin [email protected]:user/repo
2.重新添加所有文件:
git add .
3.提交:
git commit -m 'message'
4.强制更新:
git push -f origin master
After completing these operations, you can use git log --oneline
to view the operation log.
Method 2:
1.新建一个分支:
# 注意orphan代表这个分支是一个初始提交,
# 意思是前面的记录都不存在,
# 而git checkout -b则会保留之前的commit记录
git checkout --orphan <分支名>
2.将无敏感信息的新内容正常提交到新分支里:
git add .
git commit -m "new"
git push --set-upstream origin <分支名>
3.删除原来的分支:
git branch -D master
4.将<分支名>改成master:
git branch -m master
5.强制推送:
git push -f origin master --set-upstream