手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Git版本管理集锦

[日期:2017-04-23] 来源:Linux社区  作者:Linux [字体: ]

用惯了SVN,突然转到Git难免有点不适,写个笔记好好备忘总结一番。

一、先看历史(imooc上的一个图):

二、Git与Svn

Git跟SVN一样有自己的集中式版本库或服务器。但,Git更倾向于被使用于分布式模式,也就是每个开发人员从中心版本库/服务器上chect out代码后会在自己的机器上克隆一个自己的版本库。可以这样说,如果你被困在一个不能连接网络的地方时,就像在飞机上,地下室,电梯里等,你仍然能够提交文件,查看历史版本记录,创建项目分支,等。对一些人来说,这好像没多大用处,但当你突然遇到没有网络的环境时,这个将解决你的大麻烦。

三、那什么情况推荐使用svn

SVN具有的悲观锁的功能,能够实现一个用户在编辑时对文件进行锁定,阻止多人同时编辑 一个文件。这一悲观锁的功能是 Git 所不具备的。对于以二进制文件 (Word文档、PPT演示稿) 为主的版本库,为避免多人同时编辑造成合并上的困难, 建议使用SVN做版本控制。

四、Git工作原理

五、git安装配置

mac下实际无需安装直接在命令窗口输入git即可弹出安装确认,这种方式安装默认是安装到/usr/bin目录下,且不需要配置环境变量

另一种方式就是手动安装dmg包,需要配置环境变量

官网下载:http://git-scm.com/download/mac

vi ~/.bash_profile
export PATH="/usr/local/git/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:$PATH"
source ~/.bash_profile
重启一下终端,检查是否安装成功
git version

用户信息配置:

$ git config --global user.name "jager"
$ git config --global user.email jager@example.com

或 直接编辑配置文件:

vi ~/.gitconfig
 
[user]
name = jager
email = jager@example.com
[color]
ui = auto
branch = auto
diff = auto
status = auto
[color "branch"]
current = green
local = yellow
remote = red
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
[alias]
st = status
di = diff
ci = commit
co = checkout
br = branch

配置公私钥:

== 生成git密钥 ==
ssh-keygen -t rsa -C "jager@example.com"
秘钥名称填写:git_rsa
其他默认即可

== 配置git密钥 ==
vim ~/.ssh/config
//增加以下内容,IdentityFile路径为你生成的git私钥文件路径
Host XXX
User git
IdentitiesOnly yes
IdentityFile /Users/你的用户名/.ssh/git_rsa

== 配置公钥 ==
拷贝公钥 pbcopy < ~/.ssh/git_rsa.pub 
添加到git管理平台

== FAQ ==
最后一步没配置可能出现错误:
Permission denied (publickey).
fatal: Could not read from remote repository.

六、git常用命令

  •   workspace: 本地工作目录
  •   index:缓存区域,临时保存本地改动
  •   local repository: 本地仓库
  •   remote repository:远程仓库
== git配置 ==
git config --list //查看当前git的配置,Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)

== 查看信息 ==
git log //查看提交记录
git status //查看修改状态
git diff //查看详细修改内容
git show //显示某次提交的内容
git branch //列出所有本地分支
git tag //列出所有tag
git reflog //显示当前分支的最近几次提交

== 新建代码库 ==
git init //在当前目录新建一个Git代码库
git init [project-name] //新建一个目录,将其初始化为Git代码库
git clone [url] //下载一个项目和它的整个代码历史

== 增加/删除 ==
git add [file1] [file2] ... //添加指定文件到暂存区
git add [dir] //添加指定目录到暂存区,包括子目录
git add . //添加当前目录的所有文件到暂存区
git rm [file1] [file2] ... //删除工作区文件,并且将这次删除放入暂存区
git mv [file-original] [file-renamed] //改名文件,并且将这个改名放入暂存区

== 代码提交 ==
git commit -m [message] //代码提交到本地仓库
git commit [file1] [file2] ... -m [message] //提交指定文件到本地仓库
git commit -a //提交工作区自上次commit之后的变化,直接到仓库区
git commit -v //提交时显示所有diff信息
git commit --amend -m [message] //使用一次新的commit,替代上一次提交,如果代码没有任何新变化,则用来改写上一次commit的提交信息

== 分支管理 ==
git branch -r //列出所有远程分支
git branch -a //列出所有本地分支和远程分支
git branch [branch-name] //新建一个分支,但依然停留在当前分支
git checkout -b [branch] //新建一个分支,并切换到该分支
git checkout [branch-name] //切换到指定分支,并更新工作区
git checkout - //切换到上一个分支
git merge [branch] //合并指定分支到当前分支(如master)
git branch -d [branch-name] //删除分支
git push origin --delete [branch-name] //删除远程分支
git branch -dr [remote/branch] //删除远程分支

== 远程同步 ==
git fetch [remote] //下载远程仓库的所有变动,到index
git pull //更新本地仓库至最新改动,到workspace
git remote -v //显示所有远程仓库
git remote show [remote] //显示某个远程仓库的信息
git remote add [shortname] [url] //增加一个新的远程仓库,并命名
git pull [remote] [branch] //取回远程仓库的变化,并与本地分支合并
git push origin master //推送至master分支
git push [remote] [branch] //上传本地指定分支到远程仓库
git push [remote] --force //强行推送当前分支到远程仓库,即使有冲突
git push [remote] --all  //推送所有分支到远程仓库

== 撤销 ==
git reset [file] //重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset --hard //重置暂存区与工作区,与上一次commit保持一致
git checkout //从index恢复到workspace
git checkout . //恢复暂存区的所有文件到工作区
git checkout -- files //文件从index恢复到workspace
git checkout HEAD -- files //文件从local repository复制到workspace

== 冲突解决 ==
git diff //对比workspace与index
git diff HEAD //对于workspace与最后一次commit
git diff <source_branch> <target_branch> //对比差异
git add <filename> //修改完冲突,需要add以标记合并成功 

七、git使用流程规范【重要】

下面是ThoughtBot 的Git使用规范流程,推荐使用:

Create a local feature branch based off master.

git checkout master
git pull
git checkout -b <branch-name>

Rebase frequently to incorporate upstream changes.

git fetch origin
git rebase origin/master

Resolve conflicts. When feature is complete and tests pass, stage the changes.

git add --all

When you've staged the changes, commit them.

git status
git commit --verbose

Write a good commit message. Example format:

Present-tense summary under 50 characters

* More information about commit (under 72 characters).
* More information about commit (under 72 characters).

http://project.management-system.com/ticket/123

If you've created more than one commit, use git rebase interactively to squash them into cohesive commits with good messages:

git rebase -i origin/master

Share your branch.

git push origin <branch-name>

Submit a GitHub pull request.

Ask for a code review in the project's chat room.

总结大致如下:

  •   新建分支
  •   提交分支
  •   撰写commit信息
  •   与主干同步
  •   合并commit
  •   推送到远程仓库
  •   发出pull request,请求别人进行代码review

Git 教程系列文章: 

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git使用图文详细教程  http://www.linuxidc.com/Linux/2016-11/136781.htm

Ubuntu Git安装与使用 http://www.linuxidc.com/Linux/2016-11/136769.htm

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Git从入门到学会 http://www.linuxidc.com/Linux/2016-10/135872.htm

Git基本操作详解 http://www.linuxidc.com/Linux/2016-10/135691.htm

Git 的详细介绍请点这里
Git 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-04/143061.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款