git三个工作区和文件的四种状态

工作目录(workspace)、暂存区(index)、本地仓库(repo)

未跟踪(untracked)、已暂存(staged)、已修改(modified)、已提交(committed)

  1. 当在工作目录中新加入一个文件时,它处于未跟踪状态,这表示其没有纳入Git的版本控制。
  2. 通过 git add 命令可以将其加入跟踪,并同时放入暂存区。
  3. 一个已经被跟踪的文件,如果没有做过新的修改,就是未修改状态。
  4. 一旦对其做了改动,就变成了已修改状态。通过 git add 命令可以将已修改的文件放入暂存区。
  5. 初次克隆某个仓库时,工作目录中所有文件都是已跟踪且未修改的状态。
  6. git commit 命令会将暂存区中的文件提交至HEAD所指向的分支。当被commit之后,暂存区的文件将回到未修改状态。

 

工作目录 --> 暂存区 --> 本地仓库的某一个分支 --> 远程仓库的一分支

 

 

常用命令参考:http://www.yiibai.com/git/git_pull.html

 

 

git配置

    git config --global user.name "xxx"                       # 配置用户名
    git config --global user.email "xxx@xxx.com"              # 配置邮件

    git config --global color.ui true                         # git status等命令自动着色
    git config --global color.status auto
    git config --global color.diff auto
    git config --global color.branch auto
    git config --global color.interactive auto

 

初始化仓库

[hinzer@VM_16_8_centos learngit]$ git init				#初始化仓库


提交文件

[hinzer@VM_16_8_centos learngit]$ git add readme.txt        #将更改过的文件readme.txt放至暂存区(index)
[hinzer@VM_16_8_centos learngit]$ git commit -m "add distributed"     #提交到本地仓库,-m后面是提交描述

删除or重命名文件

[hinzer@VM_16_8_centos learngit]$ rm <file>            #删除工作目录的文件
[hinzer@VM_16_8_centos learngit]$ git rm <file>        #删除本地仓库的文件    
[hinzer@VM_16_8_centos learngit]$ git checkout -- <file>  #从本地仓库中恢复文件到工作目录

[hinzer@VM_16_8_centos learngit]$ git mv README README2     # 重命名文件README为README2

退回上一个版本

[hinzer@VM_16_8_centos learngit]$ git reset --hard HEAD^
[hinzer@VM_16_8_centos learngit]$ git reset --hard <commit_id>             # 将当前版本重置为commit号对应的版本

查看仓库状态

[hinzer@VM_16_8_centos learngit]$ git status     #查看当前版本状态(是否修改)
[hinzer@VM_16_8_centos learngit]$ git diff         #显示所有未添加至index的变更
[hinzer@VM_16_8_centos learngit]$ git log       #查看历史提交记录,加上 --pretty=oneline 参数可以简短显示


本地仓库与远程仓库的交互

[hinzer@VM_16_8_centos learngit]$ git remote add origin git@gitee.com:xxx/xxx.git  #关联远程仓库,使用ssh地址
[hinzer@VM_16_8_centos learngit]$ git remote -v                           # 查看远程库的信息

[hinzer@VM_16_8_centos learngit]$ git push origin master        #本地仓库推送到远程仓库,首次可用 -u 参数;暴力推送可加 -f 参数
[hinzer@VM_16_8_centos learngit]$ git pull origin master        #从远程仓库下载项目到本地

[hinzer@VM_16_8_centos test]$ git clone git@gitee.com:xxx/xxx.git  #克隆远程仓库成为本地仓库


其他命令

[hinzer@VM_16_8_centos learngit]$ git reflog    #查看历史命令记录