Git学习笔记

jupiter
2023-08-10 / 0 评论 / 46 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年08月11日,已超过261天没有更新,若内容或图片失效,请留言反馈。

1.Git简介

1.1 简介

  • Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
  • Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
  • Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

1.2 git工作流程

  • 克隆 Git 资源作为工作目录。
  • 在克隆的资源上添加或修改文件。
  • 如果其他人修改了,你可以更新资源。
  • 在提交前查看修改。
  • 提交修改。
  • 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

1.3 Git 工作区、暂存区和版本库

  • 工作区:就是你在电脑里能看到的目录。
  • 暂存区:英文叫 stage 或 index。一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
  • 版本库:工作区有一个隐藏目录 .git,这个不算工作区,而是 Git 的版本库。

2.Git 安装和配置用户信息

Git 各平台安装包下载地址为:http://git-scm.com/downloads

2.1 Linux 平台上安装

Git 的工作需要调用 curl,zlib,openssl,expat,libiconv 等库的代码,所以需要先安装这些依赖工具。

在有 yum 的系统上(比如 Fedora)或者有 apt-get 的系统上(比如 Debian 体系),可以用下面的命令安装:

各 Linux 系统可以使用其安装包管理工具(apt-get、yum 等)进行安装:

Debian/Ubuntu

$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
$ apt-get install git

Centos/RedHat

$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
$ yum -y install git-core

2.2 Windows 平台上安装

在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行:

安装包下载地址:https://gitforwindows.org/

官网慢,可以用国内的镜像:https://npm.taobao.org/mirrors/git-for-windows/

2.3 配置用户信息

配置个人的用户名称和电子邮件地址:

$ git config --global user.name "runoob"
$ git config --global user.email test@runoob.com
  • 如果用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。
  • 如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

要检查已有的配置信息,可以使用 git config --list 命令:

$ git config --list
http.postbuffer=2M
user.name=runoob
user.email=test@runoob.com

3.Git 基本操作

Git 常用的是以下 6 个命令:git clonegit pushgit addgit commitgit checkoutgit pull

  • workspace:工作区
  • staging area:暂存区/缓存区
  • local repository:版本库或本地仓库
  • remote repository:远程仓库

3.1 创建仓库命令

git init    # 初始化仓库
git clone    # 拷贝一份远程仓库,也就是下载一个项目。

3.2 提交与修改

git add    # 添加文件到暂存区
git status    # 查看仓库当前的状态,显示有变更的文件。
git diff    # 比较文件的不同,即暂存区和工作区的差异。
git commit    # 提交暂存区到本地仓库。
git reset    # 回退版本。
git rm    # 将文件从暂存区和工作区中删除。
git mv    # 移动或重命名工作区文件。

3.3 查看提交日志

git log    # 查看历史提交记录
git blame <file>    # 以列表形式查看指定文件的历史修改记录

3.4 远程操作

git remote    # 远程仓库操作
git fetch    # 从远程获取代码库
git pull    # 下载远程代码并合并
git push    # 上传远程代码并合并

4.Git 分支管理

  • 几乎每一种版本控制系统都以某种形式支持分支,一个分支代表一条独立的开发线。
  • 使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。
  • 创建的新分支会以当前分支的状态为基础

4.1 列出分支

$ git branch
* master

4.2 创建分支

如果我们要手动创建一个分支。执行 git branch (branchname) 即可。

$ git branch testing
$ git branch
* master
  testing

4.3 切换分支

git checkout (branchname)

4.4 创建和切换分支小实验(★★★)

# 初始化仓库
$ mkdir GitStudy
$ cd GitStudy
$ git init 

# 在master分支下创建master-branch.txt文件并提交到master分支
$ touch master-branch.txt
$ git add .
$ git commit -m "master first commit"
[master (root-commit) f2248bb] master first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master-branch.txt
 
 # 创建test分支,会以master分支为基础,这个时候ls看到的文件状态和master分支的是一致的
