记录下James工作中遇到的问题:
1. 在app目录下提交.cfg特制化文件,此时Git和Gerrit结合使用;
2. 对修改文件追加提交;
3. 查看当前目录的所有分支,包括:本地分支和远程分支;
4. 根据远程分支创建分支,并查看所有分支与远程分支的对应关系;
5. 切换分支前,保存当前分支的修改;并在返回时,恢复修改;
6. 在本地文件系统中,建立远程仓库并克隆仓库,在本地仓库中提交更新;
7. 删除服务器端文件内容,并提交删除修改;
上述问题分析:
1. 在app目录下提交.cfg特制化文件,使用场景:Git和Gerrit结合使用;要知道Gerrit是代码审核的工具。
提交代码和文件(是app文件夹下的内容),需要进入.git目录,执行以下步骤:
1. (首先需要查看哪些文件被改动)git add assets/cfg_k86s7_KST-T5.cfg
2. git st // 查看add指令执行结果(优先检测仓库是否有更新)git fetch // 检测远程是否有更新,如果有更新(增加了新的代码)-> 需要先执行 git pull
3. git ci -m "add cfg_k86s7_KST-T5.cfg" // commit 相当于提交命令;其后代表此次的注释
4. git lg // 查看log,检查此次的指令执行结果;
5. git push origin HEAD:refs/for/master //提交至服务器,需要审核!
6. 检测是否push到远程分支,查看gerrit2.y/服务器;
上述最需要解答的疑问是:为什么需要使用“git push origin HEAD:refs/for/master”。既然是使用Gerrit作为代码审核工具,就需要遵循push的规范;上述提交指令就是提交审核的规范。
2. 对已经push到Gerrit服务器上的Commit追加提交。
1. mv assets/cfg_k86s7_HC_CDJ7801.cfg(source) assets/cfg_k86s7_HC-CDJ7801.cfg(destination) (重命名文件)
2. git add assets/
3. git commit assets/ --amend
4. git push origin HEAD:refs/for/master
更新提交内容,可能的使用场景有:再次修改文件;增加修改...对“git commit ...”增加参数:—amend即可实现commit修改。
3. 查看当前目录的所有分支,包括:本地分支和远程分支。
git branch -a
上述指令不仅可以获取到本地分支情况,还可以获取到远程分支情况。
git branch -vv
上述指令则可以获取当前HEAD指向的远程分支情况。
4. 根据远程分支创建分支,并查看所有分支与远程分支的对应关系。
Administrator@ZHANGFENG /f/sptSrcGit/FactoryTest (develop)
$ git branch -a
* develop
remotes/origin/HEAD -> origin/master
remotes/origin/develop
remotes/origin/mx1/teyes/t7
remotes/origin/test
Administrator@ZHANGFENG /f/sptSrcGit/FactoryTest (develop)
$ git checkout -b mx1/teyes/t7 origin/mx1/teyes/t7
Branch mx1/teyes/t7 set up to track remote branch mx1/teyes/t7 from origin.
Switched to a new branch 'mx1/teyes/t7'
Administrator@ZHANGFENG /f/sptSrcGit/FactoryTest (mx1/teyes/t7)
$ git status
On branch mx1/teyes/t7
Your branch is up-to-date with 'origin/mx1/teyes/t7'.
nothing to commit, working directory clean
使用“git checkout -b ...”,参数-b则是创建分支;上述指令达到创建并切换分支的目的。
5. 切换分支前,保存当前分支的修改;并在返回时,恢复修改。
Administrator@ZHANGFENG /f/sptSrcGit/FactoryTest (develop)
$ git stash
Saved working directory and index state WIP on develop: 9c721b0 解决无SN号或IMEI号时,可能引发的崩溃问题;解决GPS测试项中点击HOME按键,导致其他测试项结果错误问题;增加ADAS测试项
HEAD is now at 9c721b0 解决无SN号或IMEI号时,可能引发的崩溃问题;解决GPS测试项中点击HOME按键,导致其他测试项结果错误问题;增加ADAS测试项
Administrator@ZHANGFENG /f/sptSrcGit/FactoryTest (develop)
$ git stash pop
On branch develop
Your branch is up-to-date with 'origin/develop'.
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: .classpath
modified: local.properties
modified: src/com/spt/view/SptActivity.java
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (44cfaa42768ef50d6b82db4c2e8d2dee508b00b4)
上述指令则是执行:保存修改,恢复修改的作用。
6. 在本地文件系统中,建立远程仓库并克隆仓库,在本地仓库中提交更新。
步骤1:创建本地分支
james@james-PC MINGW64 /d/GitDemo
$ cd remoteGit/
james@james-PC MINGW64 /d/GitDemo/remoteGit
$ git init --bare
Initialized empty Git repository in D:/GitDemo/remoteGit
步骤2:在本地创建克隆上述仓库(远程仓库),并作为本地仓库使用
james@james-PC MINGW64 /d/GitDemo/demo
$ git clone ../remoteGit/
Cloning into 'remoteGit'...
warning: You appear to have cloned an empty repository.
done.
james@james-PC MINGW64 /d/GitDemo/demo
$ ls
remoteGit/
步骤3:在上述本地仓库中,创建a.txt文件,并提交到远程分支
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ echo 111 >> a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ cat a.txt
111
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (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: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git add .
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ ls
a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ cat a.txt
111
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ ls
a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (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: a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git commit -m "add 111 to a.txt"
[master 07e3fc3] add 111 to a.txt
1 file changed, 1 insertion(+)
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git push origin HEAD
Counting objects: 3, done.
Writing objects: 100% (3/3), 256 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To D:/GitDemo/demo/../remoteGit/
bdaf647..07e3fc3 HEAD -> master
上述使用 git push origin HEAD,其中:HEAD指向的是本地的当前分支master(如果当前分支是develop,则HEAD则代表develop分支);表示:将本地工作分支推送到远程的该工作分支(master或其他分支)。
步骤4:在本地仓库中,根据远程的master分支新建develop_test分支,并提交更新到远程的master分支中
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git checkout -b develop_test origin/master
Branch develop_test set up to track remote branch master from origin.
Switched to a new branch 'develop_test'
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ ls
a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ cat a.txt
111
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ cat 222 >> a.txt
cat: 222: No such file or directory
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ echo 222 >> a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git status
On branch develop_test
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: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git add .
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git commit -m "add 222 to a.txt in develop_test branch"
[develop_test c522e2e] add 222 to a.txt in develop_test branch
1 file changed, 1 insertion(+)
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git branch -vv
* develop_test c522e2e [origin/master: ahead 1] add 222 to a.txt in develop_test branc h
master 07e3fc3 [origin/master] add 111 to a.txt
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git push origin HEAD:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To D:/GitDemo/demo/../remoteGit/
07e3fc3..c522e2e HEAD -> master
上述使用: git push origin HEAD:master 表示将当前工作分支develop的更新,提交到远程的master分支。
步骤5:验证远程的master已改变
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (develop_test)
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git fetch
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ git pull
Updating 07e3fc3..c522e2e
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
james@james-PC MINGW64 /d/GitDemo/demo/remoteGit (master)
$ cat a.txt
111
222
下述是本地仓库中保存的.git目录:
james@james-PC MINGW64 /d/GitDemo/localGit/remoteGit (master)
$ ls -al
total 5
drwxr-xr-x 1 james 197121 0 十一 20 08:41 ./
drwxr-xr-x 1 james 197121 0 十一 20 08:37 ../
drwxr-xr-x 1 james 197121 0 十一 20 08:41 .git/
-rw-r--r-- 1 james 197121 10 十一 20 08:41 a.txt
或者使用另外一种方式验证:
james@james-PC MINGW64 /d/GitDemo
$ cd remoteGit/
james@james-PC MINGW64 /d/GitDemo/remoteGit (BARE:master)
$ git log
commit c522e2e50b79aac1be118b48ca7629f38356b296
Author: ZHANGEfeng-james <zfengwust3054@163.com>
Date: Sat Nov 19 23:36:22 2016 +0800
add 222 to a.txt in develop_test branch
commit 07e3fc35d829b9cd6ff311a6a525b97d1e501612
Author: ZHANGEfeng-james <zfengwust3054@163.com>
Date: Sat Nov 19 23:09:47 2016 +0800
add 111 to a.txt
commit bdaf647cfa610d73733060ae46c4c1ee8e3857b0
Author: ZHANGEfeng-james <zfengwust3054@163.com>
Date: Sat Nov 19 23:05:20 2016 +0800
init master
如上所示,即实现了远程分支和本地分支的创建和提交操作。
7. 删除服务器端文件内容,并提交删除修改
Administrator@ZHANGFENG /f/sptSrcGit/CarDoc (master)
$ git rm 应用开发一部/设计文档/工厂测试程序设计文档_张峰_11.21.doc
rm '应用开发一部/设计文档/工厂测试程序设计文档_张峰_11.21.doc'
Administrator@ZHANGFENG /f/sptSrcGit/CarDoc (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)
deleted: 测试程序设计文档_11.21.doc
Administrator@ZHANGFENG /f/sptSrcGit/CarDoc (master)
$ git commit -m "删除测试程序设计文档,已更新至24日文件"
[master 659232c] 删除测试程序设计文档,已更新至24日文件
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 测试程序设计文档_11.21.doc
Administrator@ZHANGFENG /f/sptSrcGit/CarDoc (master)
$ git push origin HEAD:refs/for/master
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 503 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2)
remote: Processing changes: new: 1, refs: 1, done
remote:
remote: New Changes:
remote: http://gerrit.y/3244 删除测试程序设计文档,已更新至24日文件
remote:
To ssh://zhangfeng@gerrit.y:29419/yunovo_packages/CarDoc
* [new branch] HEAD -> refs/for/master
涉及到的git指令:git rm <file-name>。