Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git学习笔记摘要1——概要 #154

Open
soapgu opened this issue Jul 23, 2022 · 0 comments
Open

Git学习笔记摘要1——概要 #154

soapgu opened this issue Jul 23, 2022 · 0 comments
Labels
Git IDE Good for newcomers

Comments

@soapgu
Copy link
Owner

soapgu commented Jul 23, 2022

  • 摘要

记录一些要点,防止遗忘

  • 作用域范围参数
    --local
    --global

  • 增加所有已跟踪文件
    git add -u

  • 关于commit
    除了第一次提交,每次提交都要parent,children,可以把commit想象成一个提交树来理解。每个提交后期都“可能”分叉
    以前没没多注意。

  • 关于git log
    git log
    --oneline 简洁模式
    -n4 最近几条
    --all 所有分支
    也可以在后面加分支名

  • .git文件夹内容
    HEAD:当前分支
    config:不解释了
    refs:存放分支以及tag信息,tag信息存放commit
    objects:存放内容的核心文件,会分类打包
    git文件类型:commit,tree,blob

  • git文件关系
    commit->tree->blob
    blob:只关心内容不关心文件名
    tree:可以理解会文件夹和文件结构的定义,如果blob是File,那么tree就是directory信息

  • HEAD

  1. 分离头指针感觉没啥必要使用,感觉是临时切换使用。如果需要签入则需要建立新分支
  2. HEAD始终对应一个commit,可以理解为一个指针(游标)
  3. HEAD的高级用法
   HEAD^ :parent,等价HEAD~1
   HEAD^^: parent的parent HEAD~2
  • 删除branch
    git branch -d -D
    -d 可能会有警告信息 -D直接删除但是会有丢失commit风险

  • 修改最新一次的commit的message
    git commit --amend

  • 修改历史commit的message

这里难度一下加大,需要用到变基操作

  1. 找到需要修改的commit的parent的commit
  2. git rebase -i 上门找到的parent的commit
  3. 变基核心,对于不变的commit我们使用默认的p(pick),修改commit的使用r(reword)
  4. 完成变基,最新的commit的序号也变化了
    思考,变基操作很迷,感觉就是微调一下重头又提交了一次感觉。
  • 合并连续多个本地commit
    还是老套路 git rebase -i
    需要至少一个pick(选择最早的一个)
    其他选择sqush/s

  • 合并间隔的本地commit
    思想和上门的一样,还是需要把要合并的commit
    注意:这里把root commit也要合并进来,所以需要拷贝pick (root commit id)也要拷贝进来
    选第一个pick
    后面的拍在他后面sqush
    完成后
    再执行一次git rebase --continue

  • git比较暂存区和HEAD
    git diff --cached
    发现问题,再次修改后 git add,如此反复

  • git比较工作区暂存区区别
    git diff
    注意这里只会列出有差异的,工作区的改动全提交暂存区就没有差异了
    对于指定文件比较可以增加-- 文件名/一个或者多个

  • git恢复暂存区到HEAD
    有点类似rollback感觉
    git reset HEAD
    如果是指定部分文件
    git reset HEAD -- 文件1 文件2 .。。。

  • git工作区内容恢复成暂存区
    记住口诀
    如果要动工作区 用checkout
    如果要动暂存区 用reset
    git checkout --<file>

  • 删除最近几次的提交
    就是要恢复到前面的某一个commit,最后几次的commit要“丢弃”
    使用 git reset --hard <指定commit>
    就是一次“危险”的时光倒流,回到过去,但是未来的记忆全部消失了

  • 比较不同分支的文件比较
    git diff <分支/commit> <分支/commit> -- <文件>

  • git改名&删除文件

git rm <file>
git mv <source> <target>
  • git stash
    git临时存储以及恢复。使用场景:紧急任务
    git stash
    git stash list
    git stash apply 不清除记录
    git stash pop 清除记录

  • git远端beif
    git remote add name path
    git push --set-upstream name

  • github把本地仓库推到远端

  1. github建立一个仓库
  2. git remote add github git@github.com:soapgu/GitLearn.git
  3. git push github --all
  • 允许两个没有关系的分支merge
    --allow-unrelated-histories
    merge成功后会产生两个parent

  • git多人协作相关

  • 本地切出远端分支
    git checktout -b <branch> --track <remote>/<branch>
    --track 好像可以省略
    因为远端分支和本地分支已经做好绑定,git push可以直接推送到远端
    平时我们都是用ide基本无感

  • 关于合作push文件
    当本地分支不是保持和远端“最新”,会发生无法push的情况
    需要,
    先git fetch
    git merge remote branch
    最后再 git push
    这也是我们平时常用的用法,会多一次merge 的commit记录

  • git冲突管理
    手工处理
    直接 vi 相关冲突的文件,再执行commit,并写好手工merge的注释

  • git禁手操作

  • 禁止git push -f

  • 禁止对集成分支rebase
    会对其他正在开发的成员造成影响,会造成本地分支不是fast forward

  • git rebase小实验

  1. 从远端更新readme
    图片
    图片
    好了,修改成功
    commit 28da5b1c1649c5df41752fb52afb7e15df17ecde

2.本地分支的readme不是最新的,做一次修改并提交
图片

PS D:\Github\GitPlayPen> git add -u
PS D:\Github\GitPlayPen> git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

PS D:\Github\GitPlayPen> git commit -m "add line local"
[master 56fc66d] add line local
 1 file changed, 2 insertions(+), 1 deletion(-)

PS D:\Github\GitPlayPen> git log -n1
commit 56fc66d9b24dc5ff8e740aebf0ccd83355a36ed5 (HEAD -> master)
Author: soapgu <ghost_gu@163.com>
Date:   Mon Jul 25 16:47:07 2022 +0800

    add line local
  1. 从远端分支fetch
PS D:\Github\GitPlayPen> git fetch github master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 702 bytes | 78.00 KiB/s, done.
From github.com:soapgu/GitLearn
 * branch            master     -> FETCH_HEAD
   b4e1b06..28da5b1  master     -> github/master
PS D:\Github\GitPlayPen> git branch -av
* master                56fc66d add line local
  remotes/github/master 28da5b1 add line in Readme

4.变基变基变基
变基变基个人理解就是以什么为基,变的是什么

D:\Github\GitPlayPen> git rebase -i github master git rebase -i github/master

这里rebase -i 后面的就是基,远端分支github/master,而变的是本地的master分支
图片
把本地的master分支,远端分支基础上再对本地分支的所有更改再重演一遍
集中pick就是本地的这次commit修改,56fc66d id一致

  1. rebase冲突解决
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    error: could not apply 56fc66d... add line local
    hint: Resolve all conflicts manually, mark them as resolved with
    hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
    hint: You can instead skip this commit: run "git rebase --skip".
    hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
    Could not apply 56fc66d... add line local

毫无意外,变基失败了,我们要做手工合并处理

# Hello World
<<<<<<< HEAD
just for reabse test,modify from remote
=======
add line modify from local branch 
>>>>>>> 56fc66d (add line local)

以前都是用工具ide做的冲突管理,惭愧惭愧
手工改下“最终版本”
图片

PS D:\Github\GitPlayPen> git add .\README.md
PS D:\Github\GitPlayPen> git rebase --continue

图片
最后再重新更新下commit

[detached HEAD 000d7f2] add line local,and merge from remote
 1 file changed, 2 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.

好了rebase成功了,再看下log

PS D:\Github\GitPlayPen> git log -n3
commit 000d7f2ca15265dba99d4c9e2521ed1e1d7a6957 (HEAD -> master)
Author: soapgu <ghost_gu@163.com>
Date:   Mon Jul 25 16:47:07 2022 +0800

    add line local,and merge from remote

commit 28da5b1c1649c5df41752fb52afb7e15df17ecde (github/master)
Author: soapgu <ghost_gu@163.com>
Date:   Mon Jul 25 16:40:22 2022 +0800

    add line in Readme

commit b4e1b066cbb04c7b431fbf4a7d3ef23088566789
Author: soapgu <ghost_gu@163.com>
Date:   Mon Jul 25 15:55:12 2022 +0800

    add App.xaml.cs comment line2 merge from line1

可以看到
commit 28da5b1c1649c5df41752fb52afb7e15df17ecde (github/master)
这一行,这里指明了这条commit是从remote搬过来的。用控制台看更明显
图片
另外我们原理的本地的add line local的commit:56fc66d已经不存在了,变基后变成了000d7f2,comment也换成了最新的。

  1. git push
PS D:\Github\GitPlayPen> git push github
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:soapgu/GitLearn.git
   28da5b1..000d7f2  master -> master 

图片

最后用网上的图来示意下
图片
图片

PS D:\Github\GitPlayPen> git fetch github master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 710 bytes | 78.00 KiB/s, done.
From github.com:soapgu/GitLearn
 * branch            master     -> FETCH_HEAD
   000d7f2..f5e5bae  master     -> github/master
PS D:\Github\GitPlayPen> git branch -av
* master                000d7f2 add line local,and merge from remote
  remotes/github/master f5e5bae update readme from remote again

图片

主要区别:

  • 使用merge远端的commit和本地的commit都原汁原味保留
  • 如果用rebase本地的commit在变基merge的时候合并修改了
@soapgu soapgu changed the title Git学习笔记摘要 Git学习笔记摘要1 Jul 26, 2022
@soapgu soapgu changed the title Git学习笔记摘要1 Git学习笔记摘要1——概要 Jul 26, 2022
@soapgu soapgu added the IDE Good for newcomers label Jul 26, 2022
@soapgu soapgu added the Git label Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Git IDE Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant