git学习

创建ssh key

$ 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"

查看log

使用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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值