Git 基本用法

Posted lnlin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git 基本用法相关的知识,希望对你有一定的参考价值。

创建一个仓库

git init  将当前目录变为git管理的仓库,在成功执行此命令之后,当前目录会出现 .git 文件
[[email protected] git]$ pwd 
/home/wangml/git
[[email protected] git]$ ls
[[email protected] git]$ git init
Initialized empty Git repository in /home/wangml/git/.git/
[[email protected] git]$ ls -a
.  ..  .git

 

git只能跟踪文本文件的变化情况,而对于二进制文件却无能为力,文本文件都是有编码的,建议
仓库中文件使用同一种编码格式,一般推荐使用UTF-8
 
将一个文件添加到仓库中,或者提交一个修改(将工作区的文件加入stage暂存区)
git add filename
git commit 将文件提交到仓库,而 -m 后的参数表示对于此次提交的说明(将stage暂存区文件加
           入master分支)
git commit -m "Explain"
[[email protected] git]$ vim first.txt
[[email protected] git]$ ls
first.txt
[[email protected] git]$ git add first.txt 
[[email protected] git]$ git commit -m "the first file"
[master (root-commit) 8a16ecf] the first file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 first.txt

 

git status  查看仓库当前状态
[[email protected] git]$ git status
# On branch master
# Changed but not updated:
#   (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")

 

git diff  查看仓库与上次提交后有什么不同(工作区文件与master中的区别)
[[email protected] git]$ git diff
diff --git a/readme.txt b/readme.txt
index e69de29..aede7aa 100644
--- a/readme.txt
+++ b/readme.txt
@@ -0,0 +1,2 @@
+dont stop dont stop
+laughing about it

 

git log  查看仓库日志(显示从最近到最远的提交日志)
[[email protected] git]$ git log
commit 47c7f76657196394d9d776668cb2206c1927e1c9
Author: wangml <[email protected](none)>
Date:   Fri Aug 3 16:07:39 2018 +0800
    20180803-16:07
commit bfa08dc84f3f6f0b235f5a99c5ad4993bdfeb1f3
Author: wangml <[email protected](none)>
Date:   Wed Aug 1 21:49:30 2018 +0800
    add readme.txt
commit 8a16ecff65e153fb435f4c0fa5060fbc3bac0bc2
Author: wangml <[email protected](none)>
Date:   Wed Aug 1 21:46:55 2018 +0800
    the first file

 

git log --pretty=oneline  将每次提交日志用一行显示
[[email protected] git]$ git log --pretty=oneline
47c7f76657196394d9d776668cb2206c1927e1c9 20180803-16:07
bfa08dc84f3f6f0b235f5a99c5ad4993bdfeb1f3 add readme.txt
8a16ecff65e153fb435f4c0fa5060fbc3bac0bc2 the first file

 

上述显示的第一个字段的一长串是用SHA1计算出来的用16进制表示的 commit id(版本号)

在 git 中可以用 HEAD^ 表示上一个版本,HEAD^^ 表示上上的版本
或者使用 HEAD~N 表示最近提交的倒数第N个版本

git reset --hard HEAD~N  回退到倒数第N个版本
[[email protected] git]$ git reset --hard HEAD~
HEAD is now at bfa08dc add readme.txt
[[email protected] git]$ cat readme.txt 
[[email protected] git]$ 

 

退回到上一个版本,可以看到在上个版本中的readme.txt文件中没有任何内容
但如果此时我们希望不要回退,让其变为readme.txt文件已被添加了内容的版本,我们应该
怎么办呢?此时我们无法使用 HEAD~N ,因为这个是指向当前版本之前的版本,这时我们就
需要使用上述的 commit id
 
git reset --hard id  将仓库设置为 id 表示的版本
[[email protected] git]$ git reset --hard 47c7f76
HEAD is now at 47c7f76 20180803-16:07
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it

 

git reflog  记录了我们使用的们一条命令,也可以通过它找到不同版本的 commit id
[[email protected] git]$ git reflog
47c7f76 [email protected]{0}: 47c7f76657196394d9d776668cb2206c1927e1c9: updating HEAD
bfa08dc [email protected]{1}: HEAD~: updating HEAD
47c7f76 [email protected]{2}: commit: 20180803-16:07
bfa08dc [email protected]{3}: commit: add readme.txt
8a16ecf [email protected]{4}: commit (initial): the first file

 

注意:Git跟踪并管理的是修改,而非文件

git checkout -- filename  使filename文件回到最近一次git add/commit时的样子
这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样
的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区
后的状态。
一:
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it
test for diff
[[email protected] git]$ vim readme.txt 
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it
test for diff
checkout...
[[email protected] git]$ git checkout -- readme.txt 
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it
test for diff

 

二:
[[email protected] git]$ cat first.txt 
test for diff
[[email protected] git]$ vim first.txt 
[[email protected] git]$ cat first.txt 
test for diff
checkout...
[[email protected] git]$ git add first.txt 
[[email protected] git]$ vim first.txt 
[[email protected] git]$ cat first.txt 
test for diff
checkout...
checkout2...
[[email protected] git]$ git checkout -- first.txt 
[[email protected] git]$ cat first.txt 
test for diff
checkout...

 

git reset HEAD filename  可以把暂存区的修改撤销掉(unstage),重新放回工作区
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it
test for diff
checkout...
[[email protected] git]$ vim readme.txt 
[[email protected] git]$ cat readme.txt 
dont stop dont stop
laughing about it
test for diff
checkout...
reset...
[[email protected] git]$ git add readme.txt 
[[email protected] git]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   first.txt
# modified:   readme.txt
#
[[email protected] git]$ git reset HEAD readme.txt 
Unstaged changes after reset:
M readme.txt
[[email protected] git]$ git status
# On branch master
# modified:   readme.txt

 

rm filename      删除一个文件,被删除的文件可以使用git checkout -- filename 恢复
[[email protected] git]$ rm test.txt 
[[email protected] git]$ ls
first.txt  readme.txt
[[email protected] git]$ git checkout -- test.txt
[[email protected] git]$ ls
first.txt  readme.txt  test.txt

 

git rm filename + git commit 从版本库中删除一个文件,可以使用 git reset 恢复
[[email protected] git]$ ls
first.txt  readme.txt  test.txt
[[email protected] git]$ git add test.txt 
[[email protected] git]$ git commit -m "add test.txt"
[master 7406bd5] add test.txt
 1 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[[email protected] git]$ git rm test.txt 
rm test.txt
[[email protected] git]$ ls
first.txt  readme.txt
[[email protected] git]$ git commit -m "delete test.txt"
[master e6a2486] delete test.txt
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt
[[email protected] git]$ ls
first.txt  readme.txt
[[email protected] git]$ git log
commit e6a2486a711c71e7703804123008afe1e3aef2a1
Author: wangml <[email protected](none)>
Date:   Sat Aug 4 16:42:04 2018 +0800
    delete test.txt
...
[[email protected] git]$ git reset --hard 7406bd59979f1207f6dad4538
HEAD is now at 7406bd5 add test.txt
[[email protected] git]$ ls
first.txt  readme.txt  test.txt

 

远程库相关:

git push ...  将本地仓库的commit推送到远程库中
 
git clone ...  克隆一个远程库到本地

git branch 分支名  创建一个新的分支
git checkout 分支名  选择指定的分支
上述两个命令等价于: git checkout -b 分支名
[[email protected]6helaylvgqok97prZ git]$ git branch var
[[email protected] git]$ git checkout var
Switched to branch var

 

git branch  查看当前分支
[[email protected] git]$ git branch
  master
* var

 

在新创建的分支中做的改变对源分支没有影响,如下示例:在 var 分支中 对test.txt文件
加以修改并提交到版本库,在回到master分支,查看test.txt文件,却发现test.txt文件没
有任何改变
[[email protected] git]$ vim test.txt 
[[email protected] git]$ cat test.txt 
branch...  var
[[email protected] git]$ git add test.txt 
[[email protected] git]$ git commit -m "branch var..."
[var d991370] branch var...
 1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] git]$ git checkout master
Switched to branch master
[[email protected] git]$ cat test.txt 
[[email protected] git]$ 

 

git merge 分支名  合并指定分支到当前所在的分支,如下示例:将 var 分支合并到 当前
分支 master 中,可以看到test.txt文件内容变得和之前在 var 分支中修改后的内容一样
git log --graph  查看分支合并图
[[email protected] git]$ cat test.txt 
[[email protected] git]$ git branch
* master
  var
[[email protected] git]$ cat test.txt 
[[email protected] git]$ git merge var
Updating 7406bd5..d991370
Fast-forward
 test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] git]$ cat test.txt 
branch...  var

 

git branch -d 分支名  删除指定分支
[[email protected] git]$ git branch
* master
  var
[[email protected] git]$ git branch -d var
Deleted branch var (was d991370).
[[email protected] git]$ git branch
* master

 

注意:当两个分支都做出了更改,在对分支进行合并时可能会发生冲突,此时需要将发生冲突的文
件作出修改之后在提交

git stash  将未提交的工作空间储存起来,待以后恢复后在使用,在使用 git stash 后可
以看到working directory clean
[[email protected] git]$ git status
# On branch var
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   new.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] git]$ git stash
Saved working directory and index state WIP on var: 6fa8864 new file
HEAD is now at 6fa8864 new file
[[email protected] git]$ git status
# On branch var
nothing to commit (working directory clean)

 

git stash list 列出已储存的工作空间
[[email protected] git]$ git stash list
[email protected]{0}: WIP on var: 6fa8864 new file

 

git stash apply  恢复到上一个储存的工作空间
git stash drop   删除已储存的工作空间
上述两个命令一起用等于 git stash pop
 
[[email protected] git]$ git stash apply
# On branch var
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   new.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] git]$ git stash drop
Dropped refs/[email protected]{0} (fc7113d17f35a62b46cd7142c20ad1efa5f1d6b4)

 



























以上是关于Git 基本用法的主要内容,如果未能解决你的问题,请参考以下文章

Git基本用法1

git 的基本用法和常用命令

git的基本用法

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程