git接触已有时日,**怎奈记性太差**(重点),隔几日不去用它,很多命令就记不住了,况且在多人协作分支管理那块地方,还有较多的问题没搞透,索性写个git日记,记录自己的使用心得和疑惑吧。

git
官网文档链接
https://git-scm.com/docs
在官网可以找到一个非常生动有趣的的git教程,简直不要太容易理解哦😌http://ndpsoftware.com/git-cheatsheet.html

1. 在Mac OS上使用git删除.DS_Store

.DS_Store是什么

使用 Mac 的用户可能会注意到,系统经常会自动在每个目录生成一个隐藏的 .DS_Store 文件。.DS_Store (英文全称 Desktop Services Store)是一种由苹果公司的Mac OS X操作系统所创造的隐藏文件,目的在于存贮目录的自定义属性,例如文件们的图标位置或者是背景色的选择。相当于 Windows 下的 desktop.ini

删除.DS_Store

  1. 如果你的项目中.DS_Store 文件仍然是未跟踪文件,那么直接将 .DS_Store 加入到 .gitignore 文件即可。
1
**/.DS_Store #能囊括项目目录所有子目录下的.DS_Store文件
  1. gitignore文件是为了尚未被追踪的文件之后不会被追踪,但如果你已经把.DS_Store文件加入到追踪文件,那么可以使用git rm --cached filename命令以停止对filename的追踪,将filename删除后再把filename加入.gitignore中。
1
2
3
4
5
6
7
8
# 删除项目中的所有.DS_Store。这会跳过不在项目中的 .DS_Store
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
# 将 .DS_Store 加入到 .gitignore
echo **/.DS_Store >> ~/.gitignore
# 更新项目
git add -A
git commit -m '.DS_Store banished!'
git push

禁用或启用自动生成(没有尝试过)

  • 禁止.DS_store生成:
1
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE
  • 恢复.DS_store生成:
1
defaults delete com.apple.desktopservices DSDontWriteNetworkStores

参考链接:
https://github.com/nodejh/nodejh.github.io/issues/18

https://git-scm.com/docs/gitignore

2. git fetch vs git pull

Git 有两个命令把远程分支获取到本地。一个是git fetch,一个是git pull。原谅我之前没怎么用过git fetch :(

2.1 git fetch

git fetch是从远程获取最新版本到本地,但它不会自动merge。如果想要比较远程最新仓库与本地的不同,就得用git fetch了。我们可以这么用:

1
2
3
4
git fetch origin master	# 获取远程分支
git log -p master..origin/master # 比较差异
git merge origin/master # 合并
##不想合并怎么办??##

也可以

1
2
3
git fetch origin dev:branch1
git diff branch1 
git merge branch1

2.2 git pull

git pull就相当于git fetch+git merge,建议食用git fetch

3. upstream和downstream概述

参考链接

3.1 概念

一个分支的upstream,其实就是本地分支与远程分支做关联,告诉git,默认此分支为推送及拉取的远程分支的信息。如果A库中的分支x被push到B库中的分支y,则y就是x的upstream,而x就是y的downstream。

  1. 对于从远程库中clone或fetch得到的本地分支,都在远程库中有一个upstream分支。

  2. 对于在本地新建的本地分支,如果执行git push origin my_remote_branch_name是不会将my_remote_branch_name设置为新建的本地分支在远程库origin中的upstream分支的,此时,可以执行如下操作,为本地当前分支设置upstream:

1
git push --set-upstream origin my_remote_branch_name

或者

1
git push -u origin my_remote_branch_name

  1. 此外,还可以执行如下操作,直接为新建的本地分支设置在远程库中的upstream分支:
1
git branch --set-upstream my_local_branch_name origin/my_remote_branch_name

3.2 fork+pr模式团队开发实战

比如你使用git团队协作开发,使用clone+pr模式。步骤如下图:

step1

user1在github上创建了名为’repo’的仓库,代码库链接为’https://github.com/user1/repo'

step2

user1在把仓库克隆至本地

1
git clone https://github.com/user1/repo

step3

user2在github上fork了user1的repo,那么他的代码仓库链接为’https://github.com/user2/repo'

step4

user2把他的fork后的仓库克隆了下来。

1
git clone https://github.com/user2/repo

step5

如果user2想加一个指向user1的repo。如果user1的repo有更新,user2即可直接通过git fetch+git merge从user1的repo更新到自己的本地,

1
git remote add upstream https://github.com/user1/repo.git

step6

此时user2可以通过git remote -v命令查看他相关联的分支,

1
2
3
4
origin https://github.com/user2/repository.git (fetch) 
origin https://github.com/user2/repository.git (push)
upstream https://github.com/user1/repository.git (fetch)
upstream https://github.com/user1/repository.git (push)

GIT CONFICLTS

HOW CONFLICTS ARE PRESENTED

Git冲突可以说是合并分支冲突,当两个分支对同一个文件的同一个部分进行修改时,Git不会帮你选择一边的代码,想一想也是合情合理的,这得你们俩私下交流讨论解决,所以你要手动解决冲突。
默认,Git会在冲突区域用一对<<<<====>>>>标记突出显示,例如:

1
2
3
4
5
6
7
8
9
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed.
<<<<<<< yours:sample.txt
Conflict resolution is hard;
let's go shopping.
=======
Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.

其中,====之前的是你的代码,之后的是你的小伙伴的代码。

HOW TO RESOLVE CONFLICTS

当出现冲突的时候,你可以选择两个方法解决:

  • 不合并。你唯一要做的事情就是reset你的HEAD到前一次提交的版本。可以使用git merge --abort命令。
  • 解决冲突并合并。和小伙伴交流讨论后重新编辑冲突区域,尔后,git add 文件,接着git commit文件。

Comments

⬆︎TOP