版本控制工具Git的安装及使用方法

看到生信大佬都在用git hub管理代码,就一直也想学怎么往git hub上传代码,学了一段时间,今天整理了下学习笔记

  • git概述
  • git安装
  • git常用命令
  • git对文件版本的切换操作
  • git分支操作
  • 远程库:github

git概述

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
应用场景:分析一个项目的时候,代码会不断的修改,通常修改代码后有两个选择:第一种是直接保存,覆盖掉原来的修改,这样修改之前的代码就丢失了;第二种是修改后另存为一个副本,修改的次数多了就会产生很多的副本,虽然保存了修改之前的代码,但是文件会变的很复杂,类似于下面这种:

git采用分布式版本控制系统,可以记录文件修改历史记录,方便用户查看历史版本,方便进行版本之间的切换

git安装

官网下载:https://git-scm.com/
安装路径要求:全英文路径,且没有空格
到这一步选择:只在git bash 里使用git,不修改环境变量

再勾选:使用符号链接

其他的安装选项,默认就可以

git常用命令

## 常用命令
git config --global user.name 用户名  #设置用户签名
git config --global user.email 邮箱  #设置用户签名 
git init  #初始化本地库 
git status #查看本地库状态 
git add 文件名 #添加到暂存区 
git commit -m "日志信息" 文件名 #提交到本地库 
git reflog #查看历史记录 
git reset --hard 版本号  #版本穿梭

1.设置用户签名

## 首次安装必须设置用户签名,否则无法提交代码
## 命令
git config --global user.name 用户名 
git config --global user.email 邮箱

## 实操
git config --global user.name steven
git config --global user.email steven@email.com
## 查看
cat ~/.gitconfig
[user]
name = steven
email = steven@email.com

2.初始化本地仓库

## 初始化本地仓库,会创建一个.git的隐藏文件,相关的历史版本信息会存储在这个文件夹里
## win10下,进去你要初始化的文件夹,右键后,点击Git Bash Here,会出现命令行终端界面
## 命令
git init
## 查看是否出现.git的隐藏文件
ls -a 
./  ../  .git/

3.查看本地库状态

## 命令
git status
## 首次查看,提示在master主分支上,没有任何提交的文件
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

4.创建文件并添加暂存区

##命令
git add 文件名

##示例
## vim创建一个txt文件
vim test.txt
aaaa
bbbb
cccc
dddd
:wq #保存退出

## 再次查看本地库状态
git status
On branch master

No commits yet

Untracked files: # 这时会出现未被追踪的文件缓存区
(use "git add <file>..." to include in what will be committed)
test.txt

nothing added to commit but untracked files present (use "git add" to track)

## 将文件添加到缓存区
git add test.txt
warning: LF will be replaced by CRLF in test.txt. 
#警告不用管,win系统和linux系统的换行符不一样,git会自动替换
The file will have its original line endings in your working directory

5.将暂存区文件提交本地库

## 命令 提交本地库
git commit -m " 日志信息"  文件名 

## 示例
## 首先再次查看下本地库的状态
git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file:   test.txt #因为上一步已经将文件提交到暂存区,所以这次提示有新的文件
## 将暂存区文件,提交本地库
git commit -m"this is my first commit" test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
[master (root-commit) 5960bd5] this is my first commit
1 file changed, 5 insertions(+) #提示一个文件改变
create mode 100644 test.txt
## 再次查看本地库的状态
git status
On branch master
nothing to commit, working tree clean #提示没有文件需要提交

git对文件版本的切换操作

git reflog #查看版本信息
git log  #查看版本详细信息
git reset --hard  版本号 #版本的切换

1.首先产生新的文件版本

## 首先查看文件版本
git reflog
5960bd5 (HEAD -> master) HEAD@{0}: commit (initial): this is my first commit #提示只有一个版本,那下面修改原来的文件再进行提交,生成新的文件版本,这个5960bd5是版本号,后面用版本号切换文件版本

## 在之前创建的文件中,进行修改,添加两行数字
vim test.txt
aaaa  1111
bbbb  2222
cccc
dddd
:wq #按esc键后,输入:wq保存退出

## 然后查看本地库状态
git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified:   test.txt #提示有文件修改

no changes added to commit (use "git add" and/or "git commit -a")

## 最后将修改后的文件添加暂存区,提交本地库
git add test.txt
git commit -m"this is my second commit" test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
[master 653b89c] this is my second commit
1 file changed, 3 insertions(+), 3 deletions(-) 

## 再次查看版本,此时有两个文件版本,并且指向第二次提交的版本
git reflog
653b89c (HEAD -> master) HEAD@{0}: commit: this is my second commit
5960bd5 HEAD@{1}: commit (initial): this is my first commit

## 再查看下文件内容,文件内容也修改了
cat test.txt
aaaa  1111
bbbb  2222
cccc
dddd

2.切换文件版本

## 由第二个版本,切换回第一个文件版本,版本号为:5960bd5
git reset --hard 5960bd5
HEAD is now at 5960bd5 this is my first commit #系统提示,指针又指向了文件的第一个版本

