当前位置: 首页 > news >正文

git学习笔记

目录

  • git学习
    • 1. 创建仓库并提交
    • 2. 版本回退
    • 3. 工作区和缓存区
    • 4. 管理修改
    • 5. 撤销修改
    • 6. 删除文件
    • 7. 添加远程仓库
    • 8. 从远程库克隆
    • 9. 创建与合并分支

git学习

1. 创建仓库并提交

初始化全局配置

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

创建版本库

root@kali:~/Documents# mkdir test
root@kali:~/Documents# cd test/
root@kali:~/Documents/test# git init
Initialized empty Git repository in /root/Documents/test/.git/
root@kali:~/Documents/test# ls -la
total 12
drwxr-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 .git
root@kali:~/Documents/test# 

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

把文件添加到版本库

root@kali:~/Documents/test# echo "this is first commit" >> README.md
root@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 based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After 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.md
root@kali:~/Documents/test# 

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

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

查看改变

root@kali:~/Documents/test# echo "second commot" >> README.md 
root@kali:~/Documents/test# git status
On branch master
Changes 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.md

no 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.md
diff --git a/README.md b/README.md
index 2ed441a..b8254f4 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 this is first commit
+second commot
root@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 based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After 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 status
On branch master
nothing to commit, working tree clean
root@kali:~/Documents/test# 

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

2. 版本回退

查看历史版本

root@kali:~/Documents/test# git log
commit 5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master)
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 11:03:42 2018 -0400

    add third

commit 22bae0cda96e33739fcf0a7e7b2a459130784839
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:59:07 2018 -0400

    add second

commit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:50:11 2018 -0400

    first commit
root@kali:~/Documents/test# 
root@kali:~/Documents/test# git log --pretty=oneline  # 单行输出,美观直观
5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master) add third
22bae0cda96e33739fcf0a7e7b2a459130784839 add second
5399ca651eeb6c295332cd117be0ed5d72c4dfa8 first commit
root@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 second
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
root@kali:~/Documents/test# git log
commit 22bae0cda96e33739fcf0a7e7b2a459130784839 (HEAD -> master)
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:59:07 2018 -0400

    add second

commit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:50:11 2018 -0400

    first commit
root@kali:~/Documents/test# 

返回原版本

root@kali:~/Documents/test# git reflog  # git命令历史纪录,可以找到commit id
5ccca74 (HEAD -> master) HEAD@{0}: reset: moving to 5ccca
22bae0c HEAD@{1}: reset: moving to HEAD^
5ccca74 (HEAD -> master) HEAD@{2}: commit: add third
22bae0c HEAD@{3}: commit: add second
5399ca6 HEAD@{4}: commit (initial): first commit
root@kali:~/Documents/test# git reset --hard 5ccca
HEAD is now at 5ccca74 add third
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@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 commit
second commot
third commit
Error
root@kali:~/Documents/test# git status
On branch master
Changes 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- README.md
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test# git status
On branch master
nothing to commit, working tree clean
root@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 master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README.md

root@kali:~/Documents/test# git reset HEAD README.md
Unstaged changes after reset:
M   README.md
root@kali:~/Documents/test# git status
On branch master
Changes 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- README.md
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test# 

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

6. 删除文件

真删除

root@kali:~/Documents/test# touch test.txt
root@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 is
    commit
root@kali:~/Documents/test# 
root@kali:~/Documents/test# rm test.txt 
root@kali:~/Documents/test# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   test.txt

Changes 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.txt

root@kali:~/Documents/test# git rm test.txt 
rm 'test.txt'
root@kali:~/Documents/test# git commit -m "remove test.txt"
On branch master
nothing to commit, working tree clean
root@kali:~/Documents/test# 

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

误删

root@kali:~/Documents/test# touch test.md
root@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 based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After 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.md
root@kali:~/Documents/test# 
root@kali:~/Documents/test# rm test.md 
root@kali:~/Documents/test# git status
On branch master
Changes 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- test.md
root@kali:~/Documents/test# ls
README.md  test.md
root@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 dev
Switched to a new branch 'dev'
root@kali:~/Documents/test# 

-b 参数表示创建并切换

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

查看当前分支

root@kali:~/Documents/test# git branch
* dev
  master
root@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 based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After 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 master
Switched to branch 'master'
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test#

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

合并分支

root@kali:~/Documents/test# git merge dev
Updating a62fe29..bae88b5
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
new beanch
root@kali:~/Documents/test# 

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

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

删除分支

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

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

转载于:https://www.cnblogs.com/0x4D75/p/9308027.html

相关文章:

  • 沉淀,再出发:Java基础知识汇总
  • final 总结
  • Swagger结合mustache模板生成后台接口代码、以及前后台建模代码
  • Java 6中的synchronized
  • less匹配模式
  • 聊聊世界编程语言排行榜的事
  • CF467C George and Job
  • EF6 CodeFirst使用MySql
  • java面试题干货96-125
  • 计算机专业的男女问题
  • 夕阳下,归校的背影
  • 小程序之map地图上不能在覆盖层
  • 大家好,我是新人,请多多关照,(*  ̄3)(ε ̄ *)么么
  • mangodb的存储
  • DICOM文件添加私有Tag(DCMTK Private Tag)
  • JS 中的深拷贝与浅拷贝
  • Apache的80端口被占用以及访问时报错403
  • httpie使用详解
  • JS笔记四:作用域、变量(函数)提升
  • Laravel核心解读--Facades
  • log4j2输出到kafka
  • React Native移动开发实战-3-实现页面间的数据传递
  • RxJS: 简单入门
  • vue.js框架原理浅析
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 聊聊flink的TableFactory
  • 前端面试之CSS3新特性
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 携程小程序初体验
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (力扣)1314.矩阵区域和
  • (学习日记)2024.01.19
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (转载)虚函数剖析
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .NET Compact Framework 多线程环境下的UI异步刷新
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .net中应用SQL缓存(实例使用)
  • /*在DataTable中更新、删除数据*/
  • @Autowired自动装配
  • @Builder用法
  • @vue/cli 3.x+引入jQuery
  • [ 隧道技术 ] 反弹shell的集中常见方式(四)python反弹shell
  • [04]Web前端进阶—JS伪数组
  • [android] 手机卫士黑名单功能(ListView优化)
  • [C++]——带你学习类和对象
  • [Delphi]一个功能完备的国密SM4类(TSM4)[20230329更新]