Git简明手册

  • 创建版本库
$ git init test
Initialized empty Git repository in /home/tstcit/Git/test/.git/
  • 添加文件修改到暂存区
$ git add read.txt
  • 提交暂存区文件到仓库
$ git commit -m "hello Git"
[master (root-commit) 57cb487] hello Git
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 read

注意使用-m命令来写更新描述

  • 查看状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)
        modified:   read
no changes added to commit (use "git add" and/or "git commit -a")
  • 对比文件区别
$ git diff read
diff --git a/read b/read.txt
index e69de29..802d8e8 100644
--- a/read.txt
+++ b/read.txt
@@ -0,0 +1 @@
+sfds
  • 查看日志
$ git log
commit 57cb48772f15d42d8aba170b21cfd85b50c07818
Author: lee_3do 
Date:   Thu Nov 12 16:37:44 2015 +0800

    hello Git
  • 回退版本
$ git reset --hard 57cb48772f15d42d8aba170b21cfd85b50c07818
HEAD is now at 57cb487 hello Git

57cb48772f15d42d8aba170b21cfd85b50c07818是某个commit id,可以指定任意一个存在的commit id,回退或者前进道指定版本.
HEAD指向的版本就是当前版本

  • 查看所有操作命令
$ git reflog
57cb487 HEAD@{0}: reset: moving to 57cb48772f15d42d8aba170b21cfd85b50c07818
f78caa3 HEAD@{1}: commit: s
57cb487 HEAD@{2}: commit (initial): hello Git
  • 丢弃工作区的修改
$ git checkout -- read.txt

这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。

  • 丢弃暂存区的修改
    如果我们已经把修改add到缓存区,我们需要先丢弃暂存区的修改.
$ git reset HEAD readme.txtUnstaged changes after reset:M readme.txt

然后在执行 git checkout -- read.txt命令丢弃工作期的修改.

  • 删除文件
$ git rm read
  • 创建分支
$ git branch dev
  • 切换分支
$ git checkout dev
  • 查看分支信息
$ git branch
*dev
 master

*号后表示当前分支,master是默认分支

  • 分支整合
$ git merge dev
Updating d17efd8..fec145a
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
  • 删除分支
$ git branch -d dev
Deleted branch dev (was fec145a).
  • 存储工作现场
$ git stash
Saved working directory and index state WIP on dev: 6224937 add mergeHEAD is now at 6224937 add merge
  • 列出工作现场
$ git stash list
stash@{0}: WIP on dev: 6224937 add merge
  • 恢复指定工作现场
$ git stash apply stash@{0}
  • 工作现场出栈
$ git stash pop

恢复的同时把stash内容也删了.

  • 查看远程库信息
git remote -v
  • 从本地推送分支
git push origin branch-name
  • 在本地创建和远程分支对应的分支
git checkout -b branch-name origin/branch-name
  • 建立本地分支和远程分支的关联
git branch --set-upstream branch-name origin/branch-name
  • 从远程抓取分支
git pull
  • 打标签
$ git tag v1.0
  • 显示标签信息
git show 
  • 创建带有说明的标签
$ git tag -a v0.1 -m "version 0.1 released" 3628164

-a指定标签名,-m指定说明文字,3628164是commit id.

  • 忽略某些文件
    使用.gitignore文件.

  • 配置别名

$ git config --global alias.st status

以后st就表示status.
也可以修改配置文件.git/config,或者.gitconfig.

你可能感兴趣的:(Git简明手册)