[关闭]
@22221cjp 2016-08-17T15:26:59.000000Z 字数 14162 阅读 1526

Git记录

工作


默认git clone是clone的 master分支

如果要clone指定分支:

git checkout --track -b [local-branch-name] origin/[origin-branch-name]

git push origin HEAD:chenjp

git commit -a -m ‘test2'

一次冲突解决过程

  1. cd /Users/chenjp/Documents/dianping/biz/\.idea/artifacts
  2. Last login: Thu May 7 16:17:30 on ttys001
  3. chenjp@chenjpdeMacBook-Pro ~$ cd /Users/chenjp/Documents/dianping/biz/\.idea/artifacts
  4. chenjp@chenjpdeMacBook-Pro artifacts$ pwd
  5. /Users/chenjp/Documents/dianping/biz/.idea/artifacts
  6. chenjp@chenjpdeMacBook-Pro artifacts$ cd ..
  7. chenjp@chenjpdeMacBook-Pro .idea$ cd ..
  8. chenjp@chenjpdeMacBook-Pro biz$ ls
  9. README.md beauty-biz-impl biz-parent.iml target
  10. beauty-biz-api beauty-biz-service pom.xml
  11. chenjp@chenjpdeMacBook-Pro biz$ ls -a
  12. . .gitignore beauty-biz-api biz-parent.iml
  13. .. .idea beauty-biz-impl pom.xml
  14. .git README.md beauty-biz-service target
  15. chenjp@chenjpdeMacBook-Pro biz$ cat .gitignore
  16. /.settings
  17. /target
  18. /test-output
  19. /.classpath
  20. /.project
  21. *.iml
  22. **.DS_Store
  23. chenjp@chenjpdeMacBook-Pro biz$ echo "/.idea" >> .gitignore
  24. chenjp@chenjpdeMacBook-Pro biz$ cat .gitignore
  25. /.settings
  26. /target
  27. /test-output
  28. /.classpath
  29. /.project
  30. *.iml
  31. **.DS_Store
  32. /.idea
  33. chenjp@chenjpdeMacBook-Pro biz$ pqs
  34. -bash: pqs: command not found
  35. chenjp@chenjpdeMacBook-Pro biz$ pwd
  36. /Users/chenjp/Documents/dianping/biz
  37. chenjp@chenjpdeMacBook-Pro biz$
  38. chenjp@chenjpdeMacBook-Pro biz$
  39. chenjp@chenjpdeMacBook-Pro biz$ ls
  40. README.md beauty-biz-impl biz-parent.iml target
  41. beauty-biz-api beauty-biz-service pom.xml
  42. chenjp@chenjpdeMacBook-Pro biz$ cd ..
  43. chenjp@chenjpdeMacBook-Pro dianping$ ls
  44. biz promotion-biz-server technician-mobile-web
  45. data promotion-mobile-web
  46. iml technician-biz
  47. chenjp@chenjpdeMacBook-Pro dianping$ cd technician-biz/
  48. chenjp@chenjpdeMacBook-Pro technician-biz$ ls
  49. README.md technician-biz-api technician-biz-service
  50. pom.xml technician-biz-impl
  51. target technician-biz-parent.iml
  52. chenjp@chenjpdeMacBook-Pro technician-biz$ git branch
  53. * master
  54. chenjp@chenjpdeMacBook-Pro technician-biz$ pwd
  55. /Users/chenjp/Documents/dianping/technician-biz
  56. chenjp@chenjpdeMacBook-Pro technician-biz$
  57. chenjp@chenjpdeMacBook-Pro technician-biz$ cd ..
  58. chenjp@chenjpdeMacBook-Pro dianping$ ls
  59. biz promotion-biz-server technician-mobile-web
  60. data promotion-mobile-web
  61. iml technician-biz
  62. chenjp@chenjpdeMacBook-Pro dianping$ mkdir technician-biz-master
  63. chenjp@chenjpdeMacBook-Pro dianping$ cd technician-biz-master/
  64. chenjp@chenjpdeMacBook-Pro technician-biz-master$ ls
  65. chenjp@chenjpdeMacBook-Pro technician-biz-master$ pwd
  66. /Users/chenjp/Documents/dianping/technician-biz-master
  67. //这里突然有的,是因为用工具生成clone了
  68. chenjp@chenjpdeMacBook-Pro technician-biz-master$ ls
  69. README.md technician-biz-api technician-biz-service
  70. pom.xml technician-biz-impl
  71. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch
  72. * master
  73. chenjp@chenjpdeMacBook-Pro technician-biz-master$
  74. chenjp@chenjpdeMacBook-Pro technician-biz-master$
  75. chenjp@chenjpdeMacBook-Pro technician-biz-master$ ls
  76. README.md technician-biz-api technician-biz-service
  77. pom.xml technician-biz-impl
  78. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch
  79. * master
  80. chenjp@chenjpdeMacBook-Pro technician-biz-master$ pwd
  81. /Users/chenjp/Documents/dianping/technician-biz-master
  82. chenjp@chenjpdeMacBook-Pro technician-biz-master$
  83. chenjp@chenjpdeMacBook-Pro technician-biz-master$
  84. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch -a
  85. * master
  86. remotes/origin/HEAD -> origin/master
  87. remotes/origin/chenjp
  88. remotes/origin/hao.yuan
  89. remotes/origin/lightmerge
  90. remotes/origin/master
  91. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git checkout --track -b chenjp origin/chenjp
  92. Branch chenjp set up to track remote branch chenjp from origin.
  93. Switched to a new branch 'chenjp'
  94. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch
  95. * chenjp
  96. master
  97. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git checkout master
  98. Switched to branch 'master'
  99. Your branch is up-to-date with 'origin/master'.
  100. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git pull
  101. Already up-to-date.
  102. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git checkout chenjp
  103. Switched to branch 'chenjp'
  104. Your branch is up-to-date with 'origin/chenjp'.
  105. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch
  106. * chenjp
  107. master
  108. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git rebase master
  109. First, rewinding head to replay your work on top of it...
  110. Applying: 丽人周报提交
  111. Using index info to reconstruct a base tree...
  112. M technician-biz-service/src/main/resources/config/spring/local/applicationContext-pigeon.xml
  113. <stdin>:3362: trailing whitespace.
  114. * 根据传入的时间获得当前时间所在周的第一天和第七天日期
  115. <stdin>:3363: trailing whitespace.
  116. * @param tm 时间
  117. <stdin>:3364: trailing whitespace.
  118. * @param firstday 周日作为周一为0,周一作为周一1
  119. <stdin>:3513: trailing whitespace.
  120. Id, TechnicianId, Name, ShopId, CityId, CategoryId, Star, TotalStar, PopularityRankChange,
  121. <stdin>:3514: trailing whitespace.
  122. StarRank, StarRankChange, TotalTechnician, BookOrderCount, WeeklyVisits, IsDeleted,
  123. warning: squelched 18 whitespace errors
  124. warning: 23 lines add whitespace errors.
  125. Falling back to patching base and 3-way merge...
  126. Auto-merging technician-biz-service/src/main/resources/config/spring/local/applicationContext-pigeon.xml
  127. CONFLICT (content): Merge conflict in technician-biz-service/src/main/resources/config/spring/local/applicationContext-pigeon.xml
  128. Failed to merge in the changes.
  129. Patch failed at 0001 丽人周报提交
  130. The copy of the patch that failed is found in:
  131. /Users/chenjp/Documents/dianping/technician-biz-master/.git/rebase-apply/patch
  132. When you have resolved this problem, run "git rebase --continue".
  133. If you prefer to skip this patch, run "git rebase --skip" instead.
  134. To check out the original branch and stop rebasing, run "git rebase --abort".
  135. chenjp@chenjpdeMacBook-Pro technician-biz-master$ ls
  136. README.md technician-biz-api technician-biz-service
  137. pom.xml technician-biz-impl
  138. chenjp@chenjpdeMacBook-Pro technician-biz-master$ git branch
  139. * (no branch, rebasing chenjp)
  140. chenjp
  141. master
  142. chenjp@chenjpdeMacBook-Pro technician-biz-master$ cd technician-biz-service/
  143. chenjp@chenjpdeMacBook-Pro technician-biz-service$ ls
  144. pom.xml src
  145. chenjp@chenjpdeMacBook-Pro technician-biz-service$ cd src/main/
  146. chenjp@chenjpdeMacBook-Pro main$ ls
  147. java resources webapp
  148. chenjp@chenjpdeMacBook-Pro main$ cd resources/
  149. chenjp@chenjpdeMacBook-Pro resources$ ls
  150. META-INF config log4j.xml logback.xml
  151. chenjp@chenjpdeMacBook-Pro resources$ cd config/
  152. chenjp@chenjpdeMacBook-Pro config$ ls
  153. applicationContext.properties spring
  154. chenjp@chenjpdeMacBook-Pro config$ cd spring/
  155. chenjp@chenjpdeMacBook-Pro spring$ ls
  156. local
  157. chenjp@chenjpdeMacBook-Pro spring$ cd local/
  158. chenjp@chenjpdeMacBook-Pro local$ ls
  159. applicationContext-client.xml
  160. applicationContext-core.xml
  161. applicationContext-pigeon.xml
  162. applicationContext-swallow-technicianReview.xml
  163. chenjp@chenjpdeMacBook-Pro local$ ls -a
  164. .
  165. ..
  166. applicationContext-client.xml
  167. applicationContext-core.xml
  168. applicationContext-pigeon.xml
  169. applicationContext-swallow-technicianReview.xml
  170. chenjp@chenjpdeMacBook-Pro local$ vim applicationContext-pigeon.xml
  171. chenjp@chenjpdeMacBook-Pro local$ cd ..
  172. chenjp@chenjpdeMacBook-Pro spring$ ls
  173. local
  174. chenjp@chenjpdeMacBook-Pro spring$ cd ..
  175. chenjp@chenjpdeMacBook-Pro config$ cd ..
  176. chenjp@chenjpdeMacBook-Pro resources$ ls
  177. META-INF config log4j.xml logback.xml
  178. chenjp@chenjpdeMacBook-Pro resources$ cd ..
  179. chenjp@chenjpdeMacBook-Pro main$ cd ..
  180. chenjp@chenjpdeMacBook-Pro src$ cd ..
  181. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git branch
  182. * (no branch, rebasing chenjp)
  183. chenjp
  184. master
  185. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git add .
  186. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git rebase --continue
  187. Applying: 丽人周报提交
  188. Applying: update 技师周报
  189. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git status
  190. On branch chenjp
  191. Your branch and 'origin/chenjp' have diverged,
  192. and have 8 and 2 different commits each, respectively.
  193. (use "git pull" to merge the remote branch into yours)
  194. nothing to commit, working directory clean
  195. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git rebase master
  196. Current branch chenjp is up to date.
  197. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git branch
  198. * chenjp
  199. master
  200. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git push origin HEAD:chenjp
  201. To git@code.dianpingoa.com:technician/technician-biz.git
  202. ! [rejected] HEAD -> chenjp (non-fast-forward)
  203. error: failed to push some refs to 'git@code.dianpingoa.com:technician/technician-biz.git'
  204. hint: Updates were rejected because the tip of your current branch is behind
  205. hint: its remote counterpart. Integrate the remote changes (e.g.
  206. hint: 'git pull ...') before pushing again.
  207. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  208. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git branch
  209. * chenjp
  210. master
  211. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git status
  212. On branch chenjp
  213. Your branch and 'origin/chenjp' have diverged,
  214. and have 8 and 2 different commits each, respectively.
  215. (use "git pull" to merge the remote branch into yours)
  216. You have unmerged paths.
  217. (fix conflicts and run "git commit")
  218. Unmerged paths:
  219. (use "git add <file>..." to mark resolution)
  220. both modified: src/main/resources/config/spring/local/applicationContext-pigeon.xml
  221. no changes added to commit (use "git add" and/or "git commit -a")
  222. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git add .
  223. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git status
  224. On branch chenjp
  225. Your branch and 'origin/chenjp' have diverged,
  226. and have 8 and 2 different commits each, respectively.
  227. (use "git pull" to merge the remote branch into yours)
  228. All conflicts fixed but you are still merging.
  229. (use "git commit" to conclude merge)
  230. Changes to be committed:
  231. modified: src/main/resources/config/spring/local/applicationContext-pigeon.xml
  232. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git commit -a -m 'resolve conflict'
  233. [chenjp 45eab62] resolve conflict
  234. chenjp@chenjpdeMacBook-Pro technician-biz-service$
  235. chenjp@chenjpdeMacBook-Pro technician-biz-service$
  236. chenjp@chenjpdeMacBook-Pro technician-biz-service$
  237. chenjp@chenjpdeMacBook-Pro technician-biz-service$
  238. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git log
  239. commit 45eab62cef9cc116403f65aa3b1f008f394a1259
  240. Merge: 160f108 41ec8e0
  241. Author: jinpeng.chen <22221cjp@163.com>
  242. Date: Fri May 8 18:35:26 2015 +0800
  243. resolve conflict
  244. commit 160f1081dc209010ab4c894cb8c8c7ffc4c6541f
  245. Author: jinpeng.chen <22221cjp@163.com>
  246. Date: Fri May 8 17:16:41 2015 +0800
  247. update 技师周报
  248. commit 0c88ed90d6c2ede39dc77ce8be1b8caaeb34fe39
  249. Author: jinpeng.chen <22221cjp@163.com>
  250. Date: Thu May 7 19:31:12 2015 +0800
  251. 丽人周报提交
  252. commit 41ec8e0b88c74792639458ad3ecb37ba608ba6e4
  253. Author: jinpeng.chen <22221cjp@163.com>
  254. Date: Fri May 8 17:16:41 2015 +0800
  255. chenjp@chenjpdeMacBook-Pro technician-biz-service$ git push origin HEAD:chenjp
  256. Counting objects: 30, done.
  257. Delta compression using up to 4 threads.
  258. Compressing objects: 100% (27/27), done.
  259. Writing objects: 100% (30/30), 2.49 KiB | 0 bytes/s, done.
  260. Total 30 (delta 15), reused 0 (delta 0)
  261. To git@code.dianpingoa.com:technician/technician-biz.git
  262. 41ec8e0..45eab62 HEAD -> chenjp
  263. chenjp@chenjpdeMacBook-Pro technician-biz-service$

