场景一:首先我们要down下整个项目的代码或者上传整个项目的代码
a.上传代码
首先,我们要把已经创建的git分支down下来
git clone ssh://pan_zhongjian@127.0.0.1:29418/3559c
-b dev-20181012
&& scp -P 29418 pan_zhongjian@127.0.0.1:hooks/commit-msg 3559c/.git/hooks/
看下这个地址,这个地址是从gerrit上找到的。
-b dev-20181012 是某个分支,不写就默认是master分支。
然后,把这个地址拷贝到git窗口中运行即可。
再到下载下来的文件夹中,把我们的项目拷贝进去,进行提交代码。
然后,提交代码
在下载下来的文件夹中,打开git命令窗口。
git init //初始化
git add . //添加所有
git commit -m “first commit” //提交 并备注,双引号内容是备注
git remote add origin https://github.com/yanpenggmx/object_yes.git
git push -u origin master //push
b.下载代码
git clone ssh://pan_zhongjian@127.0.0.1:29418/3559c -b dev-20181012 && scp -P 29418 pan_zhongjian@127.0.0.1:hooks/commit-msg 3559c/.git/hooks/
这个地址拷贝到git中,运行,就可以下载整个项目到本地。
场景二:开发中提交代码,包括新增的文件,修改的文件,删除的文件。
git status
git add xxx xxx xxx —xxx代表提交的文件全路径,多个文件用空格隔开即可。
git commit -m “添加你的提交说明”
git push origin HEAD:refs/for/master —因为用gerrit,所以必须加 HEAD:refs/for/…
场景三:解决冲突的代码
git stash —把本地代码拿出来保存到另外一个地方
git pull —把服务器的代码down下来
git stash pop —合并刚才拿出来的代码和down的代码
然后用eclipse检查,解决冲突,再提交代码,走场景二。
场景四:回退代码
a.整个项目回退到某个版本
#回退所有内容到上一个版本
git reset HEAD^
#回退a.py这个文件的版本到上一个版本
git reset HEAD^ a.py
#向前回退到第3个版本
git reset –soft HEAD~3
#将本地的状态回退到和远程的一样
git reset –hard origin/master
#回退到某个版本 ——-这个比较实用
git reset 057d
#回退到上一次提交的状态,按照某一次的commit完全反向的进行一次commit
git revert HEAD
场景五:回退单个指定的文件到某个版本
参考:https://www.cnblogs.com/softidea/p/4920429.html
git checkout d98a0f565804ba639ba46d6e4295d4f787ff2949 src/main/main.c
示例
第一步: 在命令行中输入 git log src/main/main.c 得到该文件的commit 历史。 会得到类似下面的界面
第二步: 复制需要回退版本的hash,在此假设我们回退到 d98a0f565804ba639ba46d6e4295d4f787ff2949 ,则复制该序列即可
git reset d98a0f565804ba639ba46d6e4295d4f787ff2949 src/main/main.c
第三步:checkout 对应版本。格式为 git checkout , 在此即为命令行中输入 git checkout d98a0f565804ba639ba46d6e4295d4f787ff2949 src/main/main.c
场景六:打开图像化界面
gitk
场景七:比对文件
参考:https://www.cnblogs.com/taohuaya/p/11107264.html
查看工作目录与本地仓库有哪些代码文件不同,-w选项忽略代码文件由空格变化引起的不同
git diff –stat -w
比较工作目录与本地仓库中的文件fm-stat.c,忽略空格引起的不同
git diff -w src/fm-stat.c
查看每次详细修改内容的diff
git log -p src/fm-stat.c
拒绝本地修改,使用版本库更新工作目录
git checkout -- src/fm-stat.c
交互方式更新工作目录中的fm-stat.c文件,对每一个不同的代码都进行单步接收修改或者拒绝修改
git checkout -p src/fm-stat.c
作用同上,只不过fm-stat.c的更新源来自版本库的33f84版本,而不是版本库的当前版本
git checkout -p 33f84 src/fm-stat.c
场景八:gerrit 出现 submit include parents问题的解决办法
出现问题原因:commit相互依赖。具体讲就是:gerrit上已经存在commit A(commit A还未merge入库),然后你在commit A的代码基础上进行了修改(划重点,基于A修改!),并做了新的commit B,commit B已经包含了commit A的修改,于是在gerrit 上abondon commit A,只留下commit B在gerrit上,这样一来,commit B review通过后做merge时你就会得到标题中的错误。
方法一:在网页上abandon掉,然后reset到abandon前的那个版本即可。
git reset –hard ec04d0196075fb38b005a08ddffc8a072cb95911
这个版本号要是最新提交成功的那一笔commitid才行。
可以再gerrit页面上的merged列表中查找到最近一次提交成功的commitid。
用git log 可能会找错。
方法二:(没能够从根本上解决问题,当你再次回到master分支进行代码提交的时候,还是这个问题)
解决方法:
1. 从远程分支上重新创建一个新的工作分支:git fetch origin master(远程分支):new_work(新分支)
2. 切换到新的工作分支:git checkout new_work
3. 将commit B 移到新分支上(gerrit 页面右上角download中直接复制cherry-pich命令):git fetch ssh://xxx xxx && git cherry-pich xxx
4.正常解决冲突流程,不做复述
5.正常提交代码:git push origin HEAD:refs/for/mater(需要提交到的分支)
4. 刷新gerrit,重新做code review。
实例操作:
git fetch origin master:new_work
git checkout new_work
git fetch ssh://xxx@10.10.10.10:29418/xxxKIceyyy refs/changes/78/4378/1 && git cherry-pick FETCH_HEAD
然后回归到eclipse中,检查相应的文件,解决冲突,然后再正常提交。
其中,这第三步的地址的获取,要仔细看文字说明:gerrit 页面右上角download中直接复制cherry-pich命令
场景九:git add 添加错文件后撤销
这样的错误是由于, 有的时候 可能
git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件
git add 如果添加了错误的文件的话
撤销操作
git status 先看一下add 中的文件
git reset HEAD 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了
git reset HEAD XXX/XXX/XXX.java 就是对某个文件进行撤销了
如果是第一次提交源码,可能会报错
这个时候,git reset HEAD 就失灵了。
我们需要用
git rm -r --cached . //撤销所有add 的文件及文件夹
才能撤销add操作
场景十:本地修改,导致和服务器代码冲突,拉取代码失败后,用服务器代码覆盖本地代码的操作。
场景三的方法有时会失灵,具体为啥,我也不清楚。在场景三失败的情况下,才考虑该办法
git status ---查看本地哪些文件修改过
git diff ---比对下修改的文件代码和服务器上的代码区别
git checkout -- . ---让本地所有修改的文件回退到最近意思git commit 或者 git add时 的状态
git pull --rebase --- https://www.cnblogs.com/kungfupan/p/9967531.html
用在合并代码的时候其作用就是在一个随机创建的分支上处理冲突,避免了直接污染原来的分区
说明:
场景十一:pull时候报错
解决办法:
1. git add .
2. git commit -m "xxxx"
3. git pull
场景十二:git push 报错:missing Change-Id in commit message footer
参考:https://www.cnblogs.com/zndxall/p/9603834.html
(1) git reset –soft commitid3
(2) git status 可以看到绿色的已经add过的文件(即commitid1和commitid2的改动)
(3) git commit -s #添加评论,保存退出后会生成change_id
(4) git log 可以看到已经有了change_id
(5) git push origin HEAD:refs/for/工作分支
场景十三:Git diff ^M的消除
下面简单的方法可以让git diff的时候忽略换行符的差异:
git config –global core.whitespace cr-at-eol
场景十四:撤销commit操作
git reset –soft HEAD^
场景十五:push时报错,没有change-id
解决办法就在remote提示中:
执行下提示中的两条命令即可,然后,再进行push命令
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 xxxx@1.1.1.1:hooks/commit-msg ${gitdir}/hooks/
remote: git commit --amend
场景十六:abandon操作
我们再gerrit上进行了abandon后,需要把本地代码进行reset,否则下次提交报错
559f70c8bd658243c4f956a38837c241cf150e43 这个是上一笔成功提交的commit_id
可以在gerrit上查找 也可以用命令查找
git log -3
git reset --soft 559f70c8bd658243c4f956a38837c241cf150e43
源码操作记录:
$ git log -3
commit cafb47047461476dcf2801cf581aeee933aad0d2 (HEAD -> master)
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Mon Jun 22 16:00:53 2020 +0800
修改代码import * 问题
Change-Id: I69bcbb50040531119158486a465ee1c3a0865a7a
commit 559f70c8bd658243c4f956a38837c241cf150e43
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Mon Jun 22 09:57:35 2020 +0800
影藏赋权功能的两个模块
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git reset --soft 559f70c8bd658243c4f956a38837c241cf150e43
场景十七:提交的代码在gerrit上verify不通过
我们提交代码后,选择review的人,有时候被验证不通过
git commit --amend
重新在原提交记录上,修改msg,再次提交,但是在gerrit上只有一笔记录
源码操作记录:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/main/java/com/asgardiot/service/account/service/impl/PermissionAllServiceImpl.java
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: src/main/resources/application.properties
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git add src/main/java/com/asgardiot/service/account/service/impl/PermissionAllServiceImpl.java
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/main/java/com/asgardiot/service/account/service/impl/PermissionAllServiceImpl.java
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: src/main/resources/application.properties
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git commit --amend
[master c4df53b] [DEV]影藏赋权功能的两个模块,修改了import * 的问题
Date: Mon Jun 22 09:57:35 2020 +0800
1 file changed, 11 insertions(+), 1 deletion(-)
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git log -2
commit c4df53b32f3367089bd2647ebf8f9c126f5946aa (HEAD -> master)
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Mon Jun 22 09:57:35 2020 +0800
[DEV]影藏赋权功能的两个模块,修改了import * 的问题
Change-Id: I7d9b3a98bd3bb4e40714cb1d2a3a8ebd77103c4e
commit 8be842fd213c1f75045dc357716b540203a488c3
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Fri Jun 19 09:52:39 2020 +0800
baseservice 添加getId方法,部门树根目录查询项目数据功能
Change-Id: Idc394d3929856e0dda4cf503ee7a909766b25395
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git push origin HEAD:refs/for/master
Enumerating objects: 55, done.
Counting objects: 100% (55/55), done.
Delta compression using up to 8 threads
Compressing objects: 100% (25/25), done.
Writing objects: 100% (37/37), 4.07 KiB | 521.00 KiB/s, done.
Total 37 (delta 16), reused 0 (delta 0)
remote: Resolving deltas: 100% (16/16)
remote: Processing changes: refs: 1, updated: 1, done
remote:
remote: SUCCESS
remote:
remote: http://amoi.tpddns.cn:8081/c/asgard/+/1303 [DEV]影藏赋权功能的两个模块,修改了import * 的问题
remote:
To ssh://amoi.tpddns.cn:29418/asgard
* [new branch] HEAD -> refs/for/master
场景十八:服务器上abandon后,本地依然和abandon的记录比对
解决办法,本地需要reset到abandon之前的记录才可以
git reset –soft 8be842fd213c1f75045dc357716b540203a488c3
这个命令是回退到这个版本,但是保留本地修改的代码,并自动添加到缓冲区stage状态
一般默认不加参数即可,修改的代码会保留,并且,没有放入缓存区stage状态
源码操作记录:
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git log -3
commit c4df53b32f3367089bd2647ebf8f9c126f5946aa (HEAD -> master)
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Mon Jun 22 09:57:35 2020 +0800
[DEV]影藏赋权功能的两个模块,修改了import * 的问题
Change-Id: I7d9b3a98bd3bb4e40714cb1d2a3a8ebd77103c4e
commit 8be842fd213c1f75045dc357716b540203a488c3
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Fri Jun 19 09:52:39 2020 +0800
baseservice 添加getId方法,部门树根目录查询项目数据功能
Change-Id: Idc394d3929856e0dda4cf503ee7a909766b25395
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git reset --soft 8be842fd213c1f75045dc357716b540203a488c3
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard/account-service (master)
$ git log -2
commit 8be842fd213c1f75045dc357716b540203a488c3 (HEAD -> master)
Author: pan_zhongjian <pan_zhongjian@hoperun.com>
Date: Fri Jun 19 09:52:39 2020 +0800
baseservice 添加getId方法,部门树根目录查询项目数据功能
Change-Id: Idc394d3929856e0dda4cf503ee7a909766b25395
commit 9b7b8ff59252dc1f037b6f91222b8474e84457fe (origin/master, origin/HEAD)
Author: wu_chenyang <wu_chenyang@hoperun.com>
Date: Sat May 16 18:30:42 2020 +0800
[ASGARD][MFS]添加数据库文件
Change-Id: Iade6c4997f28c1d593cb9db74e21ca4028c4439a
场景十九:Git stash pop异常
The stash entry is kept in case you need it again
我的解决办法:
报这种错误,一般是pull代码后,报了冲突,我们直接在代码文件中解决了冲突
然后,执行
git stash pop 时候,报错
此时解决冲突的文件状态都是红色的,其他修改的文件变成了绿色
我执行了下
git reset HEAD .
问题就解决了。
网上解决办法:
原因是:该文件的代码已被更改,而远程仓库的代码没有改动,git pull 拉下代码后,执行git stash pop发现该文件不一样,导致无法从缓冲区里提取代码。
解决方法:将该文件copy一份,然后从项目中删除,再执行git stash pop,之后在对文件进行修改即可。
源码操作记录:
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git stash pop
Auto-merging account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java
CONFLICT (content): Merge conflict in account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java
Auto-merging account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java
CONFLICT (content): Merge conflict in account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java
Auto-merging account-service/src/main/java/com/asgardiot/service/account/controller/PerformanceEvaluationCrudController.java
The stash entry is kept in case you need it again.
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git stash pop
account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java: needs merge
account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java: needs merge
The stash entry is kept in case you need it again.
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: account-service/src/main/java/com/asgardiot/service/account/controller/PerformanceEvaluationCrudController.java
modified: account-service/src/main/resources/application.properties
modified: view-service/config/index.js
modified: view-service/static/envs.js
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java
both modified: account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
account-service/src/main/java/com/asgardiot/service/account/bean/view/PerformanceEvaluationInfoQueryProgressView.java
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git stash
account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java: needs merge
account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java: needs merge
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git pull
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git stash pop
account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java: needs merge
account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java: needs merge
The stash entry is kept in case you need it again.
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git reset HEAD .
Unstaged changes after reset:
M account-service/src/main/java/com/asgardiot/service/account/controller/PerformanceEvaluationCrudController.java
M account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java
M account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java
M account-service/src/main/resources/application.properties
M view-service/config/index.js
M view-service/static/envs.js
pan_zhongjian@pc180720 MINGW64 /d/IDEAWorkspace/asgard (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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: account-service/src/main/java/com/asgardiot/service/account/controller/PerformanceEvaluationCrudController.java
modified: account-service/src/main/java/com/asgardiot/service/account/service/PerformanceEvaluationQueryService.java
modified: account-service/src/main/java/com/asgardiot/service/account/service/impl/PerformanceEvaluationQueryServiceImpl.java
modified: account-service/src/main/resources/application.properties
modified: view-service/config/index.js
modified: view-service/static/envs.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
account-service/src/main/java/com/asgardiot/service/account/bean/view/PerformanceEvaluationInfoQueryProgressView.java
no changes added to commit (use "git add" and/or "git commit -a")
场景二十:IDEA的Revert操作
等价于下面两条命令:
git -c core.quotepath=false rm --cached -f -- account-service/src/main/java/com/service/account/interceptor/xxx.java
git -c core.quotepath=false checkout HEAD -- account-service/src/main/java/com/service/account/interceptor/xxx.java