iOS - git

作者: 梦回蓝桥 | 来源:发表于2017-08-02 10:49 被阅读63次

git 相关的 博客不少,基本命令就不多讨论了,建议大家多看看下面几篇文章,这里主要总结一些本人在使用git 过程中遇到的一些坑git供以后好查询特记录于此。

git 基础:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013744142037508cf42e51debf49668810645e02887691000

git 工作流:

https://segmentfault.com/a/1190000002918123#articleHeader10(多种)
https://www.cnblogs.com/cnblogsfans/p/5075073.html(git flow)

1. 有关ssk key (访问权限相关)的错误

 Permission denied (publickey).
 fatal: Could not read from remote repository.

 Please make sure you have the correct access rights  and the repository exists.


意思是说你没权限访问远程仓库,此时你可以执行:
$ ssh -T git@github.com (验证git的访问权限)
多数会返回:Permission denied (publickey).<权限被拒绝>

 命令回顾:
 ** git 通过检测用户名和邮箱来跟踪commit的用户 **
 $ git config --global user.name "user name"
 $ git config --global user.email "user email"
 ** 创建ssh key(通过key才可以访问远程仓库)**
 $ ssh-keygen -t rsa -C "user email" 


导致这种问题的情况有很多:

情况一:你是否配置了远程仓库的 ssh key

屏幕快照 2017-11-16 上午11.22.50.png

如果本地已经生成了但还没有配置到远程,则执行:open ~/.ssh 打开后缀为.pub的文件并复制粘贴到远程

情况二:新生成的key不能加入ssh就会导致连接不上github

解决办法如下:
如果你open ~/.ssh没有找到相关.pub文件

 $ ssh-agent
 $ ssh-add ~/.ssh/id_rsa


确保 ssh-agent 是可用的。ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程。
添加生成的 SSH key 到 ssh-agent。

情况三:无非就是你多次执行ssh-keygen -t rsa -C "user email"生成了多个key,但有效的(也就是最新的)没有粘贴到远程

解决办法:删除远程现有的ssh key并删除本地所有的ssh,然后
执行:ssh-keygen -t rsa -C "user email" 生成新的ssh
执行:open ~/.ssh 并打开后缀为.pub的文件复制粘贴到远程

2. 忘记添加.gitignore(忽略文件)导致的错误

error: Your local changes to the following files would be overwritten by merge:
lgit.xcodeproj/project.xcworkspace/xcuserdata/x8f.xcuserdatad/UserInterfaceState.xcuserstate
Please, commit your changes or stash them before you can merge.
Aborting


