git 常用的几剂后悔药
在 git add 之前放弃对文件的所有修改就算你用 rm 删除了也无所谓照样还原回来。如果是没有用 git 进行跟踪的文件可不要轻易这么尝试哟git checkout --当你对一个文件进行了修改还没有 add 进暂存区你想要放弃所有的修改将文件恢复到没有修改之前的样子这时候可以使用 git checkout -- 这条命令会将文件恢复到上一次 commit 的时候这个文件的样子。[rootmaster GitTest]# vim hello.txt [rootmaster GitTest]# git status On branch master Your branch is up to date with origin/master. Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: hello.txt no changes added to commit (use git add and/or git commit -a) [rootmaster GitTest]# git checkout -- hello.txt [rootmaster GitTest]# git status On branch master Your branch is up to date with origin/master. nothing to commit, working tree cleangit restore(推荐使用)貌似新版本的 git restore 和 git checkout -- 差不多以后可能用 git restore 更多一些了毕竟见文知意而且命令还短。[rootmaster GitTest]# vim hello.txt [rootmaster GitTest]# git status On branch master Your branch is up to date with origin/master. Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: hello.txt no changes added to commit (use git add and/or git commit -a) [rootmaster GitTest]# git restore hello.txt [rootmaster GitTest]# git status On branch master Your branch is up to date with origin/master. nothing to commit, working tree clean将 git add 的文件变成 git add 之前的状态git restore --staged file[lookingmaster GitTest]$ vim b.txt [lookingmaster GitTest]$ git add b.txt [lookingmaster GitTest]$ git status On branch master Your branch is up to date with origin/master. Changes to be committed: (use git restore --staged file... to unstage) modified: b.txt [lookingmaster GitTest]$ git restore --staged b.txt [lookingmaster GitTest]$ git status On branch master Your branch is up to date with origin/master. Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: b.txt no changes added to commit (use git add and/or git commit -a)git reset HEAD fileAdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ vi myGit.txt # 添加新行 hello world AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git add myGit.txt AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git status On branch master Your branch is ahead of origin/master by 2 commits. (use git push to publish your local commits) Changes to be committed: (use git reset HEAD file... to unstage) modified: myGit.txt AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git reset HEAD myGit.txt Unstaged changes after reset: M myGit.txt AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git status On branch master Your branch is ahead of origin/master by 2 commits. (use git push to publish your local commits) Changes not staged for commit: (use git add file... to update what will be committed) (use git checkout -- file... to discard changes in working directory) modified: myGit.txt no changes added to commit (use git add and/or git commit -a)将最近 commit 中的某个文件重新修改提交git restore -s HEAD~1 file执行以后git commit 的 log 信息并没有丢失对 file 的修改会 rollback 回滚你可以重新修改文件然后 git add git commit 对文件的修改重新进行提交。适用场景最近的 commit 修改了多个文件但是想对其中一个文件的提交撤销并重新修改[lookingmaster GitTest]$ git status On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) nothing to commit, working tree clean [lookingmaster GitTest]$ git restore -s HEAD~1 hello.txt [lookingmaster GitTest]$ git status On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: hello.txt no changes added to commit (use git add and/or git commit -a)重新填写 git commit 信息git commit --amend有时候我们发现提交信息填写错了或者在提交完成之后才发现有几个文件没有提交这时候可以使用 git commit --amend 尝试重新进行提交。$ git commit --amend - mv test.txt to test directory mv test.txt to test dir # Please enter the commit message for your changes. Lines starting # with # will be ignored, and an empty message aborts the commit. # # Date: Wed Jul 29 22:28:31 2020 0800 # # On branch master # Your branch is ahead of origin/master by 2 commits. # (use git push to publish your local commits) # # Changes to be committed: # renamed: test.txt - test/test.txt # $ git commit --amend [master db41223] mv test.txt to test dir Date: Wed Jul 29 22:28:31 2020 0800 1 file changed, 1 insertion() rename test.txt test/test.txt (71%)不想让 git 继续跟踪某个文件git rm --cached如果你只是想从暂存区删除文件但是工作区的文件保持不变将文件保存在磁盘也就是说将文件保存在磁盘但是不想让 Git 进行跟踪使用如下命令即可 git rm --cached AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ vi test.txt # 新行添加 hello world git AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git status On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) Changes not staged for commit: (use git add file... to update what will be committed) (use git checkout -- file... to discard changes in working directory) modified: test.txt no changes added to commit (use git add and/or git commit -a) AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git rm --cached test.txt rm test.txt AdministratorPC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master) $ git status # 显示文件 test.txt 为 Untracked files On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) Changes to be committed: (use git reset HEAD file... to unstage) deleted: test.txt Untracked files: (use git add file... to include in what will be committed) test.txt重新初始化仓库目录rm -rf .git; git init有时候仓库由于经历过很多次的 commit 提交特别是大文件的提交有些文件虽然已经不使用且用 git 删除了但是记录仍然存留在 .git 当中导致仓库目录空间占用较大。这个时候就可以重新初始化一下仓库将以前的提交记录全部清除。这个要稍微简单一些了毕竟 git 能进行版本控制最主要的就是因为这个隐藏的 .git 目录了删了就可以了请谨慎操作再使用 git init 初始化即可。[rootmaster GitTest]# git status On branch master Your branch is up to date with origin/master. nothing to commit, working tree clean [rootmaster GitTest]# rm -rf .git [rootmaster GitTest]# git status fatal: not a git repository (or any of the parent directories): .git撤销最近的 commit但是不撤销对应 commit 所做的文件修改git reset HEAD~1注使用此命令你原来提交的代码都在不会被撤销也即只会撤销 commit 记录不对文件内容进行任何修改。[rootmaster GitTest]# git status # On branch master nothing to commit, working directory clean [rootmaster GitTest]# vim b.txt # 删除了文件最后一行 # 下面 commit 失败因为修改的文件还没添加到暂存区 [rootmaster GitTest]# git commit -m delete last line in b.txt # On branch master # Changes not staged for commit: # (use git add file... to update what will be committed) # (use git checkout -- file... to discard changes in working directory) # # modified: b.txt # no changes added to commit (use git add and/or git commit -a) ------------------------------------------------------------ [rootmaster GitTest]# git add b.txt [rootmaster GitTest]# git commit -m delete last line in b.txt [master 131c2e9] delete last line in b.txt 1 file changed, 1 deletion(-) [rootmaster GitTest]# git log commit 131c2e9e676726168a87db678c17e8ec404b8c4e Author: looking lookingqq.com Date: Sat Aug 8 01:35:09 2020 0800 delete last line in b.txt commit f3b4d4abd65a59c8a03df6caccf28f33422b4614 Author: looking lookingqq.com Date: Tue Aug 4 21:42:27 2020 0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt ------------------------------------------------------------ # 注意啦放大招啦。 [rootmaster GitTest]# git reset HEAD~ Unstaged changes after reset: M b.txt # 这个时候回到了 add 和 commit 提交之前但是在文件 b.txt 修改之后的那个状态 [rootmaster GitTest]# git status # On branch master # Changes not staged for commit: # (use git add file... to update what will be committed) # (use git checkout -- file... to discard changes in working directory) # # modified: b.txt # no changes added to commit (use git add and/or git commit -a) ------------------------------------------------------------ # 因为 commit 被撤销了所以我们再重新提交一次 [rootmaster GitTest]# git add b.txt [rootmaster GitTest]# git commit -m delete last line in b.txt [master 33796e9] delete last line in b.txt 1 file changed, 1 deletion(-) [rootmaster GitTest]# git log commit 33796e9844af4b6e1ee23c2d98ec5be4c5fef3e6 Author: looking lookingqq.com Date: Sat Aug 8 01:44:27 2020 0800 delete last line in b.txt commit f3b4d4abd65a59c8a03df6caccf28f33422b4614 Author: looking lookingqq.com Date: Tue Aug 4 21:42:27 2020 0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt commit cd3e33625578c3aaf6f19b69c878559a1dcf552a Merge: 640fd3c 3fceb8a [rootmaster GitTest]#撤销最近的 commit同时撤销对应 commit 所作的文件修改git reset --hard HEAD^1注使用之后你最新的 commit 命令下修改的内容将完全被撤销最近一次的 commit 记录也没了。[rootmaster GitTest]# git add b.txt [rootmaster GitTest]# git commit -m modify file b.txt [master be7abc1] modify file b.txt 1 file changed, 2 deletions(-) [rootmaster GitTest]# git log commit be7abc12a1a94e9390bd6c7f4bc747f9bbe86ab9 Author: looking lookingqq.com Date: Sat Aug 8 02:01:32 2020 0800 modify file b.txt commit f3b4d4abd65a59c8a03df6caccf28f33422b4614 Author: looking lookingqq.com Date: Tue Aug 4 21:42:27 2020 0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt ------------------------------------------------------------ [rootmaster GitTest]# cat b.txt hahahahaha hohohohoho rebase test add one line in b.txt by user B add one line in b.txt by user B again ------------------------------------------------------------ [rootmaster GitTest]# git reset --hard HEAD^1 HEAD is now at f3b4d4a This is a combination of 2 commits. ------------------------------------------------------------ # 注意看git reset --hard HEAD^1 执行前后 b.txt 文件内容发生变化了哟 # git log 的最近一次 commit 没有了 ------------------------------------------------------------ [rootmaster GitTest]# cat b.txt hahahahaha hohohohoho hehehehehe rebase test add one line in b.txt by user B add one line in b.txt by user B again hello world ------------------------------------------------------------ [rootmaster GitTest]# git log commit f3b4d4abd65a59c8a03df6caccf28f33422b4614 Author: looking lookingqq.com Date: Tue Aug 4 21:42:27 2020 0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt回滚之前某个 commit 提交的内容git revert commit-idgit revert 是用于“反做”某一个版本以达到撤销该版本的修改的目的。git reset 的作用是修改HEAD的位置且那个版本之后提交的版本都会丢失。git revert 可以回滚某个 commit 对应的修改所以在这点上面 revert 和 reset 是不一样的。rootmaster ~/GitTest# vim hello.txt rootmaster ~/GitTest# git add hello.txt rootmaster ~/GitTest# git commit -m add hello in hello.txt rootmaster ~/GitTest# git show commit 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b (HEAD - master) Author: looking lookingqq.com Date: Mon Jun 6 17:11:10 2022 0800 add hello in hello.txt diff --git a/hello.txt b/hello.txt index 83585f1..25f7272 100644 --- a/hello.txt b/hello.txt -1,3 1,4 hello nice hello world hello world. I am Lookingrevert 是新建一个 commit 来回滚之前 commit-id 的操作可以看到git revert commit-id 执行的操作是 commit-id 操作的逆操作也就是 commit-id 做的动作它都会反向操作一遍。rootmaster ~/GitTest# git revert 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b [master 780b3e0] Revert add hello in hello.txt 1 file changed, 1 deletion(-) rootmaster ~/GitTest# git show commit 780b3e0044b10aa62a6459a44d4b657da76a501d (HEAD - master) Author: looking lookingqq.com Date: Mon Jun 6 17:26:12 2022 0800 Revert add hello in hello.txt This reverts commit 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b. diff --git a/hello.txt b/hello.txt index 25f7272..83585f1 100644 --- a/hello.txt b/hello.txt -1,4 1,3 -hello nice hello world hello world. I am Lookingreset 的话则 reset 对应 commit-id 之后的修改就丢失了这也是 reset 比 revert 更危险的原因之一。rootmaster ~/GitTest# git reset --hard 1912baa1b9457add393f48c41ffd35957aa8b569 HEAD is now at 1912baa deal merge conflict in hello.txt当然回滚也是可能会造成冲突的稍微注意一下就行。比如有人在你之前 commit-id 修改的地方又重新做了新的修改你 revert 这个 commit-id 的话肯定就不可避免要 conflict 了。如果不想处理冲突则需要按照修改顺序的逆序 revert 各个 commit-id 的修改。修改倒数二个 commit 相关内容git rebase -i HEAD~2 和 git rebase --continue。git 修改倒数二个 commit_TomorrowAndTuture的博客-CSDN博客git rebase -i HEAD~2git rebase -i HEAD~2 以后git 会自动给你切换到下面这个界面你将你需要修改的那个 commit 前边的 pick 修改为 edit。[rootmaster GitTest]# git rebase -i HEAD~2 edit 3d87922 nice to meet you pick df5f952 nice to meet you too # Rebase 3f20612..df5f952 onto 3f20612 (2 commands) # # Commands: # p, pick commit use commit # r, reword commit use commit, but edit the commit message # e, edit commit use commit, but stop for amending # s, squash commit use commit, but meld into previous commit # f, fixup commit like squash, but discard this commits log message # x, exec command run command (the rest of the line) using shell # b, break stop here (continue rebase later with git rebase --continue) # d, drop commit remove commit # l, label label label current HEAD with a name # t, reset label reset HEAD to a label # m, merge [-C commit | -c commit] label [# oneline] # . create a merge commit using the original merge commits # . message (or the oneline, if no original merge commit was # . specified). Use -c commit to reword the commit message.然后保存退出而且 git 也已经提醒你版本已经 stopped 在这个 commit 节点了你可以开始你的表演修改了[rootmaster GitTest]# git rebase -i HEAD~2 Stopped at 3d87922... nice to meet you You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue假如我想把原来插入的 nice to meet you 修改为 nice。用 vim 修改后用git commit --amend不会产生新的 commit 提交重新提交 commit。[rootmaster GitTest]# vim hello.txt [rootmaster GitTest]# git add hello.txt [rootmaster GitTest]# git commit --amend nice # Please enter the commit message for your changes. Lines starting # with # will be ignored, and an empty message aborts the commit. # # Date: Mon Aug 31 21:16:51 2020 0800 # # interactive rebase in progress; onto 3f20612 [detached HEAD bcbfd91] nice Date: Mon Aug 31 21:16:51 2020 0800 1 file changed, 1 insertion()git rebase --continue然后继续 git rebase --continue 就好了如果没有冲突直接 git rebase --continue 也是极好的。[rootmaster GitTest]# git add hello.txt [rootmaster GitTest]# git rebase --continue [detached HEAD 72017cd] nice to meet you too 1 file changed, 1 insertion() Successfully rebased and updated refs/heads/master.git rebase --abort这个是你在 git rebase 过程当中的后悔药如果你在 rebase 的任何过程中想退出 rebase 过程直接执行 git rebase --abort 就直接退出回到 rebase 之前的状态啦。将最近多个commit合并成一个以最近两个为例git rebase -i HEAD~2 下面列表中越靠下的 commit 离 HEAD 越近pick b6fdaf6 add nice in hello.txt squash 63bb8ef add hello world in world.txt # Rebase eced426..63bb8ef onto eced426 (2 commands) # # Commands: # p, pick commit use commit # r, reword commit use commit, but edit the commit message # e, edit commit use commit, but stop for amending # s, squash commit use commit, but meld into previous commit # f, fixup commit like squash, but discard this commits log message # x, exec command run command (the rest of the line) using shell # b, break stop here (continue rebase later with git rebase --continue) # d, drop commit remove commit # l, label label label current HEAD with a name # t, reset label reset HEAD to a label # m, merge [-C commit | -c commit] label [# oneline] # . create a merge commit using the original merge commits # . message (or the oneline, if no original merge commit was # . specified). Use -c commit to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted.使用 squash 将最近一个提交 squash 合并到上一个提交也就是倒数第二个 commit产生新的 commit然后:wq 保存。然后继续编辑提交详情信息保存退出即可。# This is a combination of 2 commits. # This is the 1st commit message: add nice in hello.txt # This is the commit message #2: add hello world in world.txtcommit bf00e63e180caa680584920c08f396818c2fcf02 (HEAD - master) Author: looking lookingqq.com Date: Tue Dec 22 09:51:15 2020 0800 add nice in hello.txt and add hello world in world.txt commit eced426098acd286cb45cb7191f83ddee9cc228c Author: Looking 385543752392863668users.noreply.github.com Date: Tue Dec 22 10:25:57 2020 0800 clear hello.txt将多个不连续commit合并成一个git rebase -i HEAD~6下面列表中越靠下的 commit 离 HEAD 越近这个和上面的操作类似还是使用 git rebase 命令rootmaster ~/GitTest# git rebase -i HEAD~6 pick b6fdaf6 add nice in hello.txt pick 63bb8ef add hello world in world.txt pick ba8b2b1 delete world in first line of hello.txt pick 5983ee6 modify b.txt pick 5eaae9c modify hello.txt pick 5c528bb modify c.txt比如说我想将倒数第2个第4个第6个 commit 和操作 hello.txt 相关的几个不连续 commit都合并到一个 commit就这么调整顺序和操作如下pick 63bb8ef add hello world in world.txt pick b68f98f modify b.txt pick b6fdaf6 add nice in hello.txt s ba8b2b1 delete world in first line of hello.txt s 2820a56 modify hello.txt pick 824c246 modify c.txt然后保存 commit 就好了这块强调一下针对有相互依赖关系的 commit 提交千万不要轻易改变 commit 的相对顺序否者可能会出现冲突——如果你不嫌麻烦要去处理冲突的忽略。比如我这块对 hello.txt 操作的三个 commit 相对顺序并没有发生变化虽然我其他 commit 和 这三个 commit 的相对顺序发生了变化但是其他 commit 和这三个关联不大不会出现冲突所以也就没太大影响rootmaster ~/GitTest# git log commit d6566c3000afe7a057d5b54946f828b3b9bcffa0 (HEAD - master) Author: looking lookingqq.com Date: Fri Feb 24 16:12:04 2023 0800 modify c.txt commit a9f43a7acc4830b50df48d4a0187154fe3f644c0 Author: looking lookingqq.com Date: Tue Dec 22 09:51:15 2020 0800 add nice in hello.txt delete world in first line of hello.txt modify hello.txt commit 6bc4f93f821b4946a725bf20c2fd6643e244a380 Author: looking lookingqq.com Date: Fri Feb 24 16:11:24 2023 0800 modify b.txt commit c18dc0314ab997f061350f89d7f983bf5687a27a Author: looking lookingqq.com Date: Tue Dec 22 09:54:39 2020 0800 add hello world in world.txt将代码临时切换回之前的某个稳定版本不乐意的话还可以切换回来git checkout 和 git checkout master。[rootmaster GitTest]# git checkout 3f2061298d921378da14f4003753f1f277e67243 Note: switching to 3f2061298d921378da14f4003753f1f277e67243. You are in detached HEAD state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c new-branch-name Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 3f20612 I am Looking [rootmaster GitTest]# git checkout master Previous HEAD position was 3f20612 I am Looking Switched to branch master Your branch is up to date with origin/master.将代码永久切换回之前的某个稳定版本对应分支的指针也一同切换过去了git reset --hard commitId谨慎使用如果想恢复到之前某个提交的版本且那个版本之后提交的版本我们都不要了就可以用这种方法这个和git checkout最大的不同在于它把你的master分支指针所引用的 commit-id ——正常情况下指向当前分支的最新提交点也切换过去了你要再想用git checkout master可就回不去origin/master了哟如果你还记得commitId的话还是可以切换回去的。master本地主分支。origin/master远端主分支。[rootmaster GitTest]# git reset --hard 3f2061298d921378da14f4003753f1f277e67243 HEAD is now at 3f20612 I am Looking [rootmaster GitTest]# git checkout master Already on master Your branch is behind origin/master by 12 commits, and can be fast-forwarded. (use git pull to update your local branch) [rootmaster GitTest]# git reset --hard 1912baa1b9457add393f48c41ffd35957aa8b569 HEAD is now at 1912baa deal merge conflict in hello.txt [rootmaster GitTest]# git checkout master Already on master Your branch is up to date with origin/master.删除远端分支git push origin --delete branch-name快速保存和恢复临时工作现场git stash 和git stash popgit stash 结合 git stash pop 适用于快速保存和和恢复临时工作现场比如你在 branch1 正在安安心心写代码突然老板让你赶紧去 branch2 去修一个紧急 bug可以使用 git stash 将当前未提交的工作快速暂存到 git 堆栈到 biranch2 修改 bug 以后再切换回 branch1再次 git stash pop 快速恢复现场还可以适用于快速对比程序修改前后的运行差异而且可以在现有修改的基础上继续修改。丢弃本地所有未提交的修改git stash 同时git stash clear针对本地所有修改了但是还没进行 git commit 提交的部分即使已经使用了 git add也仍然可以丢弃。[rootmaster GitTest]# git status On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) Changes to be committed: (use git restore --staged file... to unstage) deleted: b.txt modified: hello.txt Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: c.txt [rootmaster GitTest]# git stash Saved working directory and index state WIP on master: 2017dee add newline in c.txt [rootmaster GitTest]# git stash clear [rootmaster GitTest]# git status On branch master Your branch is ahead of origin/master by 1 commit. (use git push to publish your local commits) nothing to commit, working tree clean丢弃本地所有修改强制远端拉取覆盖本地git fetch --all ; git reset --hard origin/master如果你想以远程仓库的版本内容为准丢弃本地所有修改重置了暂存区的内容而且还修改重置了本地工作区的内容即使是已提交 commit 的部分也会重置但是又不想删除本地仓库以后再次 git clone那么就按下边这么做吧这里的origin/master可以认为是一个指针初次拉取仓库时他指向了远端 origin 仓库 master 分支的最新提交点只有当本地 commit 被 push 到远端仓库的时候指针的指向才会发生改变。git fetch --all; git reset --hard origin/mastercommit 44892cd7d4c8c36267f6af9f8568fb5369eaaddc (HEAD - master) Author: lukaiyi lukaiyizdns.cn Date: Fri Mar 10 15:42:49 2023 0800 test commit 1912baa1b9457add393f48c41ffd35957aa8b569 (origin/master, origin/HEAD) Merge: ba8b2b1 5f7be5d Author: looking lookingqq.com Date: Wed Dec 23 15:34:02 2020 0800 deal merge conflict in hello.txtrootlocalhost ~/GitTest (master)# git push origin master Username for https://gitee.com: 15249089066 Password for https://15249089066gitee.com: Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done. Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/L2392863668/GitTest.git 1912baa..44892cd master - master rootlocalhost ~/GitTest (master)# git log commit 44892cd7d4c8c36267f6af9f8568fb5369eaaddc (HEAD - master, origin/master, origin/HEAD) Author: lukaiyi lukaiyizdns.cn Date: Fri Mar 10 15:42:49 2023 0800 test commit 1912baa1b9457add393f48c41ffd35957aa8b569 Merge: ba8b2b1 5f7be5d Author: looking lookingqq.com Date: Wed Dec 23 15:34:02 2020 0800 deal merge conflict in hello.txtgit fetch --all ; git reset --hard HEAD如果你只想重置当前分支未提交的内容已提交的内容不被重置那就直接用 HEAD 就可以了像上面这样即可。丢弃远端所有修改强制推送覆盖远端git push -f repo branch这个好像没啥多说的只要你考虑好了就强制推送就好了啦。默认是git push -f origin master。删除本地仓库重新克隆终极解决方案rm -rf GitTestgit clone https://github.com/2392863668/GitTest如果你想以远程仓库的版本内容为准丢弃本地所有修改包括仓库里边 untracked file 文件的改动那就重新克隆一个吧。[rootmaster workspace]# rm -rf GitTest [rootmaster workspace]# git clone https://github.com/2392863668/GitTest Cloning into GitTest... remote: Enumerating objects: 26, done. remote: Counting objects: 100% (26/26), done. remote: Compressing objects: 100% (19/19), done. remote: Total 247 (delta 11), reused 15 (delta 5), pack-reused 221 Receiving objects: 100% (247/247), 1.27 MiB | 869.00 KiB/s, done. Resolving deltas: 100% (95/95), done.重要知识点总结“^”代表父提交,当一个提交有多个父提交时可以通过在”^”后面跟上一个数字表示第几个父提交”^”相当于”^1”.~n相当于连续的n个”^”.checkout只会移动HEAD指针reset会改变HEAD的引用值。HEAD、branch、origin/branch 都属于指针HEAD 一般指向当前位置最新提交点branch 一般指向对应分支的最新提交点、origin/branch 一般指向对应分支远端origin仓库的最新提交点本地的 commit 提交在 push 到远端之前origin/branch 的引用指向一般不会发生变化。git commit -am xxx 和 git add . ; git commit -m xxx 操作其实不等价-am 参数只会将已追踪的文件进行 add 和 commit而先 git add . 会将未追踪的文件也添加到暂存区

相关新闻

AI赋能研发效能跃迁(10倍提效实测报告):从需求评审到上线部署的全链路压缩方案

AI赋能研发效能跃迁(10倍提效实测报告):从需求评审到上线部署的全链路压缩方案

更多请点击: https://kaifayun.com 第一章:AI赋能研发效能跃迁(10倍提效实测报告):从需求评审到上线部署的全链路压缩方案 在某金融科技团队为期6周的实测中,引入AI驱动的研发协同平台后,端到…

2026/7/21 20:21:00阅读更多 →
Nmap扫描结果导入与分析:Pentestly内网资产发现实战指南

Nmap扫描结果导入与分析:Pentestly内网资产发现实战指南

Nmap扫描结果导入与分析:Pentestly内网资产发现实战指南 【免费下载链接】pentestly Python and Powershell internal penetration testing framework 项目地址: https://gitcode.com/gh_mirrors/pe/pentestly 在复杂的内网渗透测试环境中,快速准…

2026/7/21 20:21:00阅读更多 →
PLC培训现状与高效学习路径解析

PLC培训现状与高效学习路径解析

1. PLC教学现状与行业需求分析工业自动化领域对PLC技术人才的需求近年来呈现爆发式增长。根据2025年行业调研数据显示,自动化设备维护、PLC编程调试等岗位的人才缺口达到23.7万,而每年相关专业毕业生仅能补充约8万人。这种供需失衡导致大量非科班出身的从…

2026/7/21 20:21:00阅读更多 →
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解

力扣 LCR 091. 粉刷房子 —— 动态规划入门详解

引言 动态规划是算法面试中的"拦路虎",许多初学者不知从何下手。今天讲解的「力扣 91. 粉刷房子」正是 DP 入门的绝佳练习题。它不像背包问题需要纠结"容量"维度,而是用最朴素的二维 DP 表格,清晰展示了状态定义、初始化…

2026/7/21 23:57:15阅读更多 →
基于simulink的双向DC/AC接口变换器的系统效率

基于simulink的双向DC/AC接口变换器的系统效率

### 手把手教你学Simulink--直流微电网中双向DC/AC接口变换器的电压稳定控制 #### 摘要 随着能源转型的推进,直流微电网在分布式能源接入与供电可靠性提升方面发挥着日益重要的作用。双向DC/AC接口变换器作为直流微电网与交流电网能量交互的关键设备,其电压稳定控制对于保障…

2026/7/21 23:57:15阅读更多 →
KVM主题:大页内存HugePages配置实践

KVM主题:大页内存HugePages配置实践

KVM主题:大页内存HugePages配置实践 在虚拟化环境中,内存管理是影响性能的关键因素之一。KVM(Kernel-based Virtual Machine)作为Linux内核中的一个虚拟化模块,为虚拟机提供了高效的硬件虚拟化支持。为了进一步提升KVM…

