关于Git的一点总结

Git介绍
Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。
安装配置
安装很简单,可以直接yum,或者源码包安装
git --version 查看版本
Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量。
这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:
  • /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。
  • ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。
  • 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。
配置个人的用户名称和电子邮件地址:
git config --global user.name "XXX"
git config --global user.email "email邮箱"
设置Git默认使用的文本编辑器, 一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Emacs 的话,可以重新设置:
git config --global core.editor emacs
还有一个比较常用的是,在解决合并冲突时使用哪种差异分析工具。比如要改用 vimdiff 的话:
git config --global merge.tool vimdiff
git config --list 查看配置信息
配置信息也可以在 ~/.gitconfig 或 /etc/gitconfig 看到
工作流程
创建仓库
git init 在当前目录初始化仓库
git init newrepo 使用指定目录作为Git仓库
git clone xxxxxx.git/<url> 克隆远程仓库
git clone <repo> <directory> 克隆到指定的目录
基本操作
git add filename 添加文件到staged暂存区,跟踪指定文件
git add . 跟踪所有文件
git remove filename 删除暂存区文件
git status 查看上次提交后的状态
执行 git diff 来查看执行 git status 的结果的详细信息。
git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。
  • 尚未缓存的改动:git diff
  • 查看已缓存的改动: git diff --cached
  • 查看已缓存的与未缓存的所有改动:git diff HEAD
  • 显示摘要而非整个 diff:git diff --stat
git commit -m "XXX" 提交暂存区内容
git commit --amend 修改最后一次提交
git commit -am "XXX" 直接提交working区域内容到history区域
git reset HEAD 取消已缓存的内容,即将缓存区恢复为我们做出修改之前的样子,仅改变暂存区,不改变工作区
git reset HEAD -- filename 取消已缓存的指定文件
git reset --hard HEAD 撤销工作区所有未提交的修改
git rm filename 将文件从缓存区中移除,默认将文件从缓存区和你的硬盘中(工作目录)删除
git rm --cached filename 仅删除缓存区的文件
git mv filenameA filenameB 文件重命名
git revert <commit> 撤销指定的提交
分支管理
git branch 列出所有branch
git branch branchname 创建新分支
git branch -d branchname 删除分支
git checkout branchname 切换到指定分支
git checkout -b branchname 创建并切换到新分支
git merge branchname 将分支branchname合并到当前分支
git rebase branchname 衍合指定分支到当前分支
git checkout filename 从staged区域提取文件覆盖working区域
git reset filename 从history区域提取文件覆盖staged区域
git checkout HEAD filename 从history提取文件覆盖working区域
查看提交历史
git log 查看历史记录
git log -p filename 查看指定文件的提交记录
git blame filename 以列表方式查看
git log --graph 开启拓扑图选项
git log --reverse 逆向显示
git log --stat #查看提交统计信息
git log --author=WilliamWanCN 查看指定用户的提交日志
git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges 指定日期查看,--no-merges 隐藏合并提交
标签
git tag <tagname> 创建轻量标签
git tag -a <tagname> -m "标签注解" 创建附注标签
git tag -d <tagname> 删除标签
git tag 查看所有标签
远程仓库
git remote 查看当前配置的远程仓库
git remote -v 列出详细信息
git remote show <remote> 查看指定远程版本库信息
git remote add <remote> <url> 添加远程版本库
git fetch 从远程仓库下载新分支与数据,执行完后需要执行git merge 远程分支到你所在的分支
git fetch <remote> 从远程库获取代码
git pull 从远端仓库提取数据并尝试合并到当前分支
git pull <remote> <branch> 下载代码并快速合并
git push [alias] [branch] 将本地 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支
git push --tags 上传所有标签
git remote rm [别名] 删除远程仓库
暂存管理
git stash # 暂存
git stash list # 列出所有stash
git stash apply # 恢复最新暂存的内容,该暂存内容还在暂存list中
git stash pop # 恢复最新暂存的内容,该暂存内容不在暂存list中
git stash drop # 删除暂存区
git服务器搭建
1.安装git
安装完git后创建git用户
2.创建证书登录
cd /home/git/ mkdir .ssh chmod 700 .ssh touch .ssh/authorized_keys chmod 600 .ssh/authorized_keys
将客户机的公钥导入到authorized_keys
3.初始化仓库
cd /home mkdir gitrepo chown git:git gitrepo/ cd gitrepo git init first.git
将仓库属性改为git
chown -R git:git first.git
4.克隆仓库
登录到客户机
git clone [email protected]:/home/gitrepo/first.git
10.28.1.104为git服务器ip
 

猜你喜欢

转载自www.cnblogs.com/williamwan/p/10388701.html