git commit -m <MESSAGE>
是一个简短的命令,你可以用git commit
打开编辑器(主要是vim),提供详细的提交描述。
检查提交记录:
git log
Author: qq449245884 44924566884@qq.com
Date: Sat Jul 17 14:57:24 2021 +0800
Add hello.txt
创建分支
在很多情况下,拥有一个独立的初始代码版本是很有用的:例如,在测试你不确定的功能时,或者在一起工作时避免代码冲突。这正是git分支的意义所在:它从历史上的一个特定点开始生长。
要创建分支,运行git branch NAME
,要切换分支,运行git checkout NAME
。或者简单地
git checkout -b dev # 切换到一个名为“dev”的新分支
Switched to a new branch ‘dev’
gitexample git:(dev)
我们在Hello.txt
文件中更改一些内容并提交更改:
echo “\nHello, Git Branch” >> hello.txt &&
git commit -am “Change hello.txt”
现在,切换到主分支:
git checkout main &&
cat hello.txt
Switched to branch ‘main’
Hello, Git
正如你所看到的,文件内容仍然和原来一样。为了比较分支,我们可以运行。
git diff dev
diff --git a/hello.txt b/hello.txt
index 360c923…b7aec52 100644
— a/hello.txt
+++ b/hello.txt
@@ -1,3 +1 @@
Hello, Git
-
-Hello, Git Branch
(END)
type “:q” to close
我们在主分支中进行更改:
echo “\nHi from Main Branch” >> hello.txt &&
git commit -am “Change hello.txt from main”
[main 9b60c4b] Change hello.txt from main
1 file changed, 2 insertions(+)
现在让我们试着把这些变化合并起来。
git merge dev
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
因为文件在同一个地方被修改了两次,我们就产生了冲突。看看这个文件
cat hello.txt
<<<<<<< HEAD
Hello, Git
Hi from Main Branch
=======
Hello, Git
dev
还有一个命令可以单独查看更改:
git diff --ours # :q to close
git diff --theirs #:q to close
你可以手动编辑文件并提交修改,但我们设想一下,我们只想要其中一个版本。我们就从中止合并开始。
git merge --abort
并以 "theirs"策略重新启动合并,这意味着在发生冲突时,我们将使用传入的分支所坚持的东西。
git merge -X theirs dev
Auto-merging hello.txt
Merge made by the ‘recursive’ strategy.
hello.txt | 5 ±—
1 file changed, 1 insertion(+), 4 deletions(-)
与此策略相反的是 “ours”。将这两个改动合并在一起,需要手动编辑(或使用git mergetool
)。
查看所有分支运行的列表
git branch # type :q to close
dev
* main
最后,删除分支运行:
git branch -d dev
Deleted branch dev (was 6259828).
重置分支
分支从 git 历史中的某一点开始 “生长(grow)”,rebase
允许改变这个点。我们再创建一个分支,并在hello.txt
上添加一些改动。
git checkout -b story &&
echo “Once upon a time there was a file”>>story.txt &&
git add story.txt &&
git commit -m “Add story.txt”
Switched to a new branch ‘story’
[story eb996b8] Add story.txt
1 file changed, 1 insertion(+)
create mode 100644 story.txt
现在,我们回到主分支并添加更改:
git checkout main &&
echo “Other changes” >> changes.txt &&
git add changes.txt &&
git commit -m “Add changes.txt”
重置我们在main
到story
分支所做的更改:
git checkout story &&
git rebase main
Successfully rebased and updated refs/heads/story.
可以看到在主分支创建的新文件被添加到story
分支。
ls
changes.txt hello.txt story.txt
注意:不要rebase
别人可能使用过的分支,例如主分支。另外,请记住,在远程版本库上进行的每一次历史操作都需要强制这些修改生效。
远程存储库
如果你还没有,请创建一个GitHub账户,登录并创建一个新的空仓库(私有或公共)。
假设版本库的名字是 “example
”,运行以下命令(改成你的用户名)。
git remote add origin git@github.com:USERNAME/example.git &&
git push -u origin main
你可以刷新页面,看到主分支的文件。要把所有本地分支推送到远程仓库,请运行。
git push --all origin
我们在GitHub上编辑一些东西:只要点击任何文件和铅笔图标。添加一行你想要的任何文字,然后按 “提交修改”。
在本地运行这个命令,以获得远程的变化。
git checkout main &&
git pull
管理未提交的更改
如果你想保存你的本地修改以便以后使用,你可以使用git stash
。
echo “Changes” >> hello.txt &&
git stash
现在你可以使用以下命令来检查、应用或放弃这些变化。
git stash list
stash@{0}: WIP on main: 92354c8 Update changes.txt
git stash pop # 应用更改
git stash drop # 撤销修改
你可以使用 stash 编号,即git stash pop 0
来应用一个特定的储藏库,或者git stash drop 0
来撤销。
如果你想放弃所有的本地修改,只需恢复版本库到最后提交的修改,请运行。
git restore .
管理提交的更改
一旦你创建了一个提交,这个变化就会保存在本地的git历史中。如前所述,所有影响远程历史的修改都需要git push --force
。以下所有命令都要记住这一点。
我们从编辑最后的提交信息开始。
git commit --amend # type :wq to save and close
Press “i” to edit, “Esc” to stop editing
我们把一切重设到最开始怎么样?
要找到第一次提交的ID,请运行这个命令并滚动(向下箭头)到最后。
git log --abbrev-commit
commit a07ee27
Author: Your Name your@email.address
Date: Sun Jul 11 11:47:16 2021 +0200
Adds hello.txt
(END)
type “:q” to close
现在运行这个来重置版本库,但保持所有的修改不被缓存。
git reset --soft COMMIT # e.g. a07ee27
与之相反,你也可以进行硬重置,用git reset --hard COMMIT
来删除所有修改。还有几种其他的重置方式,你可以从git文档中了解到。
别名
大多数时候,你只需要使用少数几个命令(主要是checkout、add、commit、pull、push和merge),但有些命令可能是你想要“以防万一”的。
存储这些信息的一种方法是git aliases
。要配置一个别名,只需在配置中设置它。例如,我经常使用的一个别名是git tree
,它以树的形式打印出一个漂亮的历史日志。
git config --global alias.tree ‘log --graph --decorate --pretty=oneline --abbrev-commit’
Try it with git tree
另一个有用的别名是删除所有合并的分支。
git config --global alias.clbr ‘!git branch --merged | grep -v * | xargs git branch -D’