2026/7/21 23:57:15阅读更多 →
【Python自动化】安全库存阈值不同?库管/小白1个脚本预警

【Python自动化】安全库存阈值不同?库管/小白1个脚本预警

为什么你需要这个脚本 管多品类库存有多痛苦? 在仓库、仓库管理系统中管理货品时,出现令库管烦恼的问题是:把所有货品的安全库存阈值(即剩下多少件就预警且提示该补货/进货)设为统一标准,导致销量快的货总…

2026/7/21 23:57:15阅读更多 →
TPC-H 成本不到一分钱:ClickHouse Cloud 对比 Snowflake、Databricks、BigQuery 和 Redshift

TPC-H 成本不到一分钱:ClickHouse Cloud 对比 Snowflake、Databricks、BigQuery 和 Redshift

本文字数:5249;估计阅读时间:14分钟作者:Tom Schreiber, Mark Needham, Alexander Gololobov, Andriy Yakovlev and Robert SchulzeTL;DR 在 TPC-H SF100 标准下,单个 59 核 ClickHouse Cloud 节点在实际运行时间方面&…

2026/7/21 23:57:15阅读更多 →
移动端开发核心技术与性能优化实践

移动端开发核心技术与性能优化实践

1. 移动端开发基础概念解析移动端开发与PC端开发存在显著差异,核心区别主要体现在以下几个方面:1.1 视口与像素基础移动端浏览器默认布局视口宽度为980px,这与PC端存在本质区别。理解以下几个核心概念至关重要:物理像素&#xff1…

