nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

课程笔记: 玩转 Git 三剑客 (苏玲) #307

Open nonocast opened 2 years ago

nonocast commented 2 years ago

version: git 2.37.0

三剑客

git

git 怎么配置?

通过git config命令实现,配置分为local, global, system 3个级别, 通过--list查看对应的配置:

~ git config --list (当前上下文下的合并配置, 即system+global+local, 局部优先)
~ git config --list --local (仅针对当前仓库)
~ git config --list --global (对当前用户下所有仓库有效)
~ git config --list --system (对所有用户有效)

为什么要配置user.name和user.email?

一般安装后都需要配置global user, 如下:

~ git config --global user.name 'your_name'
~ git config --global user.email 'your_email@domain.com'

主要是因为git commit时需要填写name/email标识提交人,如果没有配置,git会根据当前系统的username和hostname自动生成,这显然不是我们希望的:

git101 git:(main) ✗ git commit -m "whatever"
[main cc0ff5d] whatever
 Committer: nonocast <nonocast@HUIs-Mac-mini.lan>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)

git 本地仓库怎么提交?

git本地仓库分为3个区域:

一般情况下,通过git add将工作区内容加入到暂存区,然后通过git commit将暂存区提交仓库区。

如何放弃暂存区的所有修改?

比如git add修改文件,或者做了mv操作等等,只要我没有commit,那么就可以直接通过git reset --hard撤回到原先状态。

怎么查看git log?

可以通过git help loggit help --web log查看进一步的帮助。

怎么创建切换branch?

什么tig?

tig - text-mode interface for Git

tig        [options] [revisions] [--] [paths]
tig log    [options] [revisions] [--] [paths]
tig show   [options] [revisions] [--] [paths]
tig reflog [options] [revisions]
tig blame  [options] [rev] [--] path
tig grep   [options] [pattern]
tig refs   [options]
tig stash  [options]
tig status
tig <      [Git command output]

如何修改过往commit的meessage?

16 | 怎么修改老旧commit的message?

怎样把连续的多个commit整理为1个commit?

17 | 怎样把连续的多个commit整理成1个?

怎样把间隔的多个commit整理为1个commit?

18 | 怎样把间隔的几个commit整理成1个?

怎么比较暂存区和HEAD的差异?

怎么比较工作区和暂存区的差异?

如何让暂存区恢复成和HEAD的一样?

如何让工作区的文件恢复为和暂存区一样?

reset and checkout

取消最近的几次提交?

比如有c1,c2,c3, 现在HEAD指向c3,然后通过git reset --hard c1,此时工作区恢复为c1, 暂存区清空, HEAD指向c1。

git支持哪些同步协议?

Git - The Protocols

注:

git是如何考虑多个仓库的同步的?

一个git仓库可以通过git remote管理和外部仓库的连接, 如下:

# hello
~ git init hello && cd hello
Initialized empty Git repository in /.../hello/.git/
~ echo "hi,hello" > hello.md 
~ git add hello.md 
~ git commit -m "initial hello.md"
[main (root-commit) e2d4af5] initial hello.md
 1 file changed, 1 insertion(+)
 create mode 100644 hello.md

# foo
~ git init foo && cd foo
Initialized empty Git repository in /.../foo/.git/
~ echo "hi,foo" > foo.md 
~ git add foo.md 
~ git commit -m "initial foo.md"
[main (root-commit) 324e318] initial foo.md
 1 file changed, 1 insertion(+)
 create mode 100644 foo.md

# 同样的方式建立bar repo

此时目录下有hello, foo, bar三个仓库,各自有一个md,然后通过git remote 建立连接:

~ git remote add foo ../foo
~ git remote add bar ../bar
~ git remote 
bar
foo
~ git remote -v
bar ../bar (fetch)
bar ../bar (push)
foo ../foo (fetch)
foo ../foo (push)

通过git fetch就可以将远端的内容复制到本地仓库:

~ git fetch foo
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 187 bytes | 187.00 KiB/s, done.
From ../foo
 * [new branch]      main       -> foo/main
~ git fetch bar
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 191 bytes | 191.00 KiB/s, done.
From ../bar
 * [new branch]      main       -> bar/main
~ git branch -av
* main             56e8d74 initial hello
  remotes/bar/main 775d204 initial bar
  remotes/foo/main 986eac5 initial foo

参考教程