工做以来一直就使用SourceTree提升工做效率,那些本来熟悉的命令所有忘记干净了。
前些天因为忽然断电,公司内部服务器硬盘故障,因此内部服务器上git仓库重建,我也趁机把git命令操做又熟悉了一遍,今后决定不到万不得已再也不使用SourceTree操做了,一切操做都走git命令。
在这里把一些经常使用的和注意事项罗列出来供你们参考和本身记录。强烈建议不熟悉git的朋友先熟悉命令,SourceTree只是提升效率的一种手段而已,原理仍是要明白的好
。ios
题主已经打算卸载SourceTree
,本文主要讲解git使用三个方面:
1.构建协做环境(将已有代码放到git仓库上面供别人分享,将别人放在git上面的代码clone下来)
2.git经常使用命令(包括:代码的增删改查等操做)
3.忽略文件.gitignore的配置git
公司项目协做是须要配置ssh key的,全部操做的前提是你已经配置了ssh key。vim
具体命令以下:api
cd existing_folder //进入到工程文件夹 rm -rf .git //先清除.git文件 git init //再重置 git remote add origin xxxxxxxxx //连接到远程分支xxxxxxxxx(git上面的仓库地址) git add . //暂存全部文件 commit以前设置下名字和邮箱 global为全局 local为本次 git config --global user.name "瓜皮" git config --global user.email "guapi@yaomaitong.cn" git commit 须要输入注释说明 git commit -m 'description' git push -u origin xxxx //最后push到xxxx分支 等待完成以后到sourceTree里面去从添加已存在的本地仓库
因为公司是重建的git工程,push的时候报了个错,这里记录一下(由于服务重建,.ssh下的known_hosts里面的hostkey改变了,因此vim编辑进入,删除之前的key便可)xcode
zjmdeMBP:zhujiamin PRO$ git push -u origin master @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent by the remote host is SHA256:2pWcmnQ4P38EUSSXNp89uG0um/K01bz/mWMJC3TMj2M. Please contact your system administrator. Add correct host key in /Users/a123/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /Users/a123/.ssh/known_hosts:3 ECDSA host key for git.yaomaitong.net has changed and you have requested strict checking. Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
mkdir iosProject //在当前目录建立一个空文件夹 (也可手动建立或者直接使用已有的文件夹) cd iosProject //进入到你想要放置工程的空文件夹 rm -rf .git //先删除可能隐藏的git (我的习惯而已) git clone xxxxxxxx //clone远程git仓库到本地
clone成功的样子:缓存
zjmdeMacBook-Pro:restoreOldiOS PRO$ rm -rf .git zjmdeMacBook-Pro:restoreOldiOS PRO$ git clone git@git.xxxxxx.net:xxx/ios.git Cloning into 'ios'... remote: Counting objects: 5611, done. remote: Compressing objects: 100% (1914/1914), done. remote: Total 5611 (delta 3657), reused 5611 (delta 3657) Receiving objects: 100% (5611/5611), 107.30 MiB | 5.44 MiB/s, done. Resolving deltas: 100% (3657/3657), done. Checking connectivity... done.
clone成功以后应该在进入工程文件夹里面,本地建立几个远程必须的分支,主要用到的命令是:服务器
git branch -a 查看全部分支 git checkout -b xxxx origin/xxxx //建立并切换到想要建立的本地分支
操做成功的样子:网络
zjmdeMacBook-Pro:restoreOldiOS PRO$ ls //远程文件夹为ios ios zjmdeMacBook-Pro:restoreOldiOS PRO$ cd ios //进入 zjmdeMacBook-Pro:ios PRO$ ls Medicine MedicineTests Pods Medicine.xcodeproj Podfile README.md Medicine.xcworkspace Podfile.lock zjmdeMacBook-Pro:ios PRO$ git branch //查看已有分支(只能看到已存在的分支) * master zjmdeMacBook-Pro:ios PRO$ git branch -a //查看全部分支,能够看到远程有好几个分支 * master remotes/origin/HEAD -> origin/master remotes/origin/develop remotes/origin/feature_newLogin remotes/origin/master zjmdeMacBook-Pro:ios PRO$ git checkout -b develop origin/develop //挑选本地须要的分支进行建立 Branch develop set up to track remote branch develop from origin. Switched to a new branch 'develop' zjmdeMacBook-Pro:ios PRO$ git checkout -b feature_newLogin origin/feature_newLogin Branch feature_newLogin set up to track remote branch feature_newLogin from origin. Switched to a new branch 'feature_newLogin' zjmdeMacBook-Pro:ios PRO$ git status On branch feature_newLogin Your branch is up-to-date with 'origin/feature_newLogin'. nothing to commit, working directory clean
git status 查看当前所在分支和更改内容 git branch 查看全部分支 git branch xxx 新建某分支 git checkout xxx 切换到某分支 git checkout -b xxx 建立并切换到某分支 git pull origin xxx 拉取xxx分支 git push origin xxx 推送同步xxx分支 git merge xxx 合并分支 (通常须要先切换到目的分支,而后merge想要merge的分支)
git status //查看改动 git add xxx.m //暂存xxx.m文件 git add . //暂存全部改动 git rm xxx //删除xxx文件 git commit -m '修改了xx功能' //添加注释 git push origin xxxx //push 注意当前所在网络是否容许push git commit --mend 修改最近一次提交的代码(这里会进入vim编辑器去修改)
git log --pretty=oneline 文件名 //须要进入该文件所在的文件夹 这里必需要进入到.h/.m文件所在的文件夹下,而且只能看到每次commit产生的hash码 例如: zjmdeMBP:pinyin PRO$ git log --pretty=oneline ChineseString.h f9f280d0df7907af97f8c88be58e9eb14cabdc93 去除无效的代码 7146065685ccc243bf9ca24ce67de0bf4277f7bc 完善工程 db2d150806a8583fa006e105c4461f977507d341 first 想要看到详细情形还须要使用 git show <hash码> 才能看到该次commit所作的修改内容及Author 和 Date;
a.修改最近一次commit的内容ssh
git commit --amend 或 git commit --amend -m '注释信息'
b.commit前发现某个文件没有必要改动,咱们须要让该文件回到未改动的状态编辑器
git checkout -- <filename>
c.本地已经执行提交操做,可是尚未push,想要撤销某文件的更改
git reset <文件名> //缺省文件名时默认取消上次提交的全部内容,提交的内容回到未暂存状态 git reset --hard <文件名> //缺省文件名时默认取消上次提交的全部内容,而且修改的内容也所有重置
git log --oneline //查看全部的commit历史,显示每次commit生成的hash码的前七位和注释信息,例如:
zjmdeMBP:ios PRO$ git log --oneline a674e9f AFN更新至3.1.0 345db82 消息闪退try..catch; fb33370 更新cocoaPods及第三方库; 01931f7 忘记密码布局位置调整; 08ce469 增长友盟计数事件;
git revert xxxx //xxxx为某次commit的hash码,例如我想回滚到AFN更新前,命令以下:
git revert a674e9f
执行以后进入vim编辑,填写注释后保存退出,而后add、commit、push就完成回滚了。
题主执行这一操做的时候,刚好电脑内存满了。。。全部报了下面这个错,记录一下
".git/COMMIT_EDITMSG" E509: Cannot create backup file (add ! to override)
编辑完回滚信息wq竟然一直提示这个错误退不出去,一查发现是由于电脑内存满了,清理一些内容以后就OK了。
正常状况下咱们并不须要将工程里面的全部内容都提交到git远程仓库,而且有些信息(好比那些每次编译都会变化的信息)会时常出如今UnCommitted changes里面,以下图:
因此咱们须要一个.gitignore来帮助咱们过滤这些没必要要的信息;
事实上为了节省服务器空间,一些项目也会过滤掉pods管理的第三方库,只上传Podfile相关信息,下面的代码没有过滤pod文件,各位看官按需本身添加。
# Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore ## Build generated build/ DerivedData ## Various settings *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata ## Other *.xccheckout *.moved-aside *.xcuserstate
我所知道的两种建立方法以下:
cd existing_folder //进入到工程文件夹 touch .gitignore //建立一个.gitignore的文件 vim .gitignore //使用vim编辑器编辑.gitignore文件(也可使用方法b后半部分的方法去编辑)
当你用文本编辑器去编辑一个文件后保存为.gitignore的时候,系统会告诉你"Names that begin with a dot “.” are reserved for the system. If you decide to go ahead and use a name which begins with a dot the file will be hidden."由于以.开头的文件为系统保留,必定要建立的话该文件将会被隐藏。 因而咱们能够用文本编辑器将忽略文件的代码写好后保存到项目工程目录下并命名为.gitignore文件(忽略系统提示),虽然它默认被系统隐藏,可是将你的工程文件夹拉入一个文本编辑器, 以下图所示, 本文示例使用的Sublime Text, 依然可以看到并编辑的!
若是你已经将一些本该忽略的文件提交到git服务器, 那么配置好.gitignore以后会发现以前老是出现的用户信息依然出如今Uncommitted changes中, 缘由是由于.ignore是后来加进来的local cache里面记录的这个文件是不忽略的, 因此须要进行缓存清除, 这个时候你只须要将进入该工程目录下, 执行删除缓存中的文件后提交便可:
注意缓存路径就是出如今Uncommitted changes中的文件路径, 请确认路径正确。
$ git rm --cached 你的工程名.xcodeproj/project.xcworkspace/xcuserdata/你的电脑名.xcuserdatad/UserInterfaceState.xcuserstate $ git commit -m '注释信息' $ git push origin 分支名
git操做固然不止这点内容,太晚了, 明天继续完善补充。。。
感谢阅读,但愿本文对你有帮助!
本人坐标杭州,后续我会陆续把工做中遇到的问题及解决方案分享出来,互相交流学习,本人QQ:815187811,欢迎结交[笑脸].