2026/7/21 23:55:14阅读更多 →
Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/21 0:51:49阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/21 0:51:49阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/21 0:51:49阅读更多 →
Windows+macOS 通用 OpenClaw 部署流程,内置依赖一键启动智能桌面助手

Windows+macOS 通用 OpenClaw 部署流程,内置依赖一键启动智能桌面助手

📌教程适配:OpenClaw v2.7.9 | 兼容 Windows10/11、macOS 双系统 📖前言 当下各类本地 AI 工具层出不穷,多数产品仅能完成文字问答交互,很难直接操控电脑执行实际操作。OpenClaw,业内常称小龙虾 AI&#…

2026/7/21 0:01:46阅读更多 →
Codex 接入后 Bug 反增?复盘从个人演示到团队协作的“流程陷阱”

Codex 接入后 Bug 反增?复盘从个人演示到团队协作的“流程陷阱”

聊《一次Codex项目复盘,问题最后出在流程而不是模型》之前,先说一句实在的:别急着背概念,先看它在真实项目里到底解决什么问题。摘要先把这篇文章的目标说清楚:看完之后,你应该能判断这件事值不值得做&…

2026/7/21 0:01:46阅读更多 →
手把手搓一个五子棋游戏,零代码也能当“游戏开发者”

手把手搓一个五子棋游戏,零代码也能当“游戏开发者”

大家好,还是我。前几期带大家做了心情日记本和可视化大屏,后台有朋友留言:“能不能教点好玩的?我想做游戏,但一行代码都不会。”行,这期就安排。今天的目标:从零做一个五子棋游戏。 带AI对战、三…

2026/7/21 0:03:46阅读更多 →
YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

如果你在部署 YOLOv8 时,发现推理速度只有可怜的 1-2 FPS,而别人的演示视频却能跑到 30 FPS 以上,那么问题很可能不在模型本身,而在于你的整个处理链路。很多开发者拿到一个训练好的 YOLOv8 模型后,会直接使用官方示例…

2026/7/21 22:53:50阅读更多 →
Coze与Dify对比指南:低代码AI应用开发从入门到实战

Coze与Dify对比指南:低代码AI应用开发从入门到实战

1. 从零到一:为什么你需要了解 Coze 和 Dify?如果你对 AI 应用开发感兴趣,但一看到“大模型”、“智能体”、“工作流”这些词就头疼,觉得门槛太高,那这篇文章就是为你准备的。很多开发者,包括我自己&#…

2026/7/21 18:53:30阅读更多 →
AI生图工具怎么选?2026年6月版实测对比

AI生图工具怎么选?2026年6月版实测对比

做自媒体的朋友应该都有体会:配图一直是个让人头疼的问题。2026年,AI生图工具已经非常成熟了,但工具太多反而不知道怎么选。以下是截至2026年6月我对主流AI生图工具的实测对比。Midjourney V8.1:速度之王2026年6月11日&#xff0c…

2026/7/21 18:53:30阅读更多 →