ObjectiveGit(二)push & pull

push

生成一个commit之后,接下来就可以推送到远端了。push之前需要获取到远端仓库信息。由于一个本地仓库可以设置多个远端仓库,这里默认就取第一个远端仓库。

GTConfiguration *conf = [_repo configurationWithError:NULL];
NSError *error = nil;
GTRemote *remote = [conf.remotes firstObject];

由于push操作是需要权限的,所以要配置用户名和密码。注意这里的key值和clone的时候不一样。

GTCredentialProvider *provider = [GTCredentialProvider providerWithBlock:^GTCredential * _Nullable(GTCredentialType type, NSString * _Nonnull URL, NSString * _Nonnull userName) {
   GTCredential *cre = [GTCredential credentialWithUserName:@"xporter" password:@"12345678" error:NULL];
   return cre;
}];

 NSDictionary *remoteOptions = @{GTRepositoryRemoteOptionsCredentialProvider:provider};

获取分支信息

GTBranch *curretntBarch = [_repo currentBranchWithError:&error];

开始向远端推送

BOOL r = [_repo pushBranch:curretntBarch toRemote:remote withOptions:pushOptions error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL * _Nonnull stop) {
    NSLog(@"%d/%d",current,total);
}];

如果没有报错的话就是推送成功了,可以到远端仓库进行验证。

pull

要同步远端的指定分支的信息,就需要用到pull方法。先获取需要更新的分支

GTBranch *brach = [_repo currentBranchWithError:NULL];

pull 也是需要权限信息的,要设置用户信息

GTCredentialProvider *provider = [GTCredentialProvider providerWithBlock:^GTCredential * _Nullable(GTCredentialType type, NSString * _Nonnull URL, NSString * _Nonnull userName) {
   GTCredential *cre = [GTCredential credentialWithUserName:@"xporter" password:@"12345678" error:NULL];
   return cre;
}];

NSDictionary *remoteOptions = @{GTRepositoryRemoteOptionsCredentialProvider:provider};

获取远端分支信息

GTConfiguration *conf = [_repo configurationWithError:NULL];
NSError *error = nil;
GTRemote *remote = [conf.remotes firstObject];

拉取远端信息

BOOL r = [_repo pullBranch:brach fromRemote:remote withOptions:pullOptions error:&error progress:^(const git_transfer_progress * _Nonnull progress, BOOL * _Nonnull stop) {

}];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Git 基础 基本原理 客户端并不是只提取最新版本的文件快照,而是把代码仓库完整的镜像下来。这样一来,任何一处协同...
    __silhouette阅读 16,030评论 5 147
  • Git 命令行学习笔记 Git 基础 基本原理 客户端并不是只提取最新版本的文件快照,而是把代码仓库完整的镜像下来...
    sunnyghx阅读 9,371评论 0 11
  • 高老太太有个从不示人的大红朱漆描龙雕凤的盒子,一把古旧的铜锁一直守着这个属于老太太一个人的秘密。 高老太太年纪很老...
    淡淡青莲阅读 4,446评论 14 22
  • 我发布送书的文章后,大家反响热烈,评论积极,并预祝我在广州生活愉快。 感谢大家的言布施。 我希望大家可以轻松看书,...
    阿涛有点帅阅读 2,586评论 3 2
  • 近日来江歌刘鑫案再次掀起波澜,事件的男主角陈世峰同志再次提出了新理论:是江歌先用刀刺向他,自己不过是自卫反...
    第六天魔阅读 2,166评论 0 0