## 查看文件版本,指针在第一个文件版本
git reflog
5960bd5 (HEAD -> master) HEAD@{0}: reset: moving to 5960bd5 
653b89c HEAD@{1}: commit: this is my second commit
5960bd5 (HEAD -> master) HEAD@{2}: commit (initial): this is my first commit

## 然后查看文件内容,内容有变回了第一次输入的内容
cat test.txt
aaaa
bbbb
cccc
dddd

git分支操作

## 常用命令
git branch 分支名 #创建分支  
git branch -v  #查看分支 
git checkout 分支名 #切换分支
git merge 分支名  #把指定的分支合并到当前分支上

1.查看分支

## 查看分支,显示只有一个master分支
git branch -v
* master 5960bd5 this is my first commit

2.创建分支

## 创建一个test的分支
git branch test

## 然后查看分支,多了一个新的test的分支,但是还处在原来的分支上
git branch -v
* master 5960bd5 this is my first commit
test   5960bd5 this is my first commit

3.切换分支

## 从master分支切换到test分支
git checkout test

## 再次查看分子,此时*号出现在test上,说明已经切换到了test分支
git branch -v
master 5960bd5 this is my first commit
* test   5960bd5 this is my first commit

4.修改分支上的文件

## 在小分支上修改文件,不会对主分支上的文件造成影响,相当于创建了一个文件的副本
## 首先查看test分支下的文件内容,跟master分支下的内容是一样的
cat test.txt
aaaa
bbbb
cccc
dddd

## 修改下前两行
vim test.txt
aaaa  test
bbbb  test
cccc
dddd
:wq

## 添加暂存区,并提交库
git add test.txt
git commit -m"this is my third commid from test branch" test.txt

5.合并分支

## 在test下的文件被修改了,所以跟master分支下的文件会不一样
## 接下来切换回master的分支,并合并这两个分支,这两个分支下的文件就会一样了
## 切换回master分支
git checkout master

## 查看是否切换成功;切换成功,而且可以看到两个文件的版本还是不一样的
git branch -v
* master 5960bd5 this is my first commit
test   581a1c2 this is my third commid from test branch

## 查看master分支下的文件内容
cat test.txt
aaaa
bbbb
cccc
dddd

## 分支合并
git merge test

远程库:github

## 常用命令
git remote -v  #查看当前所有远程地址别名 
git remote add 别名 远程地址 #起别名 
git push 别名 分支 #推送本地分支上的内容到远程仓库 
git clone 远程地址 #将远程仓库的内容克隆到本地 
git pull 远程库地址别名 远程分支名 #将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并
  1. 在git hub 上创建仓库 这个相当于把代码保存在云端

image-20210515164311711

  1. 查看以及创建仓库别名
## 查看当前的远程地址别名,如果之前没有创建过别名,不会返回信息
git remote -v

## 创建远程地址别名
git remote add  git_test https://github.com/ChaoXu1997/git_test.git

## 创建完后,再次查看别名信息,返回了创建的别名
git remote -v
git_test        https://github.com/ChaoXu1997/git_test.git (fetch)
git_test        https://github.com/ChaoXu1997/git_test.git (push)

在git hub 上创建完库后,在页面上看见库的地址:

  1. 推送本地分支到远程库
## 推送master分支到远程库
## 会弹出链接,点击浏览器授权登录
## 国内可能会有由于网络问题上传失败,不会科学上网就用gitee作为自己的远程库吧
git push  git_test master

image-20210515170014580

试了五六次才上传成功,然后再刷新git hub的界面就能看见自己上传的文件了

  1. 拉取远程库到本地
## 在git hub 网站上修改了上传的代码后,那么远程库和本地库的代码就不一样了
## 那么需要拉取远程库到本地
git pull git_test master
remote: Enumerating objects: 5, done. #提示拉取成功
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 660 bytes | 2.00 KiB/s, done.
From https://github.com/ChaoXu1997/git_test
* branch            master     -> FETCH_HEAD
653b89c..b71d993  master     -> git_test/master
Updating 653b89c..b71d993
Fast-forward
test.txt | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

## 查看内容,跟远程库上的文件一模一样了
cat test.txt
aaaa  1111
bbbb  2222
cccc
dddd
eeee
ffff
gggg
modify from git hub

image-20210515171536198

image-20210515171605276

  1. 克隆远程库到本地 git hub 上公共库的代码都是开源的,可以把别人上传的代码克隆到自己本地
## 克隆代码到本地
## clone会做以下操作:1.拉取代码;2.初始化本地化仓库;3.创建远程库的别名
## 新建一个展示克隆操作的文件夹,进入文件夹后,右键打开git bash
## 在终端输入命令
git clone https://github.com/ChaoXu1997/git_test.git #地址在git hub的要克隆的库页面上有

## ls查看,出现了被克隆的库
ls
git_test/

【参考资料】:B站尚硅谷git课程https://www.bilibili.com/video/BV1vy4y1s7k6?p=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许超Steven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值