Git 遇到的坑


1. 無法切換分支

$ git add .

$ git checkout function

error: Your local changes to the following files would be overwritten by checkout:
    SwiftLearn/ViewController.swift
Please, commit your changes or stash them before you can switch branches.
Aborting

原因:ViewController.swift 中的文件提交后,commit 沒有提交成功,需要提交成功,才能切換到其他支。

1 $  git commit -m "clear viewDidLoad"
2 
3 $ git checkout function
4 
5 
6 M    SwiftLearn.xcodeproj/project.pbxproj
7 M    SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate
8 Switched to branch 'function'

 

  1.1 git add 幾種區別

1      git add -A  提交所有變化
2 
3      git add -u  提交被修改(modified)和被刪除(deleted)文件,不包括新文件(new)
4 
5       git add .  提交新文件(new)和被修改(modified)文件,不包括被刪除(deleted)文件

注意:git init后新加的文件,先要git add才能納入git版本庫管理,否則新加文件為未跟蹤狀態。即:與 git 管理沒用,不搭關系

  1.2 git commit 區別

 

 1 //    對於已入版本庫並且改動的文件,可以使用git commit -am "message",新文件需要先入版本庫。
 2 //    也就是第一次你要 git add 將文件納入版本庫,后來再要修改不用 git add 了,直接用 git commit -am “123” 就可以了,將 git add省去了
 3 
 4 //   將一個文件 git add . 后,在對它修改
 5 
 6 //    第一種方法
 7 $    git commit -am "message"
 8 
 9 //    第二種方法
10 
11 $    git add .
12 
13 $    git commit -m "message"    

 

2. 解決沖突后,合並遇到問題

$ git merge function


Auto-merging 

SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate

CONFLICT (content): Merge conflict in 

SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate

Automatic merge failed; fix conflicts and then commit the result.

$ git branch -D function


//   當參數是 -d 時,對未合並的分支進行合並失敗 

$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
//   銷毀失敗。Git友情提醒,feature-vulcan分支還沒有被合並,如果刪除,將丟失掉修改,如果要強行刪除,需要使用命令git branch -D feature-vulcan。

//   現在我們強行刪除:

$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 756d4af).     

 

3. 合並后只剩一個 master 分支,切換到另一個分支上出錯

 1 $ git checkout -b develop
 2 
 3 xxx needs merge
 4 error: you need to resolve your current index first
 5 
 6 //    只有一個分支了,還怎么合並,可能沒提交,提交了下,成功了
 7 $ git branch
 8 * master
 9 
10 $ git commit -am "123"
11 [master 3cadb9c] 123
12 
13 $ git checkout -b develop
14 Switched to a new branch 'develop'

需要對合並后的分支進行提交,否則出錯

 

4.重復合並沖突

常規來說,你是把你開發的那條分支 develop 合並到 master ,但是當你把 master 合並到 你開發的 develop 上時,會報錯在終端,錯誤為沖突。

 1 $ git checkout -b developEnum
 2 
 3 $ git add .
 4 
 5 $ git commit -am "EnumFolderAndSwift"
 6 
 7 $ git merge master
 8 
 9 $ git checkout master
10 
11 $ git status
12 
13 On branch master
14 Your branch is ahead of 'origin/master' by 6 commits.
15 
16 $ git checkout developEnum
17 
18 $ git merge master
19 
20 error: Your local changes to the following files would be overwritten by merge:
21     SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate
22 
23 Please, commit your changes or stash them before you can merge.
24 
25 Aborting
26 
27 $ git commit -am "enum1"
28 
29 [developEnum 24d2739] enum1
30  1 file changed, 0 insertions(+), 0 deletions(-)
31 
32 $ git merge master 
33 
34 warning: Cannot merge binary files: SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate (HEAD vs. master)
35 
36 Auto-merging SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate
37 
38 CONFLICT (content): Merge conflict in SwiftLearn.xcodeproj/project.xcworkspace/xcuserdata/huanggang.xcuserdatad/UserInterfaceState.xcuserstate
39 
40 Automatic merge failed; fix conflicts and then commit the result.

 

 

解決方法,將已合並到 developEnumm 分支上的文件作為 master ,將其他不能合並的分支刪除掉,用 git branch -d <分支名> 是不行的,因為會提示你有沖突之類的,用強制刪除 git branch -D <分支名>

 

 1 $ git branch
 2 
 3  develop
 4 * developEnum
 5   master
 6 
 7 $ git branch -d develop
 8 
 9 error: The branch 'develop' is not fully merged.
10 If you are sure you want to delete it, run 'git branch -D develop'.
11 
12 $ git branch -D develop
13 
14 Deleted branch develop (was 1147815).
15 
16 // 將代碼上傳到服務器
17 
18 $ git push origin developEnum
19 
20 Counting objects: 77, done.
21 Delta compression using up to 4 threads.

 

 

5. 從服務器拉取不同分支報錯

 1 $ git pull
 2 
 3 error: Pull is not possible because you have unmerged files.
 4 hint: Fix them up in the work tree, and then use 'git add/rm <file>'
 5 hint: as appropriate to mark resolution and make a commit.
 6 fatal: Exiting because of an unresolved conflict.
 7 
 8 $ git branch
 9 
