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