合并不同分支查看这个链接:
http://www.cnblogs.com/kym/archive/2010/08/12/1797937.html

Git如何进行分支管理?

1、创建分支

创建分支很简单:git branch <分支名>

2、切换分支

git checkout <分支名>
该语句和上一个语句可以和起来用一个语句表示:git checkout -b <分支名>

3、分支合并

比如,如果要将开发中的分支(develop),合并到稳定分支(master),
首先切换的master分支:git checkout master。
然后执行合并操作:git merge develop。
如果有冲突,会提示你,调用git status查看冲突文件。
解决冲突,然后调用git add或git rm将解决后的文件暂存。
所有冲突解决后,git commit 提交更改。

4、分支衍合

分支衍合和分支合并的差别在于,分支衍合不会保留合并的日志,不留痕迹,而 分支合并则会保留合并的日志。
要将开发中的分支(develop),衍合到稳定分支(master)。
首先切换的master分支:git checkout master。
然后执行衍和操作:git rebase develop。
如果有冲突,会提示你,调用git status查看冲突文件。
解决冲突,然后调用git add或git rm将解决后的文件暂存。
所有冲突解决后,git rebase --continue 提交更改。

5、删除分支

执行git branch -d <分支名>
如果该分支没有合并到主分支会报错,可以用以下命令强制删除git branch -D <分支名>

