GIT命令以及在开发中的使用

SSH权限验证

生成ssh密钥(VSTS)

VSTS用户名 person@company.com
注意: 需要按三下enter使用默认的目录

heyw@ubuntu:~/code/go/MPS$ ssh-keygen -C "person@company.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/heyw/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/heyw/.ssh/id_rsa.
Your public key has been saved in /home/heyw/.ssh/id_rsa.pub.

查看密钥

目录以上述生成的目录为准

heyw@ubuntu:~/code/go/MPS$ cat /home/heyw/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkHAX2/HuFdfwwg+byZR...

添加密钥到VSTS

进入安全认证中心

在这里插入图片描述

添加SSH public keys

在这里插入图片描述
描述自定义,key data可填写上面提到的查看密钥,然后点击保存即可
在这里插入图片描述

克隆仓库

在VSTS中找到源

在这里插入图片描述

通过GIT命令克隆

heyw@ubuntu:~/code/go/abs$ git clone -v company@vs-ssh.visualstudio.com:v3/sbs/svs
Cloning into 'd-backend'...
The authenticity of host 'vs-ssh.visualstudio.com (40.81.25.218)' can't be established.
RSA key fingerprint is SHA256:fasdasdasdasdasdadadada.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'vs-ssh.visualstudio.com,40.81.25.218' (RSA) to the list of known hosts.
remote: Azure Repos
remote: Found 398 objects to send. (67 ms)
Receiving objects: 100% (398/398), 109.09 MiB | 8.34 MiB/s, done.
Resolving deltas: 100% (101/101), done.
Checking connectivity... done.

缓存克隆避免一次拉取失败

$ git clone --recursive https://github.com/spmallick/learnopencv.git

如果失败,那么进行文件夹中执行

$ git submodule update  --init --recursive

基本命令

http方式克隆现有的仓库

heyw@ubuntu:~/code/go/demo$ git clone -v https://github.com/sbinet/go-python.git

如果是通过https的方式克隆,并且需要记录用户名和密码,需要事先执行下面的一行代码,然后再克隆,克隆的时候输入一次用户名和密码之后,就会被保存下来,以后就不用再输入了。

$ git config --global credential.helper store

如果用户名和密码只想保留一定时间段,那么可以执行下面一行代码,3600为1 * 60 * 60即1个小时

$ git config credential.helper 'cache --timeout=3600'

初始化仓库

heyw@ubuntu:~/code/go/demo$ git init
Initialized empty Git repository in /home/heyw/code/go/demo/.git/
heyw@ubuntu:~/code/go/demo$ ls -la
total 28
drwxrwxr-x 3 heyw heyw 4096 Aug  1 10:34 .
drwxrwxr-x 4 heyw heyw 4096 Jul 30 17:21 ..
-rw-rw-r-- 1 heyw heyw  575 Jul 31 17:12 docker-compose.yaml
-rw-rw-r-- 1 heyw heyw   65 Jul 31 17:38 .env
drwxrwxr-x 7 heyw heyw 4096 Aug  1 10:34 .git
-rw------- 1 heyw heyw   21 Jul 31 09:53 go.mod
-rw-rw-r-- 1 heyw heyw  165 Jul 31 16:57 Makefile

检查当前仓库所有文件状态

通过该指令可以查看是否存在没有提交的文件
如果加上-s可以看到精简版的状态信息

heyw@ubuntu:~/code/go/demo$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.env
	Makefile
	docker-compose.yaml
	go.mod

跟踪一个新文件

如果使用*号代替具体文件名称,那么就是添加所有的文件

heyw@ubuntu:~/code/go/demo$ git add .env
heyw@ubuntu:~/code/go/demo$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   .env

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	Makefile
	docker-compose.yaml
	go.mod

忽略文件

如果我们不想要把一些文件添加到库里面,也不需要跟踪,那么我们可以添加.gitignore文件,把一些没有用的文件过滤掉

heyw@ubuntu:~/code/go/demo$ gedit .gitignore
heyw@ubuntu:~/code/go/demo$ cat .gitignore 
# ignore all .a files
*.a

# ignore all files in the build/ directory
build/

# ignore doc/*.txt, but not doc/server/hello.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

添加所有文件并重新查看

heyw@ubuntu:~/code/go/demo$ git add *
heyw@ubuntu:~/code/go/demo$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   .env
	new file:   .gitignore
	new file:   Makefile
	new file:   docker-compose.yaml
	new file:   go.mod
	new file:   test/f.txt

查看暂存文件更新的内容

heyw@ubuntu:~/code/go/demo$ git diff
diff --git a/test/f.txt b/test/f.txt
index 26ccfa3..cffe055 100644
--- a/test/f.txt
+++ b/test/f.txt
@@ -1,2 +1,5 @@
 asdhasdhau
+asdasd
+hello, bye
+

提交更新

配置用户名和邮箱

heyw@ubuntu:~/code/go/demo$ git config --global user.email "name@company.com"
heyw@ubuntu:~/code/go/demo$ git config --global user.name "name"

精简版提交方式

记得加上-a,不然会出现被删除的文件暂时没有被提交的问题

heyw@ubuntu:~/code/go/demo$ git commit -a -m "init code...."
[master (root-commit) ed9b747] init code....
 6 files changed, 57 insertions(+)
 create mode 100644 .env
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 docker-compose.yaml
 create mode 100644 go.mod
 create mode 100644 test/f.txt

复杂版提交方式

记得加上-a,不然会出现被删除的文件暂时没有被提交的问题

heyw@ubuntu:~/code/go/demo$ git commit -a
[master e77f4df] add file hello.txt to test 'commit'
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

进入命令窗口后,会有相关的更改提示,只需要在空白行写入提交我们自己的描述即可
修改完成使用快捷键ctrl+x退出,并输入y保存
在这里插入图片描述

撤销提交

如果提交之后发现有文件大于100M,无法传输到GITEE,可以撤销提交,删除大文件后再次提交
可通过git log -2查看提交记录,找到需要撤销的标签,然后执行下面的命令

git reset 315dde3a653c1a4fd3c452e53228d835e717de31

查看提交历史

加上-n可以查看指定数量的提交历史
如果需要显示每次commit的变化,可以加上–stat

heyw@ubuntu:~/code/go/demo$ git log
commit ffd52d613d0c19e2df36c399f696081905333be1
Author: name <name@company.com>
Date:   Thu Aug 1 11:43:05 2019 +0800

    delete test/f.txt

commit e77f4df9b975fd0b5d79aa9d48be18d798cb1a4c
Author: name <name@company.com>
Date:   Thu Aug 1 11:27:52 2019 +0800

    add file hello.txt to test 'commit'

commit ed9b747523307ad63bb68aed67df5fb6e023e01e
Author: name <name@company.com>
Date:   Thu Aug 1 11:24:42 2019 +0800

    init code....

图状查看提交历史

heyw@ubuntu:~/code/go/demo$ git log --pretty=format:"%h %s" --graph
* ffd52d6 delete test/f.txt
* e77f4df add file hello.txt to test 'commit'
* ed9b747 init code....

查看历史具体文件修改内容

-1 代表一条数据
-p 代表按补丁格式显示每个更新之间的差异

$ git log -1 -p srv/graphql/a.graphql

撤销更改文件

heyw@ubuntu:~/code/go/demo$ git checkout -- hello.txt
heyw@ubuntu:~/code/go/demo$ git status
On branch master
nothing to commit, working directory clean

撤销本地所有更改

$ git checkout .

撤销暂存区所有的更改

$ git reset HEAD *

撤销尚未添加的更改

 $ git clean -xdf

设置远程仓库并推送

在这里插入图片描述

heyw@ubuntu:~/code/go/demo$ git remote add origin hew@vs-ssh.visualstudio.com:v3/hyw/docker-demo/docker-demo
heyw@ubuntu:~/code/go/demo$ git push -u origin --all

拉取远程仓库中没有的文件

$ git fetch [remote-name]

heyw@ubuntu:~/code/go/demo$ git remote -v
origin	heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo (fetch)
origin	heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo (push)
heyw@ubuntu:~/code/go/demo$ git fetch origin
remote: Azure Repos
remote: Found 2 objects to send. (83 ms)
Unpacking objects: 100% (2/2), done.
From vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
   ffd52d6..355dc62  master     -> origin/master

注意:

$ git pull origin

等价于获取然后合并

$ git fetch origin
$ git merge

推送到远程仓库

$ git push [remote-name] [branch-name]

heyw@ubuntu:~/code/go/demo$ git push origin dev
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 218 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Analyzing objects... (2/2) (165 ms)
remote: Storing packfile... done (42 ms)
remote: Storing index... done (48 ms)
To heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
 * [new branch]      dev -> dev

查看某个远程仓库

heyw@ubuntu:~/code/go/demo$ git remote show origin
* remote origin
  Fetch URL: heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
  Push  URL: heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (local out of date)

查看分支

查看所有分支

heyw@ubuntu:~/code/go/demo$ git branch -a
* master
  remotes/origin/dev
  remotes/origin/master

查看远程分支

heyw@ubuntu:~/code/go/demo$ git branch -r
  origin/dev
  origin/master

查看本地分支

heyw@ubuntu:~/code/go/demo$ git branch
* master

拉取远程分支到本地

下面的方式会在本地创建一个分支nb并关联远程分支remote-branch
关联后会自动切换到nb这个分支上

heyw@ubuntu:~/code/go/demo$ git branch -a
* master
  remotes/origin/master
  remotes/origin/remote-branch
heyw@ubuntu:~/code/go/demo$ git checkout -b nb origin/remote-branch
Branch nb set up to track remote branch remote-branch from origin.
Switched to a new branch 'nb'
heyw@ubuntu:~/code/go/demo$ git branch -a
  master
* nb
  remotes/origin/master
  remotes/origin/remote-branch

创建分支

heyw@ubuntu:~/code/go/demo$ git branch
* master
heyw@ubuntu:~/code/go/demo$ git branch dev
heyw@ubuntu:~/code/go/demo$ git branch
  dev
* master

切换分支

heyw@ubuntu:~/code/go/demo$ git branch 
  dev
* master
heyw@ubuntu:~/code/go/demo$ git checkout dev
Switched to branch 'dev'
heyw@ubuntu:~/code/go/demo$ git branch
* dev
  master

删除分支

heyw@ubuntu:~/code/go/demo$ git branch -d dev
Deleted branch dev (was ffd52d6).

删除远程分支

heyw@ubuntu:~/code/go/demo$ git push origin --delete remote-branch
To heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
 - [deleted]         remote-branch

清理分支

如果远程分支不存在,但是在本地还存在,可通过下面的命令清除

$ git pull -p

启用可视化合并工具解决冲突

$ git mergetool

开发分支的使用

  1. 创建并切换到子分支dev
heyw@ubuntu:~/code/go/demo$ git branch 
* master
heyw@ubuntu:~/code/go/demo$ git branch dev
heyw@ubuntu:~/code/go/demo$ git branch 
  dev
* master
heyw@ubuntu:~/code/go/demo$ git checkout dev
Switched to branch 'dev'
heyw@ubuntu:~/code/go/demo$ git branch
* dev
  master
  1. 在分支上删除文件
    heyw@ubuntu:~/code/go/demo$ ls
    docker-compose.yaml  go.mod  hello.txt  hi.txt  Makefile
    heyw@ubuntu:~/code/go/demo$ rm hi.txt 
    heyw@ubuntu:~/code/go/demo$ git diff
    diff --git a/hi.txt b/hi.txt
    deleted file mode 100644
    index e69de29..0000000
  1. 提交并推送
heyw@ubuntu:~/code/go/demo$ git push origin dev
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 218 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Analyzing objects... (2/2) (165 ms)
remote: Storing packfile... done (42 ms)
remote: Storing index... done (48 ms)
To heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
 * [new branch]      dev -> dev
  1. 切换回主分支
heyw@ubuntu:~/code/go/demo$ git checkout master 
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
  1. 把子分支合并到主分支
heyw@ubuntu:~/code/go/demo$ git merge dev
Updating 355dc62..fdcc094
Fast-forward
 hi.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 hi.txt
  1. 删除子分支
heyw@ubuntu:~/code/go/demo$ git branch -d dev
  Deleted branch dev (was fdcc094).
heyw@ubuntu:~/code/go/demo$ git branch
  * master
  1. 删除远程分支
heyw@ubuntu:~/code/go/demo$ git push origin --delete dev
To heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/docker-demo
 - [deleted]         dev

更改远程仓库

  1. 删除当前远程源
  2. 添加新的源
  3. 把当前的所有内容推送给新的源
    heyw@ubuntu:~/code/go/demo$ git remote rm origin
    heyw@ubuntu:~/code/go/demo$ git remote add origin heyw@vs-ssh.visualstudio.com:v3/heyw/docker-demo/new_reps
    heyw@ubuntu:~/code/go/demo$ git push origin master
    Counting objects: 18, done.
    Compressing objects: 100% (13/13), done.

常见问题

  • RPC failed; curl 18 transfer closed with outstanding read data remaining
    解决办法:
  1. 如果是缓存区溢出
git config http.postBuffer 524288000
  1. 如果是网络问题
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
  1. 如果还是失败,可以考虑先从浅层克隆,再拉取
git clone --depth=1 http://gitlab.xxx.cn/yyy/zzz.git
git fetch --unshallow
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值