mindong 发表于 2018-1-15 17:44:23

【记录】GIT 常用命令

GIT 学习笔记  

  
集中化的版本控制系统CVCS
  
分布式版本控制系统DVCS
  

  
Git 基础要点 http://progit.org/book/zh/ch1-3.html
  
1:直接快照,而非比较差异
  
2:近乎所有操作都可本地执行
  
3:时刻保持数据完整性 (Git 使用 SHA-1 算法计算数据的校验 ,40 个十六进制字符(0-9 及 a-f)组成)
  
4:多数操作仅添加数据
  
5:三种状态(已提交(committed),已修改(modified)和已暂存(staged))
  

  
GIT安装http://progit.org/book/zh/ch1-4.html
  

  

  
配置GIT http://progit.org/book/zh/ch1-5.html
  
$ git config --global user.name "John Doe"
  
$ git config --global user.email johndoe@example.com
  

  
查看配置
  

  
git config --list
  

  

  

  

  
GIT 基础
  
初始化仓库
  
$ git init
  
$ git add *.c
  
$ git add README
  
$ git commit -m 'initial project version'
  

  
从现有仓库克隆
  
$ git clone git://github.com/schacon/grit.git
  

  
仓库状态
  
$ git status
  

  
跟踪新文件
  
$ git add fileName
  

  
忽略某些文件
  
$ cat .gitignore
  
*. //忽略以 .o 或 .a 结尾的文件
  
*~ //忽略所有以波浪符(~)结尾的文件
  

  
查看已暂存和未暂存的更新
  
$ git diff
  
$ git diff --cached//已经暂存起来的文件和上次提交时的快照之间的差异
  

  
提交更新
  
$ git commit -m "message" // 简单的提交方式
  
$ git commit -a -m "message" // 跳过add 步骤 把已经跟踪的文件全部提交
  

  
移除文件
  
$git rm fileName
  
$ git rm --cached readme.txt //移除跟踪但不删除文件
  

  
移动文件
  
$ git mv file_from file_to
  

  
日志
  
$ git log
  
$ git log –p -2 // -p 提交内容的差异-2最近两次
  
$ git log --stat//显示简要的增改行数统计
  

  
修改最后一次提交
  
$ git commit --amend
  
//---第2次提交修改了第一次提交
  
$ git commit -m 'initial commit'
  
$ git add forgotten_file
  
$ git commit --amend
  

  
取消已经暂存的文件
  
$ git reset HEAD fileName
  

  
取消对文件的修改(回退到以前未修改的状态) //很有用 也很危险
  
$ git checkout -- fileName
  

  
远程仓库的使用
  

  
查看当前的远程库
  
$ git remote -v // -v 列出远程地址
  

  
添加远程仓库
  
$ git remote add Name git://github.com/paulboone/ticgit.git
  

  
从远程仓库抓取数据
  
$ git fetch
  
$ git pull// 合并远程的全部分支到本地(不确定)
  

  
推送数据到远程仓库
  
$ git push origin master //推送 origin 到 master
  

  
查看远程仓库信息
  
$ git remote show origin
  

  
远程仓库的删除和重命名
  
$ git remote rename pb paul// pb 改成 paul分支对应前缀也会发生变化
  

  
$ git remote rm paul// 貌似删除
  

  

  
打标签 http://progit.org/book/zh/ch2-6.html
  
列显已有的标签
  
$ git tag
  
$ git tag -l 'v1.4.2.*'//搜索标签
  

  
新建标签
  
$ git tag -a v1.4 -m 'my version 1.4' //新建v1.4标签 消息是 my version 1.4
  

  
分享标签
  
$ git push origin //提交 一个标签
  
$ git push origin --tags // 推送所有本地标签
  

  
删除
  
$ git tag -d //删除标签
  
$ git push origin :refs/tags/tagname //删除远程标签
  

  

  
技巧和窍门
  
提示 // 敲两次tab
  
Git 命令别名
  
$ git config --global alias.co checkout // git co 代替了 git checkout
  

  

  
分支
  

  
创建分支
  
$ git branch testing // 创建testing
  
$ git checkout testing// 切换到testing
  
$ git checkout -b iss53 //创建并切换到iss53
  
$ git merge hotfix //把hotfix 分支合并到当前分支
  

  
查看分支
  
$ git branch -v//最后一次commit信息
  
$ git branch --merged | --no-merged//筛选出你已经(或尚未)与当前分支合并的分支
  

  
删除
  
$ git branch -D testing
  

  
推送
  
$ git push origin serverfix//把当前推送到 serverfix分支
  

  
更新同步
  
$ git fetch
  

  
删除远程分支
  
git push origin :branchname
  

  
git branch–r //查看所有分支信息
  

  
//获取远端分支
  
$ git checkout -b sf origin/serverfix
  

  

  
服务器上的GIT
  
---http://progit.org/book/zh/ch4-3.html
  
生成 SSH 公钥---http://github.com/guides/providing-your-ssh-key。
  
$ cd ~/.ssh //公钥的位置
  
$ ls

  
authorized_keys2 >
  
config         >  

  
$ ssh-keygen //如果上面看不到公钥可以用次来创建 会要求输入存放位置 和密码
  

  

  
储藏
  
$ git status //储藏
  
$ git stash list//储藏列表
  
$ git stash apply//应用储藏
  

  
参考资料
  
http://zh.wikipedia.org/wiki/Git
  
http://progit.org/book/zh/
  

  

  

  
页: [1]
查看完整版本: 【记录】GIT 常用命令