1, git init 初始化
E:MyProject>git init
Initialized empty Git repository in E:/MyProject/.git/
2,git add 添加文件到暂存区
E:MyProject>git add README.md
3,git commit -m "备注" 提交到仓库
E:MyProject>git commit -m "add a readme file"
[master (root-commit) 5601acf] add a readme file
1 file changed, 1 insertion(+)
create mode 100644 README.md
4,git status 查看仓库状态
E:MyProject>git status
On branch master
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
5,git reset HEAD~ 回撤
E:MyProject>git reset HEAD~
Unstaged changes after reset:
M LICENSE.txt
reset 命令的选项
git rest -- mixed HEAD~ (默认)
---移动HEAD的指向,将其指向上一个快照
---将HEAD移动后指向的快照回滚到暂存区域
git rest -- soft HEAD~
---移动HEAD的指向,将其指向上一个快照(相当于撤回上一个提交)
git rest --hard HEAD~
---移动HEAD的指向,将其指向上一个快照
---将HEAD移动后指向的快照回滚到暂存区域
---将暂存区域的文件还原到工作目录
git reset 版本快照的ID号
---回滚到指定ID号
E:MyProject>git log --------->提交日志
commit f4747938e0ec1634519af4491e68a0e72135c3be (HEAD -> master)
Author: chne <chenhailong199@gmail.com>
Date: Wed Jun 21 17:04:12 2017 +0800
add LICENSE.txt file
commit 5601acfa5e9f5abe4039cb8a8ff7a0a4c358484d
Author: chne <chenhailong199@gmail.com>
Date: Wed Jun 21 16:49:51 2017 +0800
add a readme file
E:MyProject>git reset 5601ac
6,git diff 比较暂存区域和工作目录
E:MyProject est>git diff
diff --git a/java.java b/java.java
index e69de29..e483af9 100644
--- a/java.java ------------>暂存区域
+++ b/java.java ------------->工作区
@@ -0,0 +1,6 @@ ------------->"-" 表示旧文件 "+"表示新文件 "1,6" 表示第一行开始,有6行
+class java
+{
+ public static void main(){
+
+ }
+}
No newline at end of file ------------->文件不是以换行符结尾
7,git diff 快照ID1 快照ID2 ---比较两个历史快照
E:MyProject est> git diff a259a 0fab929
diff --git a/java.java b/java.java
index 7efd50d..931b771 100644
--- a/java.java
+++ b/java.java
@@ -2,5 +2,6 @@ class java
{
public static void main(String arrs[]){
System.out.print("Hello Word");
+ System.out.print("Hello Word");
}
}
No newline at end of file
8,git diff HEAD 比较当前工作目录和Git仓库中的快照
E:MyProject est>git diff HEAD
........
9,git diff -- cached [快照ID] 比较暂存区域和Git仓库快照
E:MyProject est>git diff --cached
.........
10,git commit --amend 修改最后一次提交
在实际开发中,可能会遇到以下情况:
情景一:版本刚一提交(commit)到仓库,突然想起漏掉两个文件还没有添加(add)
情景二:版本刚一提交(commit)到仓库,突然想起版本说明写得不够全面,需要添加
*执行带 --amend 选项的 commit 提交命令, Git 就会 "更正" 最近的一次提交
:q ---->冒号 q 退出
:q! --->不保存退出
11,git checkout -- 文件名 恢复删除的文件
E:MyProject est>git status
On branch master
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: java.java
no changes added to commit (use "git add" and/or "git commit -a")
E:MyProject est>git checkout -- java.java ------>恢复删除的 java 文件
12,git rm 文件名 删除文件
①该命令删除的只是工作目录和暂存区域的文件,也就是取消跟踪,在下次提交时不纳入版本管理
E:MyProject est>git rm java.java
rm 'java.java'
②当工作目录和暂存区域的同一个文件存在不同内容时,执行 git rm -f 文件名 命令就可以把两个都删除
E:MyProject est>git rm test.txt
error: the following file has staged content different from both the
file and the HEAD:
test.txt
(use -f to force removal)
使用 git rm -f 强制删除 ,工作区和暂存区的文件
E:MyProject est>git rm -f test.txt
rm 'test.txt'
③如果只删除暂存区域的文件,而保留工作目录的,可以执行 git rm --cached 文件名 命令
E:MyProject est>git rm --cached test.txt
13,git mv 旧文件名 新文件名 重命名文件
相当于:
- ren / mv 旧文件名 新文件名
- git rm 旧文件名
- git add 新文件名
14,git branch 分支名 创建分支
E:MyProject est>git branch feature
查看
E:MyProject est>git log --decorate
commit a259a75c725756393022edb044604154d6bc0f30 (HEAD -> master, feature) ---> master 默认分支
15,git checkout 分支名 切换分支
E:MyProject est>git checkout feature
Switched to branch 'feature'
D java.java
M readme.md
查看
E:MyProject est>git log --decorate --oneline ----->oneline 一行显示一个快照
a259a75 (HEAD -> feature, master) changed java.java readme.md at first
85572b6 add readme and java
查看所有分支提交日志
E:MyProject est>git log --decorate --oneline --graph --all
* f03e7b2 (HEAD -> feature) add test.txt at feature
* ee36fec changed readme at next
* a259a75 (master) changed java.java readme.md at first
* 85572b6 add readme and java
16, git merge 分支名 合并分支
E:MyProject est>git merge feature2
Updating c01d871..c1634a9
Fast-forward
feature2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 feature2.txt
17,git branch --delete 分支名 删除分支
简写: git branch -d 分支名
E:MyProject est>git branch --delete feature2
Deleted branch feature2 (was c1634a9).
18,git checkout HEAD~ 匿名分支
E:MyProject est2>git log --decorate --all --oneline --graph
* fa9ecca (HEAD -> master) add 2.txt third
* 5edd797 add 1.txt second
* 2f6c50e add a,b,c txt first
E:MyProject est2>git checkout HEAD~
Note: checking out 'HEAD~'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-nam HEAD is now at 5edd797... add 1.txt second