Git使用實例分析


記錄下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>。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM