Git 基本操作

Posted 黑乌鸦

tags:

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

1.创建仓库

  ——创建工作目录(Working Directory):git三种副本:工作目录(Working Direcotry),暂存区域(Stage,索引(Index)),仓库(History)

技术分享
#/home/yang/Documents/repo01
. ├── datafiles │   └── data.txt ├── test01 ├── test02 └── test03
技术分享
# Initialize the local Git repository(在根目录下生成.git文件夹)
git init

2.查看文件状态

技术分享
[email protected] ~/Documents/repo01 $ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    datafiles/
#    test01
#    test02
#    test03
nothing added to commit but untracked files present (use "git add" to track)
技术分享

 

3.添加文件

  ——git add files 把当前文件放入暂存区域

技术分享
[email protected] ~/Documents/repo01 $ git add .
[email protected] ~/Documents/repo01 $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
#    new file: datafiles/data.txt
#    new file: test01
#    new file: test02
#    new file: test03
#
技术分享

3.提交文件

  ——git commit 给暂存区域生成快照并提交。

技术分享
[email protected] ~/Documents/repo01 $ git commit -m "Initial commit"
[master (root-commit) 3f79459] Initial commit
 4 files changed, 4 insertions(+)
 create mode 100644 datafiles/data.txt
 create mode 100644 test01
 create mode 100644 test02
 create mode 100644 test03
[email protected] ~/Documents/repo01 $ git status
# On branch master
nothing to commit, working directory clean
技术分享

4.查看文件内容变更

技术分享
[email protected] ~/Documents/repo01 $ echo "This is a change" > test01
[email protected] ~/Documents/repo01 $ echo "and this is another change" > test02
[email protected] ~/Documents/repo01 $ git diff
diff --git a/test01 b/test01
index 749eb2d..d0a432b 100644
--- a/test01
+++ b/test01
@@ -1,4 +1 @@
-datafiles
-test01
-test02
-test03
+This is a change
diff --git a/test02 b/test02
index e69de29..552c22e 100644
--- a/test02
+++ b/test02
@@ -0,0 +1 @@
+and this is another change
[email protected] ~/Documents/repo01 $ git commit -a -m "These are new changes"
技术分享

5.查看工作目录提交记录

(回顾:git status--查看文件状态,git-diff--查看文件内容状态)

技术分享
# Make some changes in the file
echo "This is a new change" > test01
echo "and this is another new change" > test02
技术分享
技术分享
[email protected] ~/Documents/repo01 $ 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:   test01
#    modified:   test02
#
no changes added to commit (use "git add" and/or "git commit -a")
[email protected] ~/Documents/repo01 $ git diff
diff --git a/test01 b/test01
index d0a432b..18fec42 100644
--- a/test01
+++ b/test01
@@ -1 +1 @@
-This is a change
+This is a new change
diff --git a/test02 b/test02
index 552c22e..0b17386 100644
--- a/test02
+++ b/test02
@@ -1 +1 @@
-and this is another change
+and this is another new change
[email protected] ~/Documents/repo01 $ git add . && git commit -m "More changes - type in the commit message"
[master 8a18ab1] More changes - type in the commit message
 2 files changed, 2 insertions(+), 2 deletions(-)
[email protected] ~/Documents/repo01 $ git log
commit 8a18ab1c77ecaf049d17e5ac8fb682ae618cd710
Author: Will Hunting <[email protected]>
Date:   Sun Aug 18 18:57:38 2013 +0800

    More changes - type in the commit message

commit a6bd74cdbaa1b349d537008f33fa186eae9d48c9
Author: Will Hunting <[email protected]>
Date:   Sun Aug 18 18:56:07 2013 +0800

    These are new changes

commit 3f794593e7008286a893a5d00f81ee5757140469
Author: Will Hunting <[email protected]>
Date:   Sun Aug 18 18:50:58 2013 +0800

    Initial commit
技术分享

6.删除文件

如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成

技术分享
# Create a file and put it under version control
touch nonsense.txt
git add . && git commit -m "a new file has been created"
# Remove the file
rm nonsense.txt
# Try standard way of committing -> will not work 
git add . && git commit -m "a new file has been created"
# Now commit with the -a flag
git commit -a -m "File nonsense.txt is now removed"
# Alternatively you could add deleted files to the staging index via
git add -A . 
git commit -m "File nonsense.txt is now removed"
技术分享

7.更正提交的信息

git commit --amend -m "More changes - now correct"

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

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

markdown 一堆Git的基本片段

如何管理在每个 git 版本中添加私有代码片段?

使用 Git 来管理 Xcode 中的代码片段

markdown Git代码片段

GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段