Git入门一

Git–分布式版本控制系统

由linux创始人Linus Benedict Torvalds(林纳斯·本纳第克特·托瓦兹)编写,所以其命令与linux的差不多(过两天再写关于linux的博客,也是初级入门)

与GitHub的关系

GitHub是托管Git的服务器,可托管Git的服务器有很多,自己在局域网也可以搭建一个Git服务器。

Git命令

以下的所有命令都是在Git Bash here中实现的
git 的自有命令都是以 git开头
git help command 以查看命令解释

设置Git的签名

git config --global user.name ‘username’
git config --global user.email ‘email’
该设置在Git显示谁提交了该文件,区分不同的开发人员,这里是设置了系统用户级别的签名,其作用域是当前这个系统。
项目级别/仓库级别:仅在当前本地库范围内有效
  git config user.name name
常用系统用户级别
这里的签名和登陆远程库(代码托管中心GitHub)的账号密码没有任何关系。

Git仓库结构

  git status : 检查一下Git的当前状况,有几个新文件被修改的文件
  git add fileName : 将工作区的文件提交到暂存区
  git commit -m “comment” : 将文件提交到Git仓库,comment是这次提交的备注(要做这次修改的原因)
Git入门一

Git Bash here

参考linux,在任意目录点击鼠标右键,出现的列表中的“Git Bash here”与linux的打开终端类似,且其打开的路径就是当前路径。

创建git仓库:

将一个文件夹变成Git仓库
  git init

远程仓库GitHub

Git入门一

  • 下载
    前提是把GitHub中的project clone下来(只用clone一次,像GitHub中的fork?)
      git clone 仓库地址
    仓库地址
    Git入门一
  • 上传
    把本地的仓库更新到GitHub上
      git push

搭建个人静态博客

创建的新的project名称必须是
  用户名.github.io

给某个project做个网页

模板网页,就那几个,
在project的Settings里,设置完就好。
Git入门一

查看修改日志

git log
  查看日志,点击“空格”下一页,“b”上一页,“q”退出。
git reflog
  显示每次修改的哈希值,有助于head指针移动时定位。

版本切换

git reset --hard 版本号(常用)
  版本号应该是其hash得到的值,
  原理是改变了head的指针,
git reset --hard HEAD^
  一个 ^ 退一个
git reset --hard HEAD~n
  回退n步

git reset 有参数 --soft --mixed --hard,三个有所区别
–soft
  Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files “Changes to be committed”, as git status would put it.

–mixed
  Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
  If -N is specified, removed paths are marked as intent-to-add (see git-add(1)).

–hard
  Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.