Git_troubleshooting总结

Q1: git push -u参数是什么意思

http://www.zhihu.com/question/20019419

Git内部原理

Git工作流总结

一.综述

二.分布式版本控制GIT工作流

场景1:

oy需要在办公室(保密局域网)内多台机器上协作开发,还需要将代码和我们实验室邵孔东协作开发,但是邵孔东不能连接到oy办公室。

1.在办公室建立中央集中式git服务器(解决局域网内的代码协作开发)
2.oy与邵孔东使用本地或者SSH协议实现在实验室的代码协作开发

oy办公室为代码一级中心,oy代码向中心提交
oy来我们实验室之后,作为中心,邵孔东代码向oy代码库提交
邵孔东走如下操作:
oy在master分支上开发,此时可以新建一个host分支来专门用户和邵孔东合并用,这个host应该为邵孔东的upstream分支。

Solution:

OY:
git branch host

SKD:
git add . && git commit -m “local snapshot”
git remote add oy ssh://oy@oy_host_ip:related_to_oy_home/repo/.git
git fetch oy host (将远端的信息和数据拿回来,通过这条命令git才有了oy/host这个变量)(注意 不是 oy/host,远端交互都是两个参数,本地分支操作室用/连起来的一个参数)
git branch oy_host -t oy/host(建立本地分支跟踪刚拿回来的oy的主分支)
git checkout master
git merge oy_host(将那会儿来的分支与自己合并)
有冲突则解决冲突,没有冲突则做版本并提交到oy/host分支
git push oy master:host

或者
git remote add oy…
git fetch oy host
git checkout oy/host
git checkout -b oy_host

OY:
git checkout master
git merge host

oy的repo作为邵孔东的remote节点,当邵孔东要想oy的repo/.git的master分支 push的时候,oy的master复制必须不是checkout状态,也就是当前分支不能使master。解决方法有两种:
远端仓库为bare仓库,即只有.git的内容,没有workingtree
git checkout -b idel新建一个空闲分支,checkout出来,将master分支空闲出来。
比如以下:将本地的u_track_fei_master
再次提交成功

场景2:

oy的repo作为邵孔东的remote节点,当邵孔东要想oy的repo/.git的master分支 push的时候,oy的master复制必须不是checkout状态,也就是当前分支不能使master。解决方法有两种:
远端仓库为bare仓库,即只有.git的内容,没有workingtree
git checkout -b idel新建一个空闲分支,checkout出来,将master分支空闲出来。

Solution:

oy的repo作为邵孔东的remote节点,当邵孔东要想oy的repo/.git的master分支 push的时候,oy的master复制必须不是checkout状态,也就是当前分支不能使master。解决方法有两种:
远端仓库为bare仓库,即只有.git的内容,没有workingtree
git checkout -b idel新建一个空闲分支,checkout出来,将master分支空闲出来。

我的Git命令箱

一.前戏

三种状态:

  • modified 已修改
  • staged 已暂存
  • committed 已提交

三种工作区域:

  • working directory 本地仓库
  • staging directory 暂存仓库
  • git directory git内部仓库

二.配置

  • git config —system user.email=feilunzhou@126.com 系统级别配置/etc/gitconfig
  • git config —global user.name=“feilunzhou” 用户级别配置 ~/.gitconfig
  • git config user.email=feilunzhou@126.com 仓库级别配置 .git/config
  • git config —global alias.co checkout 配置别名
  • git config —global alias.last ‘log -l HEAD’ 使用git last就可以查看最后一次提交信息了
  • git config —list 查看配置信息
  • git config -l 查看配置信息
  • vim .gitignore
  • gitignore工具: https://www.gitignore.io/
  • gitignore模版: https://github.com/github/gitignore

三.状态操作:

  • git status
  • git reflog 记录每一步命令history
  • git show

四.本地提交

  • git init
  • git init —bare
  • git add .
  • git commit -m “story”
  • git commit -am “msg”
  • git commit -a -m ‘made a chage’
  • git commit -a 跳过暂存区提交,跳过git add步骤
  • git commit —amend 修改最后一次提交,不产生一个新提交
  • git status
  • git diff
  • git diff —cached
  • git rm file.py
  • git rm —cached readme.py
  • git mv file_from file_to

五.历史操作

原则:版本控制就像是历史书,只记录历史,不应该抹掉历史。

  • tig
  • git log
  • git log -p -2
  • git log —stat
  • git log —pretty=oneline
  • git log —pretty=format:”%h - %an, %ar : %s”
  • git log —pretty=format:”%h %s” —graph
  • git log —since=2.weeks
  • git log —author=feilunzhou
  • git reset HEAD file.txt 取消文件暂存(与git add互逆)
  • git unstage 同git reset HEAD
  • git checkout — 丢弃对文件的修改,恢复到此文件的上一个commit状态
  • git tag 列出所有标签
  • git tag -l ‘v1.4.2.*’ 列出匹配的标签
  • git tag -a v1.4 -m ‘my version msg‘创建一个含附注的标签
  • git tag -s v1.5 -m ‘my signed 1.5 tag’创建签署标签
  • git show v1.4察看标签的信息
  • git tag v1.6-lw 创建轻量级标签(不能用-a -s -m)
  • git push origin 分享标签,git push不会把标签发送到server,需要显式指定
  • git push origin —tags 推从所有本地标签到origin上

六.版本回退

  • git commit —amend
  • git checkout — filenaem 丢弃修改
  • git unstage filename 不加入监控
  • git reset HEAD filename 不加入监控,如上
  • git reset —hard HEAD^
  • git reset —hard 3628164
  • git revert HEAD
  • git revert HEAD^

注意一下三中标识的区别

  • HEAD@{3}这种是索引 git reflog中的记录,不是前三次提交
  • HEAD^是指上上次提交
  • HEAD~3是上三次提交。

分清楚以下三命令的区别:

  • git reset 操作当前分支头(即HEAD指向的那个分之ref,也就是同时移动HEAD和master,然后根据参数重置index和工作区)
  • git revert 回退到一个版本,它之后的内容返回到暂存区中
  • git checkout (1,只移动HEAD,2.从某个地方导出文件到workingtree;或者后面跟文件名,将文件从版本库中移到工作目录)
  • git stash

git revert:(撤销,不丢失历史)

用一个新的提交会恢复到之前的一个状态。
revert后面接的事一个合并后的分支(有两个父亲),则需要用-m指定那个父亲。
git revert sha1 使用tig查看sha1值

git reset:(重新设置HEAD和branch ref)

同时移动HEAD和HEAD指向的分支头
—soft 移动之后不改变index和workingtree的内容
—mixed(默认)移动之后改变index内容与HEAD相同,但是不改变workingtree内容
—hard 移动之后将index和workingtree的内容都设置为HEAD一样。

git reset filename 将暂存区中的filename与HEAD的filename文件状态同步(指向同一个object)。
git reset HEAD filename 将暂存器中filename文件的状态与HEAD同步(指向同一个object)。

git checkout:(拿出来放到workingtree)
从某个地方提出文件到workingtree(index 或者分支头或者sha1)
只影响workingtree和HEAD值
不会影响index的内容和状态

git checkout — filename、git checkout — * 用index区的内容来替换workingtree的内容(并且将它unstage,就是git add的反操作)(workingtree中有,但是从类没有加入到版本中的文件,就自动ignore了,什么都不管)
git checkout branch_name 切换到某个分支,也就是用这个分支的内容来奇幻workingtree的内容
git checkout sha1 用某个sha1提交来替换当前的workingtree 这时候HEAD为detached状态。可以在这个点建立新分支

合并分支的回退:

git reflog来进行。
git reset [sha1]来进行,可通过tig来获知sha1值

七.分支相关:

  • git branch -a 列出所有分支(本地+远端)
  • git branch 列出本地分支、表明所在分支
  • git branch -v 列出分支详情(分支名,commit sha1, latest commit msg)
  • git branch —merged 查看哪些分支已经被并入当前分支
  • git branch —no-merged察看哪些分支没有被并入当前分支
  • git checkout -b 以当前所在的最新提交新建分支,名为,并切换到分支
  • git branch 以当前所在的最新提交新建分支,名为
  • git branch -d 删除分支
  • git checkout 切克闹! dev 切换到dev分支
  • git merge 分支与当前分支合并,并产生一个提交,可能发生冲突,

远端分支相关

  • git merge origin/serverfix 将fetch下来的远端分支合并到当前分支
  • git checkout -b serverfix origin/serverfix 新建一个分支在fetch下来的分之上
  • git mergetool
  • git fetch origin branch1 && git merge origin/branch1
  • git checkout -b branch1 origin/branch1 (跟踪分支)
  • = git checkout —track origin/branch1
  • =git checkout -b origin/branch1
  • git branch branch1 origin/branch1
  • git push origin :serverBranch 删除远端originBranch
  • git rebase 衍合(分支整合有两种方式:merge和rebase)(为造成一个等价的线性的进化版本)

分支的衍合: http://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E8%A1%8D%E5%90%88

八.远端交互:

  • git clone git://github.com/apc.git
  • git clone git@github.com/repo.git
  • git clone file:///absolute/path/to/local/repo
  • git remote -v/ —verbose 本仓库配置的有哪些远端仓库
  • git remote add dev git://github.com/repo.git
  • git remote show 显示远端仓库信息
  • git remote rename 修改远端地址别名
  • git remote rm 删除远端地址
  • git pull = git fetch + git merge
  • git push 将当前内容同步到远端,如果远端没有这个分支,且对远端有写权限,在远端创建改分支。
  • git push push到远端不同名的分支。
  • git push origin 分享标签,git push不会把标签发送到server,需要显式指定
  • git push origin —tags 推从所有本地标签到origin上
  • git push —all 推送所有的refs
  • git fetch 默认为git fetch origin
  • git fetch 这里的name是git remote add 进去的,去远端同步,更新本地origin/master到最新位置
  • git fetch origin branch1 && git merge origin/branch1
  • git checkout -b branch1 origin/branch1
  • = git checkout —track origin/branch1 (跟踪分支,会在本地创建同名分支)
  • =git checkout -b origin/branch1
  • git branch branch1 origin/branch1
  • git push origin :serverBranch 删除远端originBranch
  • git rebase

pull和push都可理解为两个过程的合操作,操作对象为两个分支。

九.Git服务器

协议:

  • file:///abs/path/to/repo
  • git://
  • ssh:
  • http:
  • https:

Hello World

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment