$ ssh-keygen -t rsa -C "youremail@example.com"
$ git push origin master
新建文件和修改文件都是在git仓库目录下进行的,如果是新建就将新建的文件放到git仓库目录,修改则直接在文件上进行修改,例如:
我们编写一个readme.txt文件,内容如下:
Git is a version control system.
Git is free software.
让后放到git目录下,然后使用git add指令添加,修改也是这个指令
$ git add readme.txt
然后用commit提交修改
$ git commit -m "wrote a readme file"
这时我们对readme文件进行修改
Git is a distributed version control system.
Git is free software.
然后可以用git status查看状态
$ 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.txt
no changes added to commit (use "git add" and/or "git commit -a")
上面的输出告诉我们,readme被修改了,但是还没提交
我们可以用git diff查看修改了什么
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 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.
然后我们重复之前的操作就可以提交修改了
$ git add readme.txt
$ git commit -m "add distributed"
这时我们再进行修改,将readme改为
Git is a distributed version control system.
Git is free software distributed under the GPL.
然后提交
$ git add readme.txt
$ git commit -m "append GPL"
使用git log命令可以查看历史修改信息
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
使用–pretty=oneline减少输出信息
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
前面的长串是版本号
HEAD表示最新版本,HEAD^ 表示上一个版本,HEAD^^ 表示上上个版本,往上很多版本比如100可以写为HEAD~100
使用git reset 可以实现版本回退
$ git reset --hard HEAD^
这时我们再看log就会变化了
$ git log
commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
如果我们想反悔的话,可以使用git reflog查看之前的操作,找到想要返回的版本号就行
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
$ git reset --hard 1094a
使用 git checkout
$ git checkout -- readme.txt
$ git reset HEAD readme.txt
$ git rm test.txt
$ git commit -m "remove test.txt"