=============================

建立本地的dev分支追踪远程仓库的develop分支

git checkout –b dev origin/develop
git push origin HEAD:chenjp

==============================

orgin在git中到底啥意思?

origin 是远程仓库在本地的别名。因为每次输入一次远程仓库的url比较麻烦,所以就起个一个别名。如果新的仓库是在远程建立的,如oschina上,然后通过clone下来的。如果在clone下来的本地仓库中输入git remote -v可以查看

可以看到默认的别名就是origin,这个名字也好理解。因为origin本意是“原始的”,远程相对于本地的clone版本,的确是原始的。
但是如果仓库是在本地通过git init 建立的。这时候就没有一个远程仓库和本地对应。这时候就需要执行如下这个命令了。

git remote add origin git@xbc.me:wordpress.git

约定俗成,这里的别名依然叫origin。

但是现在一般都是在远程先建立,然后直接clone到本地。这样更省事。

git stash save '说一点这次stash保存内容'
git stash list
git stash pop stash{0} 弹出stash栈中的某一次临时保存

===================================================================

在本地新建立仓库,然后和远程仓库对应上

git init
git add .
git commit -am ‘first commit'

git remote add origin https://xxxxxxx
git remote -v
git remote rm origin //删除origin别名对应的远程仓库
ssh-keygen -t rsa -C 'you_email@email.com'
复制~/.ssh /rsa.pub 到github上配置一下
git pull —rebase origin master