解决办法有两种:
一:为项目添加 swift.gitignore(https://github.com/github/gitignore
二:自建忽略文件

添加 swift.gitignore

 1. 创建你的工程,将swift.gitignore文件拖到根目录;
 2. $ cd /Users/x8f/Desktop/Lgithub (进入工程根目录)
 3. $ mv Swift.gitignore .gitignore (改名,这样.gitignore就隐藏了)
 4. 创建与本地项目同名的远程仓库
 5. $ git init (本地仓库初始化)
 6. $ git add . (将工作区的修改添加到暂存去)
 7. $ git commit -m 'first commit'(提交暂存区修改到当前分支)
 8. $ git remote add origin git@github.com:skyxian/Lgithub.git(关联远程git仓库)
 9. $ git push origin master(将本地master分支的内容推送到远程)
10. 之后创建分支,合并提交就不会出现冲突的情况了


http://www.jianshu.com/p/c117795ecde0
http://www.jianshu.com/p/b9d97d9f3e28

自建 .gitignore(忽略文件)

注意,开始版本控制之前就要创建.gitignore文件
第一步:首先进入项目的根目录

x8fdeMacBook-Pro:cc x8f$ cd /Users/x8f/Desktop/lgit(到当前目录)


第二步:执行git init 这时根目录下会有.git文件夹(隐藏)
第三步:执行touch .gitignore (创建.gitignore文件)
第四步:执行vim .gitignore(打开并编辑此文件)
第五步:在打开的.gitignore文件中输入以下需要过滤的内容

//按需添加自己需要的文件
*.xcuserstate
project.xcworkspace
xcuserdata
UserInterfaceState.xcuserstate
project.xcworkspace/
xcuserdata/
UserInterface.xcuserstate 

注意:点击 i 进入编辑(insert)模式,编辑完成后按esc退出基本模式,然后按 :(分号)输入wq回车就保存并退出了

第六步:git add.
第七步:git commit -m 'commit msg'
第八步:git push origin master
然后再执行上面的创建分支、修改、分支合并操作,问题解决~

之后执行

$ ssh -T git@github.com 


就会显示

 Hi youraccount! You've successfully authenticated, but GitHub does not provide shell access.(成功连接)

3. git rebase

无非就是看起来像是一个Fast-Forward , 没经过merge一样
rebase:


rebase2.png

非rebase(merge):


rebase1.png

参考:http://blog.csdn.net/wangjia55/article/details/8490679

4. git fetch 与 git pull

git pull = git fetch + git merge 

对比本地及远程分支的区别:
git fetch origin 
git diff master origin/master


所以童鞋,项目中多使用git fetch 对你没坏处!

5. git flow 的实战使用

假设你已经跟远程仓库建立了关联,本地只有master分支。
执行分支查看命令:

x8fdeMacBook-Pro:gitFlow x8f$ git branch -a
* master
remotes/origin/master


我们看到本地和远程都只有一个master分支,并且他俩是对应关系;

示例:
第一步为master分支配套一个develop分支。简单来做可以本地创建一个空的develop分支,push到服务器上:

$ git branch develop (创建本地分支develop)
$ git push -u origin develop(将本地的develop分支推送到远程,此时本地develop对应原称的develop ,本地master对应远程的master)
  Total 0 (delta 0), reused 0 (delta 0)
  To git@github.com:skyxian/gitFlow.git
  * [new branch]      develop -> develop
  Branch develop set up to track remote branch develop from origin.
$ git branch -a
  develop
  * master
  remotes/origin/develop
  remotes/origin/master


现在我们两个本地分支分别对应远程的两个分支。
可以使用命令:

$ git branch -vv
  develop bab34b5 [origin/develop] add .gitignore
  * master  bab34b5 [origin/master] add .gitignore


来查看本地分支与远程分支的对应关系;

如果你不是分支创建者,直接从远程clone的代码,并建立了对应远程develop分支的本地develop分支,只需看下面代码就可

a. 开始新Feature开发

 ** 创建一个feature-1分支对应远程的develop分支并切换到当前分支 **
$ git checkout -b feature-1 develop
  Switched to a new branch 'feature-1'


我们执行分支关联命令

 $ git branch -vv


可以看到分支对应情况,如下:

 develop   bab34b5 [origin/develop] add .gitignore
 * feature-1 bab34b5 add .gitignore
 master    bab34b5 [origin/master] add .gitignore


我随便在feature-1分支上打了一个log操作:

屏幕快照 2017-11-17 上午10.54.01.png
假设此时开发完成了,然后我们执行:
$ git add .
$ git commit -m "add feature-1"


b. 完成feature

 ** 当前分支是feature-1 ,但他已经关联到了远程的develop分支(还记得前面执行的 git checkout -b feature-1 develop 吗?)
$ git pull origin develop(拉取并合并)
   From github.com:skyxian/Lgitflow
   * branch            develop    -> FETCH_HEAD
   Already up-to-date.
$ git checkout develop (之前是在feature-1分支上)
  Switched to branch 'develop'
  Your branch is up-to-date with 'origin/develop'.
$ git merge feature-1
  Updating bab34b5..f1b19c0
  Fast-forward
  Lgitflow/ViewController.m | 1 +
  1 file changed, 1 insertion(+)
$ git push
$ git branch -d feature-1


此时执行git branch -a 命令查看

$ git branch -a
* develop
  master
  remotes/origin/develop
  remotes/origin/master


到此,一部分的功能依靠feature-1分支完成了,其他功能执行相似的操作创建feature-2,feature-3...就好了

c. 发布分支 release-1.0
这个分支是清理发布、执行所有测试、更新文档和其它为下个发布做准备操作的地方,像是一个专门用于改善发布的功能分支。

$ git checkout -b release-1 develop
  Switched to a new branch 'release-1'
  ** 然后 我在vc中执行了nslog(“release-1.0”);的操作 ** 
  ** 这里假如你在release-1这个发布分支上的操作进行完了 **
 $ git add .
 $ git commit -m " add release-1"

 $ git checkout master
 $ git merge release-1
 $ git push
 ** 同样的操作也要跟develop同步 **
 $ git checkout develop
 $ git merge release-1
 $ git push
 ** 最后,删除此发布分支 **
 $ git branch -d release-1


最后就是给master分支打上tag,我的执行是

$ git checkout master (首先给切换到了master分支,其实不这样直接执行下面两步操作应该也没问题)
$ git tag -a 1.0 -m "release1.0" master
$ git push --tags


现在你就可以去远程查看tag的情况了
下面是我测试gitflow工作流的全部命令流程:

Last login: Fri Nov 17 10:32:47 on ttys000
x8fdeMacBook-Pro:~ x8f$ cd /Users/x8f/Desktop/Lgitflow

x8fdeMacBook-Pro:Lgitflow x8f$ mv Swift.gitignore .gitignore

x8fdeMacBook-Pro:Lgitflow x8f$ git init
Initialized empty Git repository in /Users/x8f/Desktop/Lgitflow/.git/

x8fdeMacBook-Pro:Lgitflow x8f$ git add .

x8fdeMacBook-Pro:Lgitflow x8f$ git commit -m "add .gitignore"
[master (root-commit) bab34b5] add .gitignore
16 files changed, 1021 insertions(+)
create mode 100755 .gitignore
create mode 100644 Lgitflow.xcodeproj/project.pbxproj
create mode 100644 Lgitflow.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode 100644 Lgitflow/AppDelegate.h
create mode 100644 Lgitflow/AppDelegate.m
create mode 100644 Lgitflow/Assets.xcassets/AppIcon.appiconset/Contents.json
create mode 100644 Lgitflow/Base.lproj/LaunchScreen.storyboard
create mode 100644 Lgitflow/Base.lproj/Main.storyboard
create mode 100644 Lgitflow/Info.plist
create mode 100644 Lgitflow/ViewController.h
create mode 100644 Lgitflow/ViewController.m
create mode 100644 Lgitflow/main.m
create mode 100644 LgitflowTests/Info.plist
create mode 100644 LgitflowTests/LgitflowTests.m
create mode 100644 LgitflowUITests/Info.plist
create mode 100644 LgitflowUITests/LgitflowUITests.m

x8fdeMacBook-Pro:Lgitflow x8f$ git remote add origin git@github.com:skyxian/Lgitflow.git

x8fdeMacBook-Pro:Lgitflow x8f$ git push -u origin master
Counting objects: 25, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (25/25), 9.34 KiB | 0 bytes/s, done.
Total 25 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To git@github.com:skyxian/Lgitflow.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -a
* master
  remotes/origin/master

x8fdeMacBook-Pro:Lgitflow x8f$ git branch develop

x8fdeMacBook-Pro:Lgitflow x8f$ git push -u origin develop
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:skyxian/Lgitflow.git
 * [new branch]      develop -> develop
Branch develop set up to track remote branch develop from origin.

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -a
  develop
* master
  remotes/origin/develop
  remotes/origin/master

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -vv
  develop bab34b5 [origin/develop] add .gitignore
* master  bab34b5 [origin/master] add .gitignore
x8fdeMacBook-Pro:Lgitflow x8f$ git checkout -b feature-1 develop
Switched to a new branch 'feature-1'

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -vv
  develop   bab34b5 [origin/develop] add .gitignore
* feature-1 bab34b5 add .gitignore
  master    bab34b5 [origin/master] add .gitignore

x8fdeMacBook-Pro:Lgitflow x8f$ git status
On branch feature-1
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:   Lgitflow/ViewController.m

no changes added to commit (use "git add" and/or "git commit -a")

x8fdeMacBook-Pro:Lgitflow x8f$ git add .

x8fdeMacBook-Pro:Lgitflow x8f$ git commit -m "add log"
[feature-1 f1b19c0] add log
 1 file changed, 1 insertion(+)

x8fdeMacBook-Pro:Lgitflow x8f$ git pull origin develop
From github.com:skyxian/Lgitflow
 * branch            develop    -> FETCH_HEAD
Already up-to-date.

x8fdeMacBook-Pro:Lgitflow x8f$ git branch
  develop
* feature-1
  master

x8fdeMacBook-Pro:Lgitflow x8f$ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.

x8fdeMacBook-Pro:Lgitflow x8f$ git merge feature-1
Updating bab34b5..f1b19c0
Fast-forward
 Lgitflow/ViewController.m | 1 +
 1 file changed, 1 insertion(+)

x8fdeMacBook-Pro:Lgitflow x8f$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 371 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To git@github.com:skyxian/Lgitflow.git
   bab34b5..f1b19c0  develop -> develop
x8fdeMacBook-Pro:Lgitflow x8f$ git branch -d feature-1
Deleted branch feature-1 (was f1b19c0).

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -a
* develop
  master
  remotes/origin/develop
  remotes/origin/master

x8fdeMacBook-Pro:Lgitflow x8f$ git checkout -b release-1 develop
Switched to a new branch 'release-1'

x8fdeMacBook-Pro:Lgitflow x8f$ git status
On branch release-1
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:   Lgitflow/ViewController.m

no changes added to commit (use "git add" and/or "git commit -a")
x8fdeMacBook-Pro:Lgitflow x8f$ git add .
x8fdeMacBook-Pro:Lgitflow x8f$ git commit -m " add release-1"
[release-1 bcd2bde]  add release-1
 1 file changed, 1 insertion(+)

x8fdeMacBook-Pro:Lgitflow x8f$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

x8fdeMacBook-Pro:Lgitflow x8f$ git merge release-1
Updating bab34b5..bcd2bde
Fast-forward
 Lgitflow/ViewController.m | 2 ++
 1 file changed, 2 insertions(+)

x8fdeMacBook-Pro:Lgitflow x8f$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 375 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To git@github.com:skyxian/Lgitflow.git
bab34b5..bcd2bde  master -> master

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -a
develop
* master
  release-1
  remotes/origin/develop
  remotes/origin/master

x8fdeMacBook-Pro:Lgitflow x8f$ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.

x8fdeMacBook-Pro:Lgitflow x8f$ git merge release-1
Updating f1b19c0..bcd2bde
Fast-forward
 Lgitflow/ViewController.m | 1 +
 1 file changed, 1 insertion(+)

x8fdeMacBook-Pro:Lgitflow x8f$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:skyxian/Lgitflow.git
   f1b19c0..bcd2bde  develop -> develop

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -d release-1
Deleted branch release-1 (was bcd2bde).

x8fdeMacBook-Pro:Lgitflow x8f$ git branch -a
* develop
  master
  remotes/origin/develop
  remotes/origin/master

x8fdeMacBook-Pro:Lgitflow x8f$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
x8fdeMacBook-Pro:Lgitflow x8f$ git tag -a 1.0 -m "release1.0" master

x8fdeMacBook-Pro:Lgitflow x8f$ git push --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 164 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:skyxian/Lgitflow.git
 * [new tag]         1.0 -> 1.0
x8fdeMacBook-Pro:Lgitflow x8f$ 

5. E325: ATTENTION

 Found a swap file by the name "~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp"
      owned by: username   dated: Wed Dec 14 12:28:45 2016
     file name: ~username/Documents/Sites/recipegenerator/.git/MERGE_MSG
      modified: YES
     user name: username   host name: Users-MacBook-Pro.local
    process ID: 33747
While opening file "/Users/larsvanurk/Documents/Sites/recipegenerator/.git/MERGE_MSG"
         dated: Thu Dec 22 14:06:17 2016
  NEWER than swap file!


参考链接:
https://stackoverflow.com/questions/41284031/how-to-solve-git-merge-error-swap-file-merge-msg-swp-already-exists

http://blog.csdn.net/qq_32452623/article/details/78395832

http://www.zhimengzhe.com/mac/123351.html

相关文章

网友评论

      本文标题:iOS - git

      本文链接:https://www.haomeiwen.com/subject/wpohnttx.html