$ git branch test
$ git checkout test
Switched to branch 'test'
$ ls
master-branch.txt

# 在test分支下创建test-branch.txt文件并提交到test分支
$ touch test-branch.txt
$ ls
master-branch.txt  test-branch.txt
$ git add .
$ git commit -m "test branch firsh commit"
[test 070c6a3] test branch firsh commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test-branch.txt

# 切换回master分支查看test分支的修改是否对master分支造成了影响
$ git checkout master
Switched to branch 'master'
$ ls
master-branch.txt

 4.5 合并分支

一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。 你可以使用以下命令将任何分支合并到当前分支中去。

git merge branchname

执行合并命令会将指定分支合并到当前工作分支上,我们假设当前工作分支为main,合并过程如下图所示。

合并完后就可以删除分支:

$ git branch -d branchname

 4.6 分支合并小实验

# 查看主分支已提交的文件
$ git branch
* master
  test
$ ls
master-branch.txt  master.txt

# 查看test分支文件已提交的文件
$ git checkout test
Switched to branch 'test'
$ git branch
  master
* test
$ ls
master-branch.txt  test-branch.txt

# 合并test分支到master分支
$ git checkout master
Switched to branch 'master'
$ git merge test
Merge made by the 'ort' strategy.
 test-branch.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test-branch.txt

# 查看合并后的文件,均为已提交的文件
$ ls
master-branch.txt  master.txt  test-branch.txt

# 删除test分支
$ git branch -d test
Deleted branch test (was 070c6a3).
$ git branch
* master

 4.7 分支合并冲突小实验

# 该实验在空白文件夹进行实验

# 初始化master分支并创建init.txt文件并提交
$ git init
Initialized empty Git repository in C:/Users/LuoJia/Desktop/WorkSpace/GitStudy/.
git/

$ touch init.txt
$ git add .
$ git commit -m "master add init.txt"
[master (root-commit) 8af249b] master add init.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 init.txt


# 新建test分支
$ git branch test
$ git checkout test
Switched to branch 'test'

# 在test分支下创建test.txt和confict.txt文件并提交
$ touch test.txt
$ vim confict.txt

$ cat confict.txt
<?php
echo 'test';
?>
$ git add .
warning: in the working copy of 'confict.txt', LF will be replaced by CRLF the n
ext time Git touches it
$ git commit -m "test write"
[test 30bfb3c] test write
 2 files changed, 3 insertions(+)
 create mode 100644 confict.txt
 create mode 100644 test.txt


# 回到master分支,创建master.txt和confict.txt文件并提交
$ git checkout master
Switched to branch 'master'
$ ls
init.txt
$ touch master.txt
$ vim confict.txt

$ cat confict.txt
<?php
echo 'master';
?>
$ git add .
warning: in the working copy of 'confict.txt', LF will be replaced by CRLF the n
ext time Git touches it
$ git commit -m "master write"
[master dd7620b] master write
 2 files changed, 3 insertions(+)
 create mode 100644 confict.txt
 create mode 100644 master.txt


# 尝试合并test分支,会发现发生了文件冲突
$  git merge test
Auto-merging confict.txt
CONFLICT (add/add): Merge conflict in confict.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat confict.txt
<?php
<<<<<<< HEAD
echo 'master';
=======
echo 'test';
>>>>>>> test
?>


# 需要手动编辑解决冲突
$ vim confict.txt

$ cat confict.txt
<?php
echo 'master';
echo 'test';
?>

# 手动解决冲突后需要对冲突文件进行重新提交
$ git add confict.txt
$ git commit -m "solve confict of confict.txt"
[master 9b4bbfc] solve confict of confict.txt

 参考资料

1. Git 教程 | 菜鸟教程 (runoob.com)
2. Git 常用基本命令使用详细大全_git命令_坚强的小水滴的博客-CSDN博客
3. 使用分支——Git Merge命令 - 知乎 (zhihu.com)

0

评论 (0)

打卡
取消