Git

一、安装Git

在 Mac OS 上安装Git:直接从AppStore安装Xcode,Xcode集成了Git,不过默认没有安装,你需要运行Xcode,选择菜单"Xcode"->"Preferences",在弹出窗口中找到"Downloads",选择"Command Line Tools",点击"Install"就可以安装完成了。

二、常用命令

  • 找一个合适的地方创建一个空目录
$ mkdir learngit
$ cd learngit
$ pwd
/Users/mukang/Documents/Work/learngit
  • git init把这个目录变成Git可以管理的仓库
$ git init
Initialized empty Git repository in /Users/mukang/Documents/Work/learngit/.git/
  • git add告诉Git,把文件添加到暂存区
$ git add readme.txt

如果想要添加全部到暂存区,则使用命令:

$ git add .
  • git commit告诉Git,把暂存区的所有内容提交的当前分支
$ git commit -m 'wrote a readme file'
[master (root-commit) 83fd321] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt
  • git status查看仓库当前状态
$ 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:   readme.txt
//
no changes added to commit (use "git add" and/or "git commit -a")
  • git diff查看difference
$ git diff
**diff --git a/readme.txt b/readme.txt**
**index d8036c1..013b5bc 100644**
**--- a/readme.txt**
**+++ b/readme.txt**
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.
\ No newline at end of file
  • git log查看提交历史记录
$ git log
commit d154630a46829754d044513d72993fe9b0f09846
Author: mukang 
Date:   Tue Oct 11 11:47:39 2016 +0800
//
    append GPL
//
commit 966b6ff2c22489a9d3c51f99208ec07c8a1960de
Author: mukang 
Date:   Tue Oct 11 11:46:22 2016 +0800
//
    add distributed
//
commit 83fd321904b5247f23122b9361c8e7cc4539e060
Author: mukang 
Date:   Tue Oct 11 11:27:48 2016 +0800
//
    wrote a readme file

如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数

$ git log --pretty=oneline
d154630a46829754d044513d72993fe9b0f09846 append GPL
966b6ff2c22489a9d3c51f99208ec07c8a1960de add distributed
83fd321904b5247f23122b9361c8e7cc4539e060 wrote a readme file

如果只想显示commit id的前几位,可以再加上--abbrev-commit参数

$ git log --pretty=oneline --abbrev-commit
d154630 append GPL
966b6ff add distributed
83fd321 wrote a readme file
  • 回退版本
    在Git中,用HEAD表示当前版本,也就是最新的提交,上一版本就是HEAD^,上上一个版本就是HEAD^^,往上100个版本写成HEAD~100
    现在我们回退到上一个版本,使用命令git reset:
$ git reset --hard HEAD^
HEAD is now at 966b6ff add distributed

想要回到某个版本,可以指定commit id:

$ git reset --hard d154630
HEAD is now at d154630 append GPL

git reflog命令可以显示出你每次输入的命令:

$ git reflog
d154630 HEAD@{0}: reset: moving to d154630
966b6ff HEAD@{1}: reset: moving to HEAD^
d154630 HEAD@{2}: commit: append GPL
966b6ff HEAD@{3}: commit: add distributed
83fd321 HEAD@{4}: commit (initial): wrote a readme file

三、分支

  • 查看分支:git branch

  • 创建分支:git branch

  • 切换分支:git checkout

  • 创建+切换分支:git checkout -b

  • 合并某分支到当前分支:git merge

  • 删除分支:git branch -d

  • 推送分支到远程:git push origin

  • 删除本地分支后删除远程分支:git push origin :

你可能感兴趣的:(Git)