git(gerrit) 常用命令

上一篇 / 下一篇  2021-08-24 09:20:28 / 个人分类:SoftTest

51Testing软件测试网1oIx;B e.^y

S-_0@6@&@!x4qR0

6jOH_'P#Q3C8Z0
A. 通过windows shell设置用户名和邮箱:
git config --global user.name "XXX"
git config --global user.email "XXX@xxx.com"
B.生成win公钥,继续通过windows shell窗口输入:
ssh-keygen -t rsa -C "XXX@xxx"
  • 在上条工作目录下打开.ssh文件夹后查看并复制公钥;
  • 打开远程仓库,将复制的公钥填入 settings --> SSH Keys
C. 首次clone: 从远程服务器上查看到 Clone -- Clone with ssh 从而找到clone的命令。
本地执行:git clone git@git.xx.x.com/x/x/x.git
git checkout yourbranch

t8j UY@t-vw.t0

I*@ M_ QZ-ep*|3zH0
  1. 1. Make the changes to the files on your machine51Testing软件测试网i n m3lAE

    git add <file name>
    git add .      # 全部提交到缓存区

4a__ut xL'hY0
  1. 2. If you are making a new commit use51Testing软件测试网lMj,Z Q#^}8y4H

  2.     git commit51Testing软件测试网OK*tF'b+u kC%{

  3. 51Testing软件测试网 \,?(Z,W:B/@;B)m/f
    51Testing软件测试网 ]3v;yL2r$q2^*c$[

  4. Or to amend an existing commit use

    x^ tj)|(Uj8n0
  5.     git commit --amend51Testing软件测试网V/l.C8}C2y/}5j.wu

  6. 51Testing软件测试网,G-d&H2]'Z_

    .api|n C'aNi0`0
  7. Please make sure you add a commit message as it becomes the description for your change.

    :YZ5J!G[0

*^g3i&a?k.[q0
在commit的时候,发现提交者的名称和email都是错误的,这时需要先配置成当前的用户,以及对应的email
1
2
$ git config user.name test
$ git config user.email test@xxx.com

  1. d;Czhc(Y Ps6}"\051Testing软件测试网'|7V#pB3K(?+E/cm]

  2. 3. Push the change for code review

    En$WY2Ul;sv0
  3.     git push origin HEAD:refs/for/master51Testing软件测试网5UPY!_GP


  4. HQtk0Ux0

    8xh"t#d0i?0
  5. Close this dialog and you should be able to see your recently created change in the 'Outgoing changes' section on the 'Your changes' page.

    ,A/SG3D+o#Qn7\0

6X&[7zqj+|-c@,]0
本地显示配置信息:
    git config --list

1Gfd\a0
4. Branch:
    git checkout -b <your-branch-name >       #创建一个新的分支同时切换到新创建的分支

.hE_|T9?0

DwI1u'H*M0
Q1: push远程时被拒绝! [remote rejected] HEAD -> refs/for/master (no new changes)
分析:
no new changes 表示本地分支和远程分支代码没有差别,也就是没有新的提交。
Gerrit审核根据commit id 和 changeId来判断是不是新的提交,dev分支merge到master分支后,在master上可能没有生成新的commit id和changeId。也就是两个分支的commit id和changeId都相同,在master分支推送到Gerrit的时候,Gerrit判断内容相同,因此拒绝push。
51Testing软件测试网z~INKVc,`p \
方法一:通过git commit --amend生成新的changeId,然后再push;

方法二:在merge的时候加上--no-ff参数。51Testing软件测试网vH E#X^)j)q

--no-ff:不使用fast-forward方式合并,合并的时候会创建一个新的commit用于合并。

P Y*_)?q3[V0
一、创建版本库

初始化一个Git仓库,使用git init命令。或者直接从远程库 git clone.51Testing软件测试网3L-K'] UF8|]Q

添加文件到Git仓库,分两步:51Testing软件测试网 W1R_Y-x'sY

  1. 使用命令git add <file>,注意,可反复多次使用,添加多个文件;
  2. 使用命令git commit -m <message>,完成。

!~/b#BM'Xr!^/_0
二、版本回退
  • HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id51Testing软件测试网*k:D8?&Dl

  • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。51Testing软件测试网yw5ZA E3R k#@"yUNP

  • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

    +aey@8f9JGS0

)zLs&M"Amg0
三、版本修改

    每次修改,如果不用git add到暂存区,那就不会加入到commit中。


.V#]:R(}Z|1T5AI2m0
四、撤销修改

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file51Testing软件测试网LOj(maNC.[6?

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。51Testing软件测试网iDc'zXr8vqUQ.C-p3a

五、删除文件

;_2i"\3|E.J0

命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容51Testing软件测试网J\%s'~ ?#{\jl

把误删的文件恢复到最新版本:$git checkout -- test.txt51Testing软件测试网 fwn9bg

六、添加远程库

gap2Bf:xx0

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

2nr#yD ?^q0

关联一个远程库时必须给远程库指定一个名字,origin是默认习惯命名;51Testing软件测试网v,]n(dbrw

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;51Testing软件测试网 bZ ys3H G6Xe^

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改

5ct!p4R&D|x\Vi0


TAG:

 

评分:0

我来说两句

Open Toolbar