0%

git tutorial

Guide

git config

配置user.name user.email

代码提交时,如果不希望使用global的用户,可以为每个项目单独配置用户名和提交邮箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#全局
git config --global user.name "username"
git config --global user.email "[email protected]"

#取消全局
git config --global --unset user.name
git config --global --unset user.email

#局部配置
git config user.name "username"
git config user.email "[email protected]"

#取消局部
git config --unset user.name
git config --unset user.email

#查看全局所有配置
git config --global --list

#查看全局user email
git config --global user.name
git config --global user.email

#查看局部所有配置
git config --global --list

#查看局部user email
git config user.name
git config user.email

git basic workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#克隆新仓库
git clone https://github.com/kezunlin/demo.git
git remote -v
git add .
git commit -am "init repo"
git push origin master

#或者提交已有仓库
git init .
git remote rm origin
git remote add origin https://github.com/kezunlin/demo.git
git remote -v
git pull origin master

git add .
git commit -am "init repo"
git push origin master

git branch workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#比如,我需要开发一个newfeature分支进行开发,具体流程如下
#1)添加分支
cd demo
git checkout -b newfeature

#2)现在已经从master分支切换到newfeature分支,在newfeature分支添加若干代码和文件
touch main.cpp

#3)提交到本地
git ls-files # check which files are included in git

git add .
git commit -am 'update in newfeature branch'

#4)同步gitlab的最新master分支到本地
git checkout master
git pull origin master

#> 可以看到,同步master分支之后,gitlab上的最新文件和代码会同步到本地的master分支。

#5)合并master分支到自己的newfeature分支
git checkout newfeature
git merge master

output
Already up-to-date!
Merge made by the 'recursive' strategy.

#>可以看到master分支被成功合并进了newfeature分支。
#>合并过程可能会有冲突,根据提示需要先修复冲突文件,然后在本地提交之后,推送到gitlab的newfeature分支。

#5.1) 合并之后没有冲突,对自己的newfeature分支进行再次测试,确保没有问题之后推送到gitlab。
#5.2) 合并之后有冲突,请按照提示逐个修复冲突文件,冲突修复完毕,对自己的newfeature分支进行再次测试,确保没有问题之后推送到gitlab。

git add .
git commit -am 'fix some conflicts'

#6)推送newfeature分支到gitlab
#>现在gitlab上的最新代码已经同步到本地master并且合并进了newfeature分支,并且解决了冲突,通过了测试,现在需要将newfeature分支推送到gitlab

git push -u origin newfeature

#> 推送成功

#7)在gitlab上合并newfeature分支到master
#7.1)打开demo git可以看到新建的分支newfeature,点击merge request
#7.2)发起submit merge request请求
#7.3)进入到merge页面,等待merge请求通过。
#7.4)maintainer通过了merge请求,自己的分支newfeature成功合并到master分支.

#> 如果此处合并失败,请回到(4),(5),(6)步骤重试。

git tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
git tag -a V1.2 -m 'release 1.2'

git tag

git show V1.2

# push local tags to remote
git push origin V1.2
git push origin --tags

# delete local and remote tag
git tag -d V1.2
git push origin :refs/tags/V1.2

# fetch remote tag
git fetch origin tag V1.2
```

### multiple github workflow

#### ssh-keygen
```bash
#为git配置多个用户和key
#账号1
ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kezunlin/.ssh/id_rsa): id_rsa_kezunlin1

#账号2
ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kezunlin/.ssh/id_rsa): id_rsa_kezunlin2


#添加这些key到SSH agent中
ssh-add ~/.ssh/id_rsa_kezunlin1
ssh-add ~/.ssh/id_rsa_kezunlin2

#>因为默认只读取id_rsa

edit config

edit ~/.ssh/config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#配置多个github账号
#默认
Host github.com
User git
Hostname github.com
IdentityFile ~/.ssh/id_rsa

#用户kezunlin1
Host kezunlin1.github.com
User kezunlin1
Hostname github.com
IdentityFile ~/.ssh/id_rsa_kezunlin1

#用户kezunlin2
Host kezunlin2.github.com
User kezunlin2
Hostname github.com
IdentityFile ~/.ssh/id_rsa_kezunlin2

upload public keys

  1. 将id_rsa_kezunlin1.pub添加到kezunlin1的github账号中
  2. 将id_rsa_kezunlin2.pub添加到kezunlin2的github账号中

test ssh

1
2
3
4
5
ssh -T [email protected]
Hi kezunlin! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T [email protected]
ssh -T [email protected]

clone repo

1
2
3
4
5
6
7
8
9
10
11
#错误方法

git clone [email protected]:kezunlin1/demo.git
git clone [email protected]:kezunlin2/demo.git

Cloning into 'demo'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

when clone repos we must specify
[email protected]:kezunlin1/demo.git or
[email protected]:kezunlin2/demo.git

1
2
3
#正确方法
git clone [email protected]:kezunlin1/demo.git
git clone [email protected]:kezunlin2/demo.git

Git LFS

Git LFS lets you store files up to 2 GB in size instead of normal 50 MB.

install

download git-lfs-linux-amd64-v2.7.2.tar.gz from git lfs

1
2
3
4
tar -xzvf git-lfs-linux-amd64-v2.7.2.tar.gz
chmod +x install.sh
sudo ./install.sh
#Git LFS initialized.

usage

To get started with Git LFS, the following commands can be used.

  1. Setup Git LFS on your system. You only have to do this once per repository per machine:

    git lfs install

  2. Choose the type of files you want to track, for examples all ISO images, with git lfs track:

    git lfs track “*.iso”

  3. The above stores this information in gitattributes(5) files, so
    that file need to be added to the repository:

    git add .gitattributes

  4. Commit, push and work with the files normally:

    git add file.iso
    git commit -m “Add disk image”
    git push

issue the commands

>git lfs install
Updated git hooks.
Git LFS initialized.

>git lfs track "*.pdf" 
# generate .gitattributes file

>cat .gitattributes
*.pdf filter=lfs diff=lfs merge=lfs -text

>git add .gitattributes

>git add file.pdf
>git commit -m "Add pdf file"
>git push

Reference

History

  • 20190313: created.
  • 20191127: add multiple workflow