回滚、还原、重置和变基

  • 关键词:Git For Teams – 读书笔记

Part3 Git托管平台

GitHub

git remote add origin https:…(克隆仓库)

git push –set-upstream origin master(设置远程仓库中的上游分支)

git checkout master

git pull –rebase=preserve

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

创建仓库的克隆:

git clone https:…

mv …

cd …

git branch –move master upstream(创建一个上游分支)

git checkout -b master

为工作副本添加一个远程仓库:

git remote rename origin upstream

git remote add origin https:…

git push -u origin master

现在,你同时拥有一个名为upstream的分支和upstream的远程分支。

git checkout upstream(检测上游分支以获得更新)

git pull –rebase=preserve

git diff master upstream(比较上游的更改和你本地的工作)

可以在log命令后添加一些华丽的参数,查看特定提交的摘要:

git log –cherry-mark –left-right –oneline master..upstream

这些参数在提交开头添加一个符号,表明这个更改是列表中第一个分支还是第二个分支引入的。

git checkout master(并入上游更改)

git rebase upstream

使用issue跟踪更改

在公开项目中,issue通常被找到bug的用户打开。

授权共同维护

New collaborator(新建协作者)

评审并接受拉取请求:

git checkout master

git remote add username git://…

git fetch username(在添加远程仓库后,你现在必须下载贡献者的工作)

git merge –no-ff username/branch_name(如果你希望进行一些修改来清理几个空格问题,或者修复一个拼写错误,你可以选择–no-commit)

git merge –no-ff –no-commit username/branch_name(如果你决定每个更改必须经历拉取请求环节,这个参数或许就不合适)

git push origin master

Bitbucket

GitLab

配置Git

git config –get user.name(显示一个设置的值)

git config –list

git help config

git config –global user.name ‘Your Name’(配置你的名字)

git config –global user.email ‘your email’

更改提交说明编辑器:

git config –get core.editor

git config –global core.editor mate -w(使用Textmate)

git config –global core.editor subl -n -w(使用Subline)

如果是更改windows的编辑器,则需要加入应用文件的完整路径。

git config –global core.editor ‘“C:/…” –nofort’

添加颜色:

git config –global color.ui true

git config –global diff.ui auto

忽略系统文件

避免Git将系统文件提交到任何你创建的本地仓库,一旦你选好了想要忽略的文件列表,进行如下:

创建.gitignore_global

git config –global core.excludesfile ~/.gitignore_global

或者创建.gitignore

git add .gitignore

git commit -m “adding list of files to be ignored”

行结束符

如果是跨平台工作,开发者分别使用OS X、Linux和Windows。

你应该在全局设置这个行结束符,但同时在每个仓库中添加这个设置,从而确保没有显示设置行结束符的开发者不会遇到问题。

git config –global core.autocrlf input

需要在你的仓库中添加一个.gitattributes文件,标记正确的行结束符、应该被改正的文本文件和不应该被修改的二进制文件。

设置所有文件的默认行为:text=auto

列出应使用系统相关的行结束符的文本文件:.php text、.html text、*.css text

列出应使用CRLF行结束符且不根据本地操作系统转换的文件:*.sln text eol=crlf

列出所有不应进行修改的二进制文件:*.png binary

将文件添加到暂存区索引:

git add .gitattributes

git commit -m “Require the right line endings for everyone, forever.”

创建你的SSH秘钥

ssh-keygen -t rsa -b 4096 -C “your email”

  • 系统将提示你输入以下信息:
    • 文件位置(回车默认位置继续)
    • 密码(可选项)

eval “$(ssh-agent -s)”

ssh-add ~/.ssh/id_rsa

获得你的ssh公钥:

OS X: cat ~/.ssh/id_rsa.pub / pbcopy

Linux: cat ~/.ssh/id_rsa.pub

Windows: clip < ~/.ssh/id_rsa.pub