代码改变世界

2018-05-08 git学习

2018-05-09 16:53  受匕图灵  阅读(481)  评论(0编辑  收藏  举报

[weis@localhost ~]$ git
usage: git [--version] [--help] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

最常用的 git 命令有:
   add        添加文件内容至索引
   bisect     通过二分查找定位引入 bug 的变更
   branch     列出、创建或删除分支
   checkout   检出一个分支或路径到工作区
   clone      克隆一个版本库到一个新目录
   commit     记录变更到版本库
   diff       显示提交之间、提交和工作区之间等的差异
   fetch      从另外一个版本库下载对象和引用
   grep       输出和模式匹配的行
   init       创建一个空的 Git 版本库或重新初始化一个已存在的版本库
   log        显示提交日志
   merge      合并两个或更多开发历史
   mv         移动或重命名一个文件、目录或符号链接
   pull       获取并合并另外的版本库或一个本地分支
   push       更新远程引用和相关的对象
   rebase     本地提交转移至更新后的上游分支中
   reset      重置当前HEAD到指定状态
   rm         从工作区和索引中删除文件
   show       显示各种类型的对象
   status     显示工作区状态
   tag        创建、列出、删除或校验一个GPG签名的 tag 对象

usage整理:
usage: git    [--version]
        [--help]
        [-c name=value]
        [--exec-path[=<path>]]
        [--html-path]
        [--man-path]
        [--info-path]
        [-p|--paginate|--no-pager]
        [--no-replace-objects]
        [--bare]
        [--git-dir=<path>]
        [--work-tree=<path>]
        [--namespace=<name>]
        <command> [<args>]


看不懂:
基本概念小节看不懂
工作区:就是你在电脑里能看到的目录。
暂存区:英文叫stage, 或index。一般存放在 ".git目录下" 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
工作区    个人电脑上的目录    
暂存区(索引)    工作区/.git/index文件    
版本库    工作区/.git目录
疑问:
如何还原已修改但未添加缓存的文件???
如何还原已提交但未合并的文件???

学习于菜鸟教材网

git init /home/weis/mygit/    #在目录下创建新的 Git 仓库

git clone                #拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改
git clone <repo>
git clone <repo> <directory>
• repo:Git 仓库。
• directory: 本地目录。

git add                #将文件添加到缓存
$ git add README hello.php

git status                #查看在上次提交之后是否有改动
git status -s 结果和教材中不一致
[weis@localhost mygit]$ git status -s
[weis@localhost mygit]$ vim README
[weis@localhost mygit]$ git status -s
 M README
[weis@localhost mygit]$ git add README
[weis@localhost mygit]$ git status -s
M  README

git diff                #查看执行 git status 的结果的详细信息
$ git diff                #查看尚未缓存的改动
$ git diff --cached        #查看已缓存的改动
$ git diff HEAD            #查看已缓存的与未缓存的所有改动
$ git diff --stat            #显示摘要而非整个 diff

git commit                #将缓存区内容添加到仓库中。是git add命令的下一步
参数:
-m    如果没有设置 -m 选项, Git 在它的配置中寻找相关信息,如果找不到,则会打开一个编辑器以填写提交信息
-a    跳过添加缓存步骤(git add 命令)直接添加到仓库

git reset HEAD            #将git add 命令添加过的文件从缓存中取消
$ git reset HEAD -- hello.php

git rm                #从 Git 中移除某个文件
git rm <file>            #要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交此命令
git rm -f <file>            #删除之前修改过并且已经放到暂存区域的文件
git rm --cached <file>        #从暂存区域移除(仅是从跟踪清单中删除),但仍然希望保留在当前工作目录中

git mv                #移动或重命名一个文件、目录、软连接

git branch                #列出分支基本命令
git branch (branchname)        #创建分支命令
git branch -d (branchname)    #删除分支命令

git checkout (branchname)    #切换分支命令
git checkout -b (branchname)    #创建新分支并立即切换到该分支下

git merge                #合并分支命令


git log                #列出历史提交记录
--oneline                查看历史记录的简洁的版本
--graph                查看历史中什么时候出现了分支、合并
--reverse                #逆向显示所有日志
--author=用户名            #查找指定用户的提交日志
--since
--before={3.weeks.ago}        #查看三周前的提交
--until
--after={2010-04-18}        #查看在四月十八日之后的提交
--no-merges                #隐藏合并的提交
--decorate                #查看标签

git tag                #查看所有标签
可选参数:-a    给特别的提交快照打上标签:创建一个带注解、注解人、注解时间的标签
$ git tag -a v1.0
$ git tag -a v0.9 85fc7e7

git tag -a <tagname> -m "runoob.com 标签"    #指定标签信息命令
git tag -s <tagname> -m "runoob.com 标签"    #PGP 签名标签命令


生成 SSH Key:
$ ssh-keygen -t rsa -C "296575445@qq.com"
/home/weis/.shh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwnnSc/7SavLIO0Tb8eX2BAocQyoV98CN2baSepn6nByumSoDYeleEjkWNS63TAaUeJJOmukFH/JmEoSNjS+ajB4SnvA2EdvGCQ3vtnHDlwlAbvAnA/K/ELLkDyTTfaJbmf2ekBbbeEE1ZkRkaLCy/tRE03vfJre1yQgISewlrFUZEnGPwh01cn2t7jo1vVHX7XUyIU/ocC/4xk78VW9jlDq4prGQx7eLlIgr1UdKQ+fGoItGjBFyPFZei6zMHInCHJFEcBjCpG/3M1Qa6ReDvYytTMiZ744wLf8b6xlgSFez+3rrjjdV6INvzNjxZgGAv7UPMLj4a2Y6YIX0do+oH weis@localhost.localdomain
添加成功:
mygit
Fingerprint: da:e1:37:6b:a6:5e:27:90:7c:ec:b8:d0:44:84:ce:92 Added on 9 May 2018 Never used — Read/write

$ ssh -T git@github.com

git remote add [shortname] [url]    #提交到Github
[weis@localhost weis-git-test20180508]$ git remote add origin https://github.com/githubweis/weis-git-test20180509.git
#$ git remote add origin git@github.com:tianqixin/runoob-git-test.git
$ git push -u origin master

$ git remote            #查看当前配置有哪些远程仓库
origin
$ git remote -v            #看到每个别名的实际链接地址
origin    https://github.com/githubweis/weis-git-test20180509.git (fetch)
origin    https://github.com/githubweis/weis-git-test20180509.git (push)

git fetch [alias]            #1 、从远程仓库下载新分支与数据
git merge [alias]/[branch]    #2 、从远端仓库提取数据并尝试合并到当前分支
[weis@localhost weis-git-test20180508]$ git fetch origin
[weis@localhost weis-git-test20180508]$ git merge origin/master

git push [alias] [branch]    #将 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支
$ git push origin master

git remote rm [别名]        #删除远程仓库
$ git remote rm origin2