15

Git

 5 years ago
source link: http://chchaooo.github.io/2018/06/18/Git/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

概念

  • 工作区: 当前存在于电脑上的文件(不包含.git隐藏文件夹)
  • 隐藏目录.git中保存的东西很多,其中最重要的是称为stage的暂存区,还有Git会自动为我们创建第一个分支master,以及指向master的一个指针叫HEAD

  • 由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以想要上传内容到github,需要向github的账户中设置公钥。
  • 不想让他人看到Git库,1. 交保护费,2. 公司内部搭建一个git服务器
  • 因为创建、合并和删除分支非常快,所以git鼓励使用分支来完成某个任务,合并后再删除分支。这个直接在master分支上工作效果相同,但是过程更安全

git指令

qQRvQbV.png!web

  • 查看git设置信息

    vim ~/.gitconfig
  • 初始化git仓库

    git init
  • 加入管理

    git add readme.txt
  • commit

    git commit -m "fisrt modify"
  • 查询状态

    git status
  • 查询提交日志

    git log
  • 修改版本到某个固定版本

    git reset --hard f7181
  • 修改版本到上一个版本

    git reset --hard HEAD^
  • 抛弃修改

    git checkout -- readme.txt

    把file在工作区的修改全部撤销,这里有两种情况

    1. 把readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态
    2. readme.txt自修改后已经添加到暂存区,又做了修改,现在,撤销修改就回到添加到暂存区后的状态。 总之,就是让这个文件回到最近一次git commit或git add时的状态
  • 撤销暂存区版本,回到最近的历史版本

    git reset HEAD readme.txt
  • 删除文件

    git rm readme.txt
  • 添加远程库关联

    git remote add origin https://github.com/chchaooo/GitDemo3.git
  • 第一次推送到远程库

    git push -u origin master

    后续的推送,可以去掉-u参数(-u参数,不仅会把本地的master分支的内容推送到远程新的Master分支,还会把本地的master分支和远程的Master分支关联起来。)

  • 克隆已有远程库

    git clone https://github.com/chchaooo/chchaooo.github.io.git
  • 将当前修改推送到远程库

    git push origin master
  • 创建dev分支

    git branch dev
  • 查看当前分支

    git branch
  • 切换分支

    git checkout dev
  • 合并分支(先切换到master)

    git merge dev

    merge用于合并指定分支到当前分支

  • 删除分支

    git branch -d dev
  • 创建tag

    git tag V1.0
  • 查看所有的tag

    git tag
  • 推送某个标签到远程

    git push origin V1.0
  • gitignore

    • 存在于Git工作区的根目录下:.gitignore文件
    • 不要从头开始创建.gitignore文件,https://github.com/github/gitignore上有很多模板

参考资料


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK