创建Git仓库

 

目录:

1.git init命令

2.git clone

3.git status

4.git add

5.git diff

6.git commit

7.git reset HEAD

8.git rm

9.git mv


1.git init命令

创建一个目录,进入目录后执行git  init命令可以初始化一个Git仓库,执行完成命令后会产生一个.git目录。当然你也可以通过指定目录名称的方式初始化一个Git仓库,只需要在git  init命令后面接目录名称即可。

第一步:创建一个空的文件夹
random@random:~$ mkdir repo
random@random:~$ cd repo
第二步:在文件夹中执行git init初始化一个Git仓库
random@random:~/repo$ git init
Initialized empty Git repository in /home/random/repo/.git/
第三步:查看当前目录,已经自动生成了一个.git目录
random@random:~/repo$ ll
total 12
drwxr-xr-x  3 random random 4096 5月  12 20:53 ./
drwxr-xr-x 26 random random 4096 5月  12 20:53 ../
drwxr-xr-x  7 random random 4096 5月  12 20:53 .git/

你也可以指定一个目录并初始化一个Git仓库
第一步:创建一个目录
random@random:~$ mkdir repo1
random@random:~$ pwd
/home/random
第二步:执行git init dirname(这里为你的目录名称)
random@random:~$ git init repo1
Initialized empty Git repository in /home/random/repo1/.git/
第三步:查看Git仓库目录,生成了一个.git目录
random@random:~$ ll ./repo1/
total 12
drwxr-xr-x  3 random random 4096 5月  12 20:57 ./
drwxr-xr-x 27 random random 4096 5月  12 20:56 ../
drwxr-xr-x  7 random random 4096 5月  12 20:57 .git/

2.git clone

git clone命令可以将GitHub上面的项目拷贝到本地,你可以指定拷贝到本地的文件夹名称。

第一步:将github上面的项目下载到本地
random@random:~$ git clone http://github.com/randomWZ/git_test.git
Cloning into 'git_test'...
warning: redirecting to https://github.com/randomWZ/git_test.git/
remote: Enumerating objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.
第二步:查看当前目录,目录中存在了一个git_test的目录
random@random:~$ ll
drwxr-xr-x 26 random random  4096 5月  12 21:06 ./
drwxr-xr-x  3 random random  4096 5月  12 21:06 git_test/

当然你也可以指定生成的文件夹的名称
第一步:执行git clone url dirname(这里指你要指定的名称)
random@random:~$ git clone http://github.com/randomWZ/git_test.git myrepo
Cloning into 'myrepo'...
warning: redirecting to https://github.com/randomWZ/git_test.git/
remote: Enumerating objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.
第二步:查看当前目录,目录中存在了一个myrepo的目录
random@random:~$ ll
drwxr-xr-x 26 random random  4096 5月  12 21:06 ./
drwxr-xr-x  3 random random  4096 5月  12 21:10 myrepo/

3.git status

git status命令可以查看当前git仓库中文件的状态,通常于-s参数联合使用输出比较简洁,文件总共有以下几种状态:

  1. ?? 两个问好表示你并没有将文件提交到暂存区,为一个未知的状态。
  2. A 表示该文件已经使用git add提交到缓存区了。
  3. AM 表示文件已经保存在缓存区,但是被更改了,需要再次执行git add提交更改到缓存区。
  4. M 表示已经提交到版本库的文件被更改了。
  5. D表示文件被删除。

4.git add

git add命令可以将文件加入缓存,add后面可以通过使用星号匹配所有文件,也可以使用*.c匹配所有以.*结尾的文件。

第一步:创建一个文件
random@random:~/repo$ touch README
第二步:查看仓库文件状态,可以看到两个?
random@random:~/repo$ git status -s
?? README
第三步:将文件提交到缓存区
random@random:~/repo$ git add README 
第四步:查看仓库文件状态,变成A
random@random:~/repo$ git status -s
A  README
第五步:更改文件内容
random@random:~/repo$ echo "hello" >> README 
第六步:查看仓库文件状态,变成AM
random@random:~/repo$ git status -s
AM README
第七步:将文件提交到缓存区
random@random:~/repo$ git add README 
第八步:查看文件状态为A
random@random:~/repo$ git status -s
A  README
第九步:将文件提交到版本库,-m 指定说明
random@random:~/repo$ git commit -m "create README"
[master (root-commit) 36abe79] create README
 1 file changed, 1 insertion(+)
 create mode 100644 README
第十步:更文件内容,并查看git仓库文件状态,变为M
random@random:~/repo$ echo "hello world" >> README 
random@random:~/repo$ git status -s
 M README

5.git diff

git diff用来显示git status的详细结果,可以查看文件内容的更改。

git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。

  • 尚未缓存的改动:git diff
  • 查看已缓存的改动: git diff --cached
  • 查看已缓存的与未缓存的所有改动:git diff HEAD
  • 显示摘要而非整个 diff:git diff --stat
第一步:创建一个新的文件,文件内容为hello
random@random:~/repo$ echo "hello" > file 
random@random:~/repo$ cat file 
hello
第二步:将文件提交到缓存区
random@random:~/repo$ git add .
第三步将文件提交到版本库
random@random:~/repo$ git commit -m "create file"
[master 9bafc4f] create file
 1 file changed, 1 insertion(+)
 create mode 100644 file
第四步:更改文件内容
random@random:~/repo$ echo "git diff" >> file
random@random:~/repo$ cat file 
hello
git diff
第四步:查看文件修改的内容与缓存区的改动
random@random:~/repo$ git diff
diff --git a/file b/file
index ce01362..b1d9f39 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
 hello
+git diff
第五步:查看摘要
random@random:~/repo$ git diff --stat
 file | 1 +
 1 file changed, 1 insertion(+)
第六步:将更改的文件提交到缓存区
random@random:~/repo$ git add file
第七步:更改文件内容
random@random:~/repo$ echo "git diff1" >> file
random@random:~/repo$ cat file 
hello
git diff
git diff1
第八步:查看版本库和缓存区相比的改动
random@random:~/repo$ git diff --cached
diff --git a/file b/file
index ce01362..b1d9f39 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
 hello
+git diff
第九步:查看工作区文件与缓存区的修改
random@random:~/repo$ git diff
diff --git a/file b/file
index b1d9f39..82b6454 100644
--- a/file
+++ b/file
@@ -1,2 +1,3 @@
 hello
 git diff
+git diff1
第十步:查看版本库与工作区文件的修改
random@random:~/repo$ git diff HEAD
diff --git a/file b/file
index ce01362..82b6454 100644
--- a/file
+++ b/file
@@ -1 +1,3 @@
 hello
+git diff
+git diff1

6.git commit

git commit命令用于将缓存区的内容提交到版本库中,需要使用-m指定你本次提交的说明,方便查看。

如果你觉得 git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式如下:

注意:已追踪的文件才可以使用下面的命令跳过git add步骤,也就是说之前至少执行过一次git add命令。

git commit -am "info"

7.git reset HEAD

git reset HEAD 命令用于取消已缓存的内容。

第一步:更改文件内容
random@random:~/repo$ echo 'git commit' >> file
第二步:将文件提交到缓存区
random@random:~/repo$ git add file
random@random:~/repo$ git status -s
M  file
第三步:清除file文件的缓存,可以同时重置多个文件,文件名称以空格隔开。
random@random:~/repo$ git reset HEAD file
Unstaged changes after reset:
M	file
random@random:~/repo$ git status -s
 M file
第四步:执行commit发现不能commit
random@random:~/repo$ git commit -m "test4"
On branch master
Changes not staged for commit:
	modified:   file

no changes added to commit
第五步:执行commit -am提交更改,成功。
random@random:~/repo$ git commit -am "test4"
[master 91c9e45] test4
 1 file changed, 1 insertion(+)

8.git rm

如果只是简单地从工作目录中手工删除文件,运行 git status 时就会在 Changes not staged for commit 的提示。

要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交,这时就需要用到git rm命令。这个命令有三种应用场景,下面分别举例介绍。

第一种:工作目录中需要删除的文件已经提交到版本库,则直接使用git rm进行删除,删除后工作目录中的文件也会被删除。

第一步:将文件提交到版本库
random@random:~/repo$ git add file
random@random:~/repo$ git commit -m "haha"
[master 0f310f6] haha
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file
第二步:删除文件
random@random:~/repo$ git rm file
rm 'file'
第三步:查看文件已经被删除
random@random:~/repo$ ls
README
第四步:查看文件状态为deleted
random@random:~/repo$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    file