git checkout -b new_branch
git checkout -b new_branch origin/branch1 //基于远程仓库的branch1创建本地分支new_branch
git stash 将本地工作临时保存
git stash save ‘descripe your work’
git stash list
git stash pop stash@{0}
git stash clear

git push origin HEAD:chenjp
git rebase master //将master合并到当前分支,保留master的提交记录

git branch 查看分支
git branch -D branch1

//基于某次提交创建分支----间接回到历史上某个版本
git branch branch_name fe873ac91

git push origin :branch-name //删除远程仓库分支
git branch -m old_branch new_branch //重命名分支
git cherry-pick [commit_bash] //将一个次提交应用到当前分支

//有时候rebase了其他分支,改变了当前分支之前的历史,push的时候会出错,这时候需要添加--force参数
git push —force origin HEAD:chenjp
git rm —cached filename //取消某个文件的跟踪
git rm —cached -r dir //取消某个目录的跟踪

git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。

$ git pull <远程主机名> <远程分支名>:<本地分支名>
比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。

$ git pull origin next:master
如果远程分支是与当前分支合并,则冒号后面的部分可以省略。

$ git pull origin next

实际使用最好用git pull --rebase保留合并的历史

故障处理

==========================git 故障处理========================
基于远程分支创建本地分支的时候,有时候会报这个错。

  1. chenjp@chenjp ~/D/d/beauty-merchant-biz> git checkout -b dengyun.zhou origin/dengyun.zhou
  2. fatal: Cannot update paths and switch to branch 'dengyun.zhou' at the same time.
  3. Did you intend to checkout 'origin/dengyun.zhou' which can not be resolved as commit?
  4. chenjp@chenjp ~/D/d/beauty-merchant-biz>

