git学习笔记

git学习

1. 创建仓库并提交

初始化全局配置

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

创建版本库

root@kali:~/Documents# mkdir testroot@kali:~/Documents# cd test/root@kali:~/Documents/test# git initInitialized empty Git repository in /root/Documents/test/.git/root@kali:~/Documents/test# ls -latotal 12drwxr-xr-x 3 root root 4096 Jul 13 10:40 .drwxr-xr-x 4 root root 4096 Jul 13 10:40 ..drwxr-xr-x 7 root root 4096 Jul 13 10:40 .gitroot@kali:~/Documents/test#

看到,多了一个.git文件夹,轻易不要修改

把文件添加到版本库

root@kali:~/Documents/test# echo "this is first commit" >> README.mdroot@kali:~/Documents/test# git add README.md root@kali:~/Documents/test# git commit -m "first commit"[master (root-commit) 5399ca6] first commit Committer: root <root@localhost.localdomain>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly. Run thefollowing command and follow the instructions in your editor to edityour configuration file:    git config --global --editAfter doing this, you may fix the identity used for this commit with:    git commit --amend --reset-author 1 file changed, 1 insertion(+) create mode 100644 README.mdroot@kali:~/Documents/test#

commit 可以一次提交多个文件,所以可以经过多次add单个文件后一起commit

git add其实是将修改加到缓存区,而commit是将缓存区的修改提交给仓库

查看改变

root@kali:~/Documents/test# echo "second commot" >> README.md root@kali:~/Documents/test# git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)modified:   README.mdno changes added to commit (use "git add" and/or "git commit -a")root@kali:~/Documents/test#

可以看到README.md已经被修改,但是还不知道被做了哪些修改

查看具体修改内容

root@kali:~/Documents/test# git diff README.mddiff --git a/README.md b/README.mdindex 2ed441a..b8254f4 100644--- a/README.md+++ b/README.md@@ -1 +1,2 @@ this is first commit+second commotroot@kali:~/Documents/test#

可以看到,修改内容是在第二行加了second commot

再次提交到仓库

root@kali:~/Documents/test# git add README.md

无输出

git commit提交

root@kali:~/Documents/test# git commit -m "add second"[master 22bae0c] add second Committer: root <root@localhost.localdomain>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly. Run thefollowing command and follow the instructions in your editor to edityour configuration file:    git config --global --editAfter doing this, you may fix the identity used for this commit with:    git commit --amend --reset-author 1 file changed, 1 insertion(+)

查看当前状态

root@kali:~/Documents/test# git statusOn branch masternothing to commit, working tree cleanroot@kali:~/Documents/test#

git告诉我们当前没有需要提交的内容

2. 版本回退

查看历史版本

root@kali:~/Documents/test# git logcommit 5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master)Author: root <root@localhost.localdomain>Date:   Fri Jul 13 11:03:42 2018 -0400    add thirdcommit 22bae0cda96e33739fcf0a7e7b2a459130784839Author: root <root@localhost.localdomain>Date:   Fri Jul 13 10:59:07 2018 -0400    add secondcommit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8Author: root <root@localhost.localdomain>Date:   Fri Jul 13 10:50:11 2018 -0400    first commitroot@kali:~/Documents/test# root@kali:~/Documents/test# git log --pretty=oneline  # 单行输出,美观直观5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master) add third22bae0cda96e33739fcf0a7e7b2a459130784839 add second5399ca651eeb6c295332cd117be0ed5d72c4dfa8 first commitroot@kali:~/Documents/test#

可以看到有三次提交

commit后的字符串是commit id

回滚到上一版本

git中,HEAD表示当前版本,HEAD是上一个版本,HEAD^是上上一版本,HEAD~100是上100个版本

root@kali:~/Documents/test# git reset --hard HEAD^HEAD is now at 22bae0c add secondroot@kali:~/Documents/test# cat README.md this is first commitsecond commotroot@kali:~/Documents/test# git logcommit 22bae0cda96e33739fcf0a7e7b2a459130784839 (HEAD -> master)Author: root <root@localhost.localdomain>Date:   Fri Jul 13 10:59:07 2018 -0400    add secondcommit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8Author: root <root@localhost.localdomain>Date:   Fri Jul 13 10:50:11 2018 -0400    first commitroot@kali:~/Documents/test#

返回原版本

root@kali:~/Documents/test# git reflog  # git命令历史纪录,可以找到commit id5ccca74 (HEAD -> master) HEAD@{0}: reset: moving to 5ccca22bae0c HEAD@{1}: reset: moving to HEAD^5ccca74 (HEAD -> master) HEAD@{2}: commit: add third22bae0c HEAD@{3}: commit: add second5399ca6 HEAD@{4}: commit (initial): first commitroot@kali:~/Documents/test# git reset --hard 5cccaHEAD is now at 5ccca74 add thirdroot@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitroot@kali:~/Documents/test#

3. 工作区和缓存区

本地文件夹就是工作区,.git目录就是版本库,

git add命令实际上是把修改放到缓存区,然后git commit会将缓存区的所有修改一次性提交到分支。

4. 管理修改

修改后add, 然后再修改,然后直接commit, 这时第二次修改并不会被提交

5. 撤销修改

root@kali:~/Documents/test# echo "Error" >> README.md root@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitErrorroot@kali:~/Documents/test# git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)modified:   README.mdno changes added to commit (use "git add" and/or "git commit -a")root@kali:~/Documents/test# git checkout -- README.mdroot@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitroot@kali:~/Documents/test# git statusOn branch masternothing to commit, working tree cleanroot@kali:~/Documents/test#

git checkout --其实就是让文件回到最近一次commitadd的状态

如果修改已经add到缓存区,则怎么办呢???

root@kali:~/Documents/test# echo "Error" >> README.md root@kali:~/Documents/test# git add README.md root@kali:~/Documents/test# git status On branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)modified:   README.mdroot@kali:~/Documents/test# git reset HEAD README.mdUnstaged changes after reset:MREADME.mdroot@kali:~/Documents/test# git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)modified:   README.mdno changes added to commit (use "git add" and/or "git commit -a")root@kali:~/Documents/test# git checkout -- README.mdroot@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitroot@kali:~/Documents/test#

先git reset HEAD README.md,再git checkout -- README.md

6. 删除文件

真删除

root@kali:~/Documents/test# touch test.txtroot@kali:~/Documents/test# git add test.txt root@kali:~/Documents/test# git commot -m "add test.txt"git: 'commot' is not a git command. See 'git --help'.The most similar command iscommitroot@kali:~/Documents/test# root@kali:~/Documents/test# rm test.txt root@kali:~/Documents/test# git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)new file:   test.txtChanges not staged for commit:  (use "git add/rm <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)deleted:    test.txtroot@kali:~/Documents/test# git rm test.txt rm 'test.txt'root@kali:~/Documents/test# git commit -m "remove test.txt"On branch masternothing to commit, working tree cleanroot@kali:~/Documents/test#

先rm 本地文件,再git rm 仓库文件,最后commit

误删

root@kali:~/Documents/test# touch test.mdroot@kali:~/Documents/test# git add test.md root@kali:~/Documents/test# git commit -m "add test.md"[master a62fe29] add test.md Committer: root <root@localhost.localdomain>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly. Run thefollowing command and follow the instructions in your editor to edityour configuration file:    git config --global --editAfter doing this, you may fix the identity used for this commit with:    git commit --amend --reset-author 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.mdroot@kali:~/Documents/test# root@kali:~/Documents/test# rm test.md root@kali:~/Documents/test# git statusOn branch masterChanges not staged for commit:  (use "git add/rm <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)deleted:    test.mdno changes added to commit (use "git add" and/or "git commit -a")root@kali:~/Documents/test# git checkout -- test.mdroot@kali:~/Documents/test# lsREADME.md  test.mdroot@kali:~/Documents/test#

直接用git checkout -- test.md还原即可

7. 添加远程仓库

可以把本地已有的仓库和github库关联

git remote add origin https://github.com/b4zinga/Lance.git

将所有内容推送到远程仓库

git push -u origin master

第一次推送master时,加上-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

以后只要本地有commit,就可以通过git push origin master将本地master分支的最新修改推送至github.

8. 从远程库克隆

git clone https://github.com/b4zinga/Lance.git

9. 创建与合并分支

创建并切换到dev分支

root@kali:~/Documents/test# git checkout -b devSwitched to a new branch 'dev'root@kali:~/Documents/test#

-b 参数表示创建并切换

相当于git branch dev 创建分支,然后git checkout dev转换分支两条命令

查看当前分支

root@kali:~/Documents/test# git branch* dev  masterroot@kali:~/Documents/test#

当前分支前会有*

root@kali:~/Documents/test# echo "new beanch" >> README.md root@kali:~/Documents/test# git add README.md root@kali:~/Documents/test# git commit -m "branch"[dev bae88b5] branch Committer: root <root@localhost.localdomain>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly. Run thefollowing command and follow the instructions in your editor to edityour configuration file:    git config --global --editAfter doing this, you may fix the identity used for this commit with:    git commit --amend --reset-author 1 file changed, 1 insertion(+)root@kali:~/Documents/test# root@kali:~/Documents/test# git checkout masterSwitched to branch 'master'root@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitroot@kali:~/Documents/test#

在dev分支修改,提交后转换到master,可以看到master上文件无变化

合并分支

root@kali:~/Documents/test# git merge devUpdating a62fe29..bae88b5Fast-forward README.md | 1 + 1 file changed, 1 insertion(+)root@kali:~/Documents/test# cat README.md this is first commitsecond commotthird commitnew beanchroot@kali:~/Documents/test#

git merge用于将指定分支合并到当前分支

Fast-forward指本次合并是“快进模式”

删除分支

root@kali:~/Documents/test# git branch -d devDeleted branch dev (was bae88b5).root@kali:~/Documents/test# git branch* masterroot@kali:~/Documents/test#

reference: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

(0)

相关推荐

  • Git的Fast Forward和no fast foward合并模式对比

    详细版本见个人博客:Git的Fast Forward和no fast foward合并模式对比 通常,合并分支时,如果没有分歧解决,就会直接移动文件指针,这就是Fast forward模式. 举例来说 ...

  • Git之HEAD和origin

    Git之HEAD和origin

  • 【效率】超详细!手把手带你快速入门 GitHub!

    作者:Peter     编辑:JackTian 来源:公众号「杰哥的IT之旅」 快速入门GitHub GitHub在程序开发领域家喻户晓,现在几乎整个互联网的开发者都将版本管理工具GitHub作为版 ...

  • 一则公报案例学习笔记:对修改股东出资期限应否适用资本多数决规则的思考|审判研究

    一.问题的提出 2021年第3期<最高人民法院公报案例>刊登了鸿大(上海)投资管理有限公司与姚锦城公司决议纠纷上诉案,裁判要旨为:"公司股东滥用控股地位,以多数决方式通过修改出资 ...

  • JAVA多线程学习笔记整理

    多线程: 三种创建方法 继承Thread类,以线程运行内容重写run方法,创建Thread对象并用start方法启动该线程. (匿名内部类) (Lambda表达式) 实现Runable接口,以线程运行 ...

  • 周哥学习笔记(2021.5.8)

    心理界限存在的意义,正是为了帮助人们控制情绪进入的量,不至于太过冷漠或太过投入,让我们保持一个合适的距离与外界互动. 人没有办法只通过吸收变得更美好和丰富,它必须通过大胆的碰撞和创造.如果不能保持足够 ...

  • 【学习笔记】控制角色移动的N种方法,但都离不开重复执行

    [学习笔记]控制角色移动的N种方法,但都离不开重复执行 今天我们讲一下控制角色移动的多种方法,因为缺少操作实例,希望课下同学们结合例子好好练习. 首先,我们说一下控制角色移动的多种方法.最比较常见的就 ...

  • 胡希恕伤寒论学习笔记——42

    42.太阳病,外证未解,脉浮弱者,当以汗解,宜桂枝汤. 字面意思是说:太阳病,外证依然存在,脉是浮弱的,治疗上依然需要通过出汗的方法,这时应该用桂枝汤一类的方剂. "宜"字说明不是 ...

  • 量柱擒涨停 - 量柱战法学习笔记(2)

    四.倍量战术 1.倍量的理解 [形态特征]:与前一个交易日比较高出1倍或1倍以上,就是倍量(4倍以上为发烧柱) ; [本质特征]:体现主力强势态度,主动(倍量阳/阴)买/卖盘吸筹坚决; [位置性质]: ...

  • 胡希恕伤寒论学习笔记——43

    43.太阳病,下之微喘者,表未解故也,桂枝加厚朴杏子汤主之. 桂枝加厚朴杏子汤方 桂枝三两 芍药三两 厚朴二两(炙,去皮) 杏仁五十枚(去皮尖)甘草二两(炙) 生姜三两(切)大枣十二枚(掰) 上七味, ...

  • 学习笔记:信息技术

    学习笔记:信息技术

  • 安全学习笔记

    安全学习笔记