github命令及版本控制

$ git init                    #把当前目录变成git可以管理的仓库
$ git add readme.txt          #添加一个文件,也可以添加文件夹
$ git add -A                  #添加全部文件
$ git commit -m "some commit" #提交修改
$ git status                  #查看是否还有未提交
$ git log                     #查看最近日志
$ git reset --hard HEAD^      #版本回退一个版本
$ git reset --hard HEAD^^     #版本回退两个版本
$ git reset --hard HEAD~100   #版本回退多个版本
$ git remote add origin +地址 #远程仓库的提交(第一次链接)
$ git push -u origin master   #仓库关联
$ git push                    #远程仓库的提交(第二次及之后)

1. 安装:

sudo apt install git
git
git init

创建一个git库

2. 版本创建和版本回退

touch code.txt
vi code.txt
# 添加完内容后
git add code.txt
git commit -m '版本1'

git log

这样就完成文件的创建以及版本的提交,这个时候如果没登陆, 需要输入:

git config --global user.email "ccc179@gmail.com"

告诉git你是谁

wqk@ubuntu:~/git_test$ vi code.txt 
wqk@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
wqk@ubuntu:~/git_test$ git add code.txt 
wqk@ubuntu:~/git_test$ git commit -m '版本2'
[master 7a36f84] 版本2
 1 file changed, 1 insertion(+)
wqk@ubuntu:~/git_test$ git log
commit 7a36f84faae675dbe3c3aabdcfd40044d9ccf660
Author: wqk <ccc179@gmail.com>
Date:   Mon Apr 8 17:36:46 2019 +0800

    版本2

commit 497c42204e33b7a64222eccc9b41ece7b57a0783
Author: wqk <ccc179@gmail.com>
Date:   Mon Apr 8 17:35:08 2019 +0800

    版本1

此时,经过第二次commit, 再次进行git log查看的时候可以查看到第二个版本号了

在我们拥有2个版本时如何进行版本回退呢?

此时我们需要知道,git中有一个指针叫做HEAD 指向当前的最新版本.

HEAD^   -->上一个版本

HEAD^^  -->上2个版本

HEAD~1  -->上一个版本

HEAD~100 -->上100个版本

wqk@ubuntu:~/git_test$ git reset --hard HEAD^
HEAD is now at 497c422 版本1
wqk@ubuntu:~/git_test$ git reset --hard 7a36f
HEAD is now at 7a36f84 版本2

  用 :

git reset --hard HEAD^  或者HEAD 号  可以回退或进入到某个版本
git reflog
# 用来查看历史操作的版本号

3. git的工作区和版本库

  电脑中的目录,比如init的文件夹就是工作区

  工作区有个隐藏的.git 这个是版本库, 里面有一个非常重要的区域: stage(或者叫index)的暂存区, 还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫做HEAD

  第一步:git add 是将修改的内容放到暂存区里面

  第二步:git commit 将暂存区中的修改创建版本记录

vi code.tx
vi code.txt
# 这里新建一个code.tx文件,并给code.txt加第三行,然后保存

  这里可以用git status 命令查看当前工作树的状态

    (是否有修改,以及是否有不能commit的内容, 哪些内容已经add了等待一次性commit)

  git管理文件的修改,它只会提交暂存区的修改来创建版本.

4. 丢弃对工作区的改动:

git checkout -- code.txt

  此时再git status 会提示你工作区是干净的

如果此时已经add提交了怎么改变呢?

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

	modified:   code.txt
git reset HEAD code.txt
git checkout -- code.txt

  可以先用  撤销暂存

git reset HEAD 文件名   撤销掉暂存区的修改,重新放回工作区.
然后用
git checkout -- code.txt
丢弃修改即可

5.  查找不同

git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index 01e1274..f0eea5e 100644
--- a/code.txt
+++ b/code.txt
@@ -1,3 +1,4 @@
 this is the first line
 this is the second line
+add a new line
 this is the third line

减号代表前面的版本,  加号代表后面的版本. 加号离现在最近

6. 删除文件

rm code.txt
git status
# 此时会提示我已经删除了文件
# 并且会提示我 可以使用git cheackout -- code.txt 撤回操作
# 删除的改动可以用git add/rm code.txt 进行提交到暂存区

 因为日志太多了,想简短的显示: 

git log --pretty=oneline

  

91b76994921927f9fad3021831a16ea3a6e5cbdf 删除了code.tx
331aa9ed730e1747a40bdb5162763bf68269f9e8 版本3
7a36f84faae675dbe3c3aabdcfd40044d9ccf660 版本2
497c42204e33b7a64222eccc9b41ece7b57a0783 版本1

  

原文地址:https://www.cnblogs.com/djflask/p/10672229.html