10 * developEnum
11 
12 $ git commit -am "over1"
13 
14 [developEnum 545beae] over1
15 
16 $ git pull
17 
18 There is no tracking information for the current branch.
19 Please specify which branch you want to merge with.
20 
21 // 報錯原因是沒有指定本地master和遠程origin/master的連接,執行git branch --set-upstream master origin/master,設置鏈接
22 
23 $ git branch --set-upstream developEnum origin/developEnum
24 
25 The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
26 Branch developEnum set up to track remote branch developEnum from origin.
27 
28 //    添加--set-upstream-to,這是肯比老方法,不對了
29 $ git branch --set-upstream-to developEnum origin/developEnum
30 fatal: branch 'origin/developEnum' does not exist   //不存在
31 
32 //   成功了!
33 $ git pull
34 Already up-to-date.
35 
36 //    第一種 :推送代碼到服務器,本地分支在 developEnum上
37 $ git push origin developEnum
38 
39 Counting objects: 14, done.
40 Delta compression using up to 4 threads.
41 
42 //    第而種 :推送代碼到服務器,git push origin local_branch:remote_branch,這個操作,local_branch必須為你本地存在的分支,remote_branch為遠程分支,如果remote_branch不存在則會自動創建分支。git push origin :remote_branch,local_branch留空的話則是刪除遠程remote_branch分支
43 $ git push origin developEnum:master 
44
45 Everything up-to-date

 

6. 從服務器拉代碼有兩種方式

  6.1  本地分支與服務器的分支相同,才能拉代碼

 1 $ git checkout -b master
 2 Switched to a new branch 'master'
 3 
 4 //    報錯
 5 $ git pull
 6 There is no tracking information for the current branch.
 7 Please specify which branch you want to merge with.
 8 
 9 //    正確從服務器拉代碼或者 git fetch origin xxx 等價於  git pull origin xxx
10 $ git pull origin master
11 From github.com:WindAndSand/SwiftLearn
12  * branch            master     -> FETCH_HEAD
13 Already up-to-date.
14 
15 $ git branch
16   developEnum
17 * master
18 
19 //    推送本地分支到遠程倉庫
20 
21 $ git push origin master
22 Total 0 (delta 0), reused 0 (delta 0)
23 To git@github.com:WindAndSand/SwiftLearn.git

 

   6.2 建立本地分支與服務器分支相連

//    查看遠程分支
$ git branch -r

  origin/baseOne
  origin/developEnum
  origin/master

$ git branch
  developEnum
* master

//    本地dev分支與遠程origin/dev分支的鏈接
$ git branch --set-upstream developEnum origin/developEnum

//    拉取代碼
$ git pull
From .
 * branch            developEnum -> FETCH_HEAD
Already up-to-date.

   6.2 在本地建一個分支與服務器進行映射,前提本地沒有一個與遠程服務器相同的分支

$ git branch -r

  origin/HEAD -> origin/master
  origin/MataRelease
  origin/a
  origin/correct
  origin/customupload
  origin/dev
  origin/dev-swift3.2
  origin/dev1.1.2
  origin/dev1.2
  origin/feature/adjust-font
  origin/feature/send-file
  origin/master
  origin/pjsip
  origin/rageshake
  origin/reconnect
  origin/release
  origin/sslsocket
  origin/tls
  origin/zimsdk


$ git branch -d adjust-font
Deleted branch adjust-font (was 2191110).

//    與遠程服務器建立映射
$ git checkout -b adjust-font origin/feature/adjust-font
M    submodules/zimsdk
Branch adjust-font set up to track remote branch feature/adjust-font from origin.
Switched to a new branch 'adjust-font'

$ git pull
Already up-to-date.

$ git branch
* adjust-font
  master

 

 

7. 將本地的代碼推送到服務器

 1 $ cd /Users/用戶名/Desktop/
 2 
 3 $ cd GitDemo
 4 
 5 $ git init
 6 
 7 $ git add --all    //提交到暫存區
 8 
 9 $ git commit -m "提交文件到 master"
10 
11 $ git remote add origin git@github.com:WindAndSand/GitTest.git
12 
13 $ fatal: remote origin already exists.    //不是錯誤,告訴你 origin 已存在
14 
15 $ git push origin master:master 
16 
17 //    出現錯誤,制取其中一個如:
18 
19 $ Updates were rejected because the remote contains work that you do
20 
21 //    原因:造成這個錯誤是因為我在github上修改了自己的文件,在上傳本地文件之前沒有把github上的文件拉倒本地。意思是本地和遠程的文件應該合並后才能上傳本地的新文件
22 
23 //    先拉下來,會自動合並的(不用操心)
24 
25 $ git pull origin master  //若遠程倉庫不存在其他文件,空空的什么也沒有,不用敲,當然不包括 REAMD
26 
27 //    再上傳
28 
29 $ git push -u origin master

 

 

 

8. 拉取遠程服務器的文件

  命令:

1 git clone "地址";

 

   注意:有一個陷阱,工程中還有子模塊,否則會報錯,如錯誤:( MessagesMissing dependency target "zimsdkoc (from zimsdkoc.xcodeproj)"),沒法修改;

  下載子模塊的命令:

1 git submodule  update --init --recursive

 

  子模塊和類庫的定義:http://blog.csdn.net/sinat_16714231/article/details/52797845;

  若提示版本更新可以忽略下載:

1  pod install --verbose

   


免責聲明!

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



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