原因是自己原来clone的时候,远程并没有这个分支,通过如下命令查看

  1. chenjp@chenjp ~/D/d/beauty-merchant-biz> git branch -a
  2. * chenjp
  3. master
  4. remotes/origin/20150810-offline
  5. remotes/origin/20151010-authority
  6. remotes/origin/HEAD -> origin/master
  7. remotes/origin/lightmerge
  8. remotes/origin/master
  9. chenjp@chenjp ~/D/d/beauty-merchant-biz>

所以会报上面的错。解决方法,需要fetch下所有的远程分支。

  1. chenjp@chenjp ~/D/d/beauty-merchant-biz> git fetch origin
  2. remote: Counting objects: 892, done.
  3. remote: Compressing objects: 100% (486/486), done.
  4. remote: Total 892 (delta 364), reused 175 (delta 71)
  5. Receiving objects: 100% (892/892), 79.89 KiB | 0 bytes/s, done.
  6. Resolving deltas: 100% (364/364), completed with 21 local objects.
  7. From code.dianpingoa.com:beauty/beauty-merchant-biz
  8. d2b3d6b..ab0979c 20151010-authority -> origin/20151010-authority
  9. * [new branch] dengyun.zhou -> origin/dengyun.zhou
  10. d2b3d6b..84519c7 lightmerge -> origin/lightmerge
  11. * [new branch] shuang.he -> origin/shuang.he
  12. chenjp@chenjp ~/D/d/beauty-merchant-biz> git branch -a
  13. * chenjp
  14. master
  15. remotes/origin/20150810-offline
  16. remotes/origin/20151010-authority
  17. remotes/origin/HEAD -> origin/master
  18. remotes/origin/dengyun.zhou
  19. remotes/origin/lightmerge
  20. remotes/origin/master
  21. remotes/origin/shuang.he
  22. chenjp@chenjp ~/D/d/beauty-merchant-biz> git checkout -b dengyun.zhou origin/dengyun.zhou
  23. Branch dengyun.zhou set up to track remote branch dengyun.zhou from origin.
  24. Switched to a new branch 'dengyun.zhou'
  25. chenjp@chenjp ~/D/d/beauty-merchant-biz>

Git忽略文件

git忽略多层文件夹是经常的需求,在.gitignore忽略多层文件夹用:**
**/bin/Debug/
前面的两个*号代表任意多层上级文件夹
需要 git 1.8.2 及其以上的版本才支持

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注