git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
# 将该文件添加到暂存区
git add test.txt
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: test.txt
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: test.txt
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: test.txt
rm test.txt
git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
git rm test.txt --cached
rm 'test.txt'
git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: test.txt
Untracked files:
(use "git add ..." to include in what will be committed)
test.txt
3.4.3 恢复被删除的文件/文件夹
有些时候,可能误删除了某些文件,只有.git目录没被删除,被误删的文件是可以找回来的。
rm test1.txt
git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
deleted: test1.txt
mv test.txt test1.txt
git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
deleted: test.txt
Untracked files:
(use "git add ..." to include in what will be committed)
test1.txt
git add --all
git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
renamed: test.txt -> test1.txt
3.5.2 git修改文件名
git mv test.txt test1.txt
git mv test.txt test1.txt
git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
renamed: test.txt -> test1.txt
git add -p test1.txt
# 输出内容
diff --git a/test1.txt b/test1.txt
index 9801343..5791be4 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,2 +1,3 @@
Hello World
+success2
No newline at end of file
(1/1) Stage this hunk [y,n,q,a,d,e,?]?
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,2 +1,3 @@
Hello World
+success2
No newline at end of file
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
# If the patch applies cleanly, the edited hunk will immediately be marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again. If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.
git cat-file -p aeb827f8087c395d0c38362328af8d41f1cee0c3
# 输出内容
tree efbd263bc3503e5ebc193a8051ee264b461f89bf
author StoNE <767864968@qq.com> 1676096772 +0800
committer StoNE <767864968@qq.com> 1676096772 +0800
inti commit
git checkout master
# 输出内容
error: Your local changes to the following files would be overwritten by checkout:
test2.html
Please commit your changes or stash them before you switch branches.
Aborting
git stash push -m 'test stash'
# 输出内容
Saved working directory and index state On test2: test stash
# 查看stash列表
git stash list
#输出内容
stash@{0}: On test2: test stash
git checkout master
Switched to branch 'master'
# 此时删除dev分支,会提示dev分支还没有完全合并到master,我们仍然将其删除
git branch -d dev
error: The branch 'dev' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev'
# 强行删除dev分支
git branch -D dev
Deleted branch dev (was accd781)
git merge test
Auto-merging inex.html
CONFLICT (content): Merge conflict in inex.html
Automatic merge failed; fix conflicts and then commit the result
4.查看git状态
git status
On branch dev
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: test.html
new file: test2.html
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: inex.html
git add inex.html
git status
On branch dev
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: inex.html
new file: test.html
new file: test2.html
pick de98713 test2
pick 777cde5 test stash
# Rebase aeb827f..777cde5 onto aeb827f (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup [-C | -c] = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop = remove commit
# l, label
pick de98713 test2
pick 777cde5 test stash
# Rebase aeb827f..777cde5 onto aeb827f (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup [-C | -c] = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop = remove commit
# l, label = label current HEAD with a name
# t, reset = reset HEAD to a label
# m, merge [-C | -c ] [# ]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# This is a combination of 2 commits.
# This is the 1st commit message:
test2
# This is the commit message #2:
test stash
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Feb 11 16:31:51 2023 +0800
#
# interactive rebase in progress; onto aeb827f
# Last commands done (2 commands done):
# pick de98713 test2
# squash 777cde5 test stash
# No commands remaining.
# You are currently rebasing branch 'test2' on 'aeb827f'.
#
# Changes to be committed:
这里我们将commit信息,修改为一个就好。编辑并保存后,Git会继续完成后续的rebase操作。
5.3 将一个commit拆解为多个commit
有的时候,我们在一个commit中提交了过多的文件,我们同样可以使用git rebase -i来进行拆解。
git rebase -i aeb827f
会弹出vim编辑窗口:
pick de98713 test2
pick 777cde5 test stash
pick 3ac54d7 commit two files
# Rebase aeb827f..3ac54d7 onto aeb827f (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup [-C | -c] = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop = remove commit
# l, label = label current HEAD with a name
# t, reset = reset HEAD to a label
# m, merge [-C | -c ] [# ]
# create a merge commit using the original merge commit's
git rebase -i aeb827f
Stopped at 3ac54d7... commit two files
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
先查看一下git log
git log --oneline
3ac54d7 (HEAD, test2) commit two files
777cde5 test stash
de98713 test2
aeb827f (tag: v1.0) inti commit
git status
interactive rebase in progress; onto aeb827f
Last commands done (3 commands done):
pick 777cde5 test stash
edit 3ac54d7 commit two files
(see more in file .git/rebase-merge/done)
No commands remaining.
You are currently editing a commit while rebasing branch 'test2' on 'aeb827f'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
Untracked files:
(use "git add ..." to include in what will be committed)
one.html
two.html
nothing added to commit but untracked files present (use "git add" to track)