今天学习Git的基本操作。
Git配置
Git有非常多的子命令,采用git <subcommand>
这种形式,不过只有少数的几种是我们经常用到的。
首先对git进行身份配置
git config --global user.name "yongwudao"
git config --global user.email "yongwudao@163.com"
方便区分,设置命令颜色
git config --global color.ui true
Git初始化
git初始化有两种方式,首先是基于已经存在的文件夹:
cd cd zmays-snp/
git init
# Initialized empty Git repository in /home/way/zmays-snp/.git/
会生成一个隐藏的.git
文件夹,另外一种方式是直接clone
别人的库:
git clone https://github.com/lh3/seqtk.git
# Cloning into 'seqtk'...
# remote: Enumerating objects: 359, done.
# remote: Counting objects: 100% (20/20), done.
# remote: Compressing objects: 100% (14/14), done.
# remote: Total 359 (delta 11), reused 15 (delta 6), pack-reused 339
# Receiving objects: 100% (359/359), 171.46 KiB | 1.02 MiB/s, done.
# Resolving deltas: 100% (206/206), done.
cd seqtk/
# ls
# LICENSE Makefile README.md khash.h kseq.h seqtk.c
Git文件track和stage
首先使用git status
查看状态:
git status
# On branch master
# No commits yet
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# README.md
# data/
# nothing added to commit but untracked files present (use "git add" to track)
这里的信息说明我们没有文件是被追踪的,我们可以通过git add
命令来添加追踪文件。手动添加虽然麻烦,但是可以避免追踪一些不必要文件例如大的原始数据,中间数据。
这里我们以README.md
文件为例:
git add README.md
git status
# On branch master
# No commits yet
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: README.md
#Untracked files:
# (use "git add <file>..." to include in what will be committed)
# data/
现在READMD.md文件已经被追踪了,如果README.md文件被改变了,需要使用git add
命令进行stage才会在commit的时候提交最新版本,否则不会生成拍照(fig.1)。
Figure 1 track与stage的区别
可以这样理解:git commit相当于所有文件状态的拍照,第一次git add让git知道一个文件的存在并生成第一次存档,后面对文件进行修改但是没有改完的话可以不进行更新,等到将这个文件修改完毕了使用
git add
把这个文件加入更新列表,下次commit的时候才生成拍照。举例说明:
echo "This is a test" >> README.md
git status
# On branch master
# No commits yet
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: README.md
# 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
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# data/
可以看到README.md修改了但是并没有staged for commit,commit的话不会拍照修改后的版本
git add README.md
git status
# On branch master
# No commits yet
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: README.md
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# data/
使用git add
stage之后下次commit之后会拍照修改后的README.md文件
git commit生成工程拍照
理解了stage之后commit比较简单:
git commit -m "initial import"
# [master (root-commit) 615348d] initial import
# 1 file changed, 38 insertions(+)
# create mode 100644 README.md
-m
后面给出了描述信息,最好使用有意义的描述对本次的拍照版本进行说明,方便以后查看。如果不加-m
的话会打开默认编辑器。可以使用以下命令修改默认编辑器
git config --global core.editor vim