如果直接手动删除文件:

第一步:将文件提交到版本库
random@random:~/repo$ git add haha
random@random:~/repo$ git commit  -m "haha"
[master 0a45b7b] haha
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 haha
第二步:查看git仓库状态
random@random:~/repo$ git status
On branch master
nothing to commit, working tree clean
第三步:手动删除haha文件
random@random:~/repo$ rm haha
第四步:查看状态,显示需要git add/rm haha,需要重新commit,使用git rm却不需要重新commit
random@random:~/repo$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    haha

no changes added to commit (use "git add" and/or "git commit -a")

第二种:删除经过修改后提交到暂存区的文件,这时只需要使用git rm -f file即可。

第一步:创建test1文件并提交到版本库
random@random:~/repo$ touch test1
random@random:~/repo$ git add test1
random@random:~/repo$ git commit -m "test1"
[master 66f07dc] test1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test1
第二步:更改文件内容并提交到缓存区
random@random:~/repo$ echo "test1" > test1
random@random:~/repo$ git add test1
random@random:~/repo$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test1
第四步:使用git rm删除文件,无法删除,提示可以使用-f或者--cached进行删除
random@random:~/repo$ git rm test1
error: the following file has changes staged in the index:
    test1
(use --cached to keep the file, or -f to force removal)
第四步:使用git  rm -f删除test1文件,删除成功,同时工作区的文件也被删除了。
random@random:~/repo$ git rm -f test1
rm 'test1'
random@random:~/repo$ ls
README
第五步:更改test1文件内容并将文件提交到暂存区
random@random:~/repo$ echo "test2" > test1
random@random:~/repo$ git add test1
random@random:~/repo$ git status -s
M  test1
第六步:使用git rm --cached删除文件,删除成功,这样只是将暂存区的文件删除了,工作区文件依然存在。
random@random:~/repo$ git rm --cached test1
rm 'test1'
random@random:~/repo$ ls
README  test1

第三种:递归删除,与删除目录的方式一样,加-r参数即可

第一步:创建目录
random@random:~/repo$ mkdir test
random@random:~/repo$ cd test/
random@random:~/repo/test$ touch file{1..2}
random@random:~/repo/test$ ls
file1  file2
random@random:~/repo/test$ mkdir test1
random@random:~/repo/test$ cd test1
random@random:~/repo/test/test1$ touch file{1..10}
random@random:~/repo/test/test1$ ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
random@random:~/repo/test/test1$ cd ../../
random@random:~/repo$ ls
README  test
第二步:将文件提交到版本库
random@random:~/repo$ git add test/
random@random:~/repo$ git status -s
R  test1 -> test/file1
A  test/file2
A  test/test1/file1
A  test/test1/file10
A  test/test1/file2
A  test/test1/file3
A  test/test1/file4
A  test/test1/file5
A  test/test1/file6
A  test/test1/file7
A  test/test1/file8
A  test/test1/file9
random@random:~/repo$ git commit -m "test"
[master 9d147c8] test
 12 files changed, 0 insertions(+), 0 deletions(-)
 rename test1 => test/file1 (100%)
 create mode 100644 test/file2
 create mode 100644 test/test1/file1
 create mode 100644 test/test1/file10
 create mode 100644 test/test1/file2
 create mode 100644 test/test1/file3
 create mode 100644 test/test1/file4
 create mode 100644 test/test1/file5
 create mode 100644 test/test1/file6
 create mode 100644 test/test1/file7
 create mode 100644 test/test1/file8
 create mode 100644 test/test1/file9
第三步:使用-r参数递归删除test目录
random@random:~/repo$ git rm -r test
rm 'test/file1'
rm 'test/file2'
rm 'test/test1/file1'
rm 'test/test1/file10'
rm 'test/test1/file2'
rm 'test/test1/file3'
rm 'test/test1/file4'
rm 'test/test1/file5'
rm 'test/test1/file6'
rm 'test/test1/file7'
rm 'test/test1/file8'
rm 'test/test1/file9'
第四步:查看目录test已经被删除
random@random:~/repo$ ls
README

 

9.git mv

git mv 命令用于移动或重命名一个文件、目录、软连接。

random@random:~/repo$ git mv README.md README
random@random:~/repo$ git status -s
R  README.md -> README

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值