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 performing another checkout.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
1 pick 4d2b6ce 合并提交测试一号 2 pick f526876 合并提交测试2号 3 pick 3fa2e1a 合并提交测试3号,测试修改最后一次提交。在测试一下。 4 5 # Rebase 9e791f4..3fa2e1a onto 9e791f3 (3 commands) 6 # 7 # Commands: 8 # p, pick = use commit 9 # r, reword = use commit, but edit the commit message 10 # e, edit = use commit, but stop for amending 11 # s, squash = use commit, but meld into previous commit 12 # f, fixup = like "squash", but discard this commit's log message 13 # x, exec = run command (the rest of the line) using shell 14 # d, drop = remove commit 15 # 16 # These lines can be re-ordered; they are executed from top to bottom. 17 # 18 # If you remove a line here THAT COMMIT WILL BE LOST. 19 # 20 # However, if you remove everything, the rebase will be aborted. 21 # 22 # Note that empty commits are commented out
1 # This is a combination of 3 commits. 2 # This is the 1st commit message: 3 4 合并提交测试一号 5 6 # This is the commit message #2: 7 8 合并提交测试2号 9 10 # This is the commit message #3: 11 12 合并提交测试3号,测试修改最后一次提交。在测试一下。 13 14 # Please enter the commit message for your changes. Lines starting 15 # with '#' will be ignored, and an empty message aborts the commit. 16 # 17 # Date: Thu Nov 2 11:25:59 2017 +0800 18 # 19 # interactive rebase in progress; onto 9e791f3 20 # Last commands done (3 commands done): 21 # squash f526876 合并提交测试2号 22 # squash 3fa2e1a 合并提交测试3号,测试修改最后一次提交。在测试一下。 23 # No commands remaining. 24 # You are currently rebasing branch 'dev' on '9e791f3'. 25 # 26 # Changes to be committed: 27 # modified: 1.txt 28 # new file: 5.txt 29 # new file: 6.txt
编辑好合并的提交信息后保存,可看见如下输出信息:
1 2 3 4 5 6
[detached HEAD 83d65c7] 三次合并提交的测试,成功了。 Date: Thu Nov 2 11:25:59 2017 +0800 3 files changed, 7 insertions(+) create mode 100644 5.txt create mode 100644 6.txt Successfully rebased and updated refs/heads/dev.
最后把合并的记录强推到服务器中去:
1 2 3 4 5 6 7 8
git push -f Counting objects: 5, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 596 bytes | 596.00 KiB/s, done. Total 5 (delta 1), reused 0 (delta 0) To http://192.168.102.9/yulilong/test.git + 3fa2e1a...83d65c7 dev -> dev (forced update)
$ git filter-branch -f --tree-filter 'rm -f 1.txt' HEAD Cannot create a new backup. A previous backup already exists in refs/original/ Force overwriting the backup with -f
$ git reset --hard refs/original/refs/heads/dev HEAD is now at 83d65c7 三次合并提交的测试,成功了。
把删除后的项目提交到服务器
注意:一旦运行此命令,删除的文件不能找回
1 2 3 4 5 6 7 8 9
$ git push origin +dev
Counting objects: 14, done. Delta compression using up to 8 threads. Compressing objects: 100% (9/9), done. Writing objects: 100% (14/14), 1.40 KiB | 1.40 MiB/s, done. Total 14 (delta 1), reused 0 (delta 0) To http://192.168.102.110/user/test.git + 83d65c7...4b4da80 dev -> dev (forced update)
注意,分支名字前面的 + 号一定不能忘记,否则会报如下错误:
1 2 3 4 5 6 7
To http://192.168.102.110/user/test.git ! [rejected] dev -> dev (non-fast-forward) error: failed to push some refs to 'http://192.168.102.110/user/test.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.
推送之前要把服务器的分支保护去掉,否则会报如下错误:
1 2 3 4
remote: GitLab: You are not allowed to force push code to a protected branch on this project. To http://192.168.102.110/user/test.git ! [remote rejected] dev -> dev (pre-receive hook declined) error: failed to push some refs to 'http://192.168.102.110/user/test.git'