---
title: "Git笔记"
author: "zhb8015"
date: "2014-04-17T23:20:35Z"
tags: []
categories: ["非技术"]
slug: "git"
draft: false
lastmod: "2014-06-09T14:00:04Z"
id: 2048052
url: "https://peter8015.iteye.com/blog/2048052"
---

1、初始化

$ git init

2、显示当前工作目录状态

$ git-status - Show the working tree status

```
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   1.txt

no changes added to commit (use "git add" and/or "git commit -a")
```

 

3、恢复到某一版本

$ git reset --hard "378a57"

4、git log

$ git-log - Show commit logs

```
commit ee62fc68906b810d93ef7576390fc3122f1300b1
Author: peter <peter8015@126.com>
Date:   Thu Apr 17 21:29:27 2014 +0800

    nothing

commit 23d817c82188736d28493bc8c72e98fd1f63f0cd
Author: peter <peter8015@126.com>
Date:   Thu Apr 17 20:44:25 2014 +0800

    modified 1.txt

commit f64a9141504a733730fc88f6fdb9bc21331f3a16
Author: peter <peter8015@126.com>
Date:   Thu Apr 17 20:29:27 2014 +0800

    add 1.txt

commit 378a57fb9ca1a9094af950e9c816b0abc94abb8e
Author: peter <peter8015@126.com>
Date:   Thu Apr 17 19:44:54 2014 +0800

    init

Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (master)
```

5、git checkout 

git-checkout - Checkout a branch or paths to the working tree

 

```
$ git checkout v1
M       1.txt
Switched to branch 'v1'
```

 6、git branch

$ git branch -a //list all branches 

 

7、git branch v1

$ git  branch v1    //创建分支

 

8、git reset --hard    //恢复删除的文件 

 

```
Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (v1)
$ git reset --hard
HEAD is now at ee663dd v1 add

Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (v1)
$ git reset --hard
HEAD is now at ee663dd v1 add

Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (v1)
$ ls
1.txt  2.txt  css  images  index.html  js  product.html  product_date.html
```

 

9、git merge

$git merge v1

 

```
Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (v1)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

Administrator@Q3CKOWU5VSZ9N1O /E/baimu/local/html (master)
$ git merge v1
Updating ee663dd..dcd3285
Fast-forward
 1.txt | 14 +++++++++++++-
 2.txt |  1 -
 4.txt |  5 +++++
 3 files changed, 18 insertions(+), 2 deletions(-)
 delete mode 100644 2.txt
 create mode 100644 4.txt
```

 10、恢复某一个文件 

 

**恢复单个文件**:  
**git** checkout \[--] &lt;/path/to/需要被恢复的文件&gt;  
**git** reset -- &lt;/path/to/需要被恢复的文件&gt;  
**git** reset HEAD &lt;/path/to/需要被恢复的文件&gt;

例如:  
**git** checkout -- packages/apps/Settings/src/com/android/settings/wifi/AccessPoint.java  
**git** checkout -- packages/apps/Settings/src/com/android/settings/wifi/WifiDialog.java  
**git** checkout -- packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java

把整个版本库恢复到之前的某一个版本:  
**git** reset --hard 50191e117062cb628ed40b5c8ee19152ba3b52c8  
这种恢复把磁盘文件也恢复了;

其它恢复操作:  
**git** revert

列出仓库中所有修改过的文件(修改历史):  
**git** log --name-only  
**git** log --name-status  
**git** log --name-status --pretty=oneline  
**git** log --name-status -&lt;n&gt; 显示最近n次commit的文件的状态;

 

 12、git建立远程仓库并提交本地文件 

 

```
A、create fold on github.com,folder name is "boilerplate",so repository url is "git@github.com:peter8015/bm-boilerplate.git  or https://github.com/peter8015/bm-boilerplate.git"

B、commit local changes

$ touch readme.md
$ echo "sdfdsfs" >> readme.md
$ git remote add origin https://[username]:[password]@github.com/peter8015/bm-boilerplate.git
(git remote add origin https://peter8015:....@github.com/peter8015/bm-boilerplate.git)
$ git push origin master

```

 如遇到 “Error when push commits with Github: fatal: could not read Username”, reference to 

[http://stackoverflow.com/questions/20871549/error-when-push-commits-with-github-fatal-could-not-read-username](http://stackoverflow.com/questions/20871549/error-when-push-commits-with-github-fatal-could-not-read-username)

 

 13、git rm (必须先删除后再add)

```
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/com/jdk/Timer/ChannelInfo.java
        new file:   src/com/jdk/Timer/FTPEngine.java
        new file:   src/com/jdk/Timer/FTPTemplate.java
        new file:   src/com/jdk/Timer/FtpOper.java
        modified:   src/com/jdk/Timer/Task.java

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    src/com/jdk/Timer/Main.java
```

 

 

 

 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

git bash 乱码问题

[http://hi.baidu.com/xiboliya/item/db89c00ba28348c32f4c6b46](http://hi.baidu.com/xiboliya/item/db89c00ba28348c32f4c6b46)

[http://blog.csdn.net/self001/article/details/7337182](http://blog.csdn.net/self001/article/details/7337182)