跳至主要內容

Git 入门

Haynes...大约 3 分钟Linuxgitshell

以前对 Git 一知半解,直到最近发现了一个 Git 游戏 https://learngitbranching.js.orgopen in new window 让我重新更加深入的了解了 Git 的工作原理。

learn git branching 与传统的文字讲解相比,它有一个显著的亮点——图形化的 Git 提交树。这个树状结构可以实时反馈并清晰地展示你所进行的 Git 操作对代码管理的实际影响,为你提供了出色的反馈机制。相比于直接使用命令行学习 Git,通过这种方式你将更加直观地了解当前的操作意味着什么。而且,它以一种闯关的形式进行 Git 教学。如果你能够顺利通过所有关卡,那么你对于 Git 的理解也将达到一个相当不错的水平。

首先分享一点 Git 的 Documenation

基础篇

1. Git Commit

git commit 命令是将暂存区内容添加到本地仓库中。

基本命令:

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
	   [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]
	   [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
	   [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
	   [--date=<date>] [--cleanup=<mode>] [--[no-]status]
	   [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
	   [(--trailer <token>[(=|:)<value>])…​] [-S[<keyid>]]
	   [--] [<pathspec>…​]

题目:
完成两次提交

git commit -m "first commit"
git commit -m "second commit"

2. Git Branch

git branch 命令用于显示、创建、删除分支。
基本命令:

git branch [--color[=<when>] | --no-color] [--show-current]
	[-v [--abbrev=<n> | --no-abbrev]]
	[--column[=<options>] | --no-column] [--sort=<key>]
	[--merged [<commit>]] [--no-merged [<commit>]]
	[--contains [<commit>]] [--no-contains [<commit>]]
	[--points-at <object>] [--format=<format>]
	[(-r | --remotes) | (-a | --all)]
	[--list] [<pattern>…​]
git branch [--track[=(direct|inherit)] | --no-track] [-f]
	[--recurse-submodules] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-c | -C) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>…​
git branch --edit-description [<branchname>]

题目:
创建一个名为 bugFix 的新分支,然后切换过去。

git branch bugFix
git checkout bugFix

提示

Git 2.23 版本中,引入了 git switch 的新命令,最终会取代 git checkout,因为 checkout 作为单个命令承载了很多独立的功能。
使用 git switch 用一句话就可以完成上述操作

git switch -c bugFix

3. Git Merge

基本命令:

# 合并指定分支到当前分支
git merge [branch]

题目:

  • 创建新分支 bugFix

  • git checkout bugFix 命令切换到该分支

  • 提交一次

  • git checkout main 切换回 main

  • 再提交一次

  • git mergebugFix 合并到 main

git switch -c bugFix
git commit -m "first commit"
git checkout main
git commit -m "second commit"
git merge bugFix

4. Git Rebase

基本命令:

git rebase [-i | --interactive] [<options>] [--exec <cmd>]
	[--onto <newbase> | --keep-base] [<upstream> [<branch>]]
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
	--root [<branch>]
git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)

题目:

  • 新建并切换到 bugFix 分支
  • 提交一次
  • 切换回 main 分支再提交一次
  • 再次切换到 bugFix 分支,rebasemain
git switch -c bugFix
git commit -m "first commit"
git checkout main
git commit -m "second commit"
git checkout bugFix
git rebase main

进阶篇

1. 分离 HEAD

上次编辑于:
贡献者: HaynesChen
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3