Git:基本操作

Posted Storm_L

tags:

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

Git 创建仓库

执行<git init>命令后,Git仓库会生成一个.git目录,该目录包含了资源的所有元数据,其他的项目目录保持不变(Git 只在仓库的根目录生成 .git 目录)。

使用当前目录作为Git仓库

$git init

使用指定目录作为Git仓库

$git init <directory>

使用<git clone>从Git仓库中拷贝项目。

克隆仓库

$git clone <remote repository>

克隆到指定的目录

$git clone <remote repository> <local directory>
技术分享
admin MINGW64 /
$ cd c

admin MINGW64 /c
$ mkdir GitRepositoryTest

admin MINGW64 /c
$ cd GitRepositoryTest/

admin MINGW64 /c/GitRepositoryTest
$ git init test1
Initialized empty Git repository in C:/GitRepositoryTest/test1/.git/

admin MINGW64 /c/GitRepositoryTest
$ ls
test1/

admin MINGW64 /c/GitRepositoryTest
$ git git clone [email protected]:lyz170/DesignPattern.git DesignPattern
Cloning into ‘DesignPattern‘...
remote: Counting objects: 7132, done.
remote: Compressing objects: 100% (558/558), done.
remote: Total 7132 (delta 6541), reused 7065 (delta 6498)
Receiving objects: 100% (7132/7132), 1.84 MiB | 0 bytes/s, done.
Resolving deltas: 100% (6541/6541), done.
Checking connectivity... done.
Checking out files: 100% (2902/2902), done.

admin /c/GitRepositoryTest
$ ls
DesignPattern/  test1/
<例>

 

Git add/status/diff/commit/reset/remove/move

<git add>命令可将该文件添加到缓存([.]整个目录add)

$git add <file name> <file name> [.]

<git status>以查看在你上次提交之后是否有修改及文件状态 (-s:简短输出)

[A]表示已缓存

[AM]表示文件在将它添加到缓存之后又有改动,需要再使用git add后才会变成[A]

$git status [-s]
技术分享
admin MINGW64 /c/GitRepositoryTest
$ ls
DesignPattern/  test1/

admin MINGW64 /c/GitRepositoryTest
$ cd test1/

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ cp -p file1 file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ ls
file1  file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
?? file1
?? file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git add file1 file2
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
A  file1
A  file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
AM file1
A  file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git add file1
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
A  file1
A  file2
<例>

<git diff>命令显示已写入缓存与已修改但尚未写入缓存的改动的区别(default:当前目录下所有尚未缓存的改动  [--cached]:查看已缓存的改动  [--stat]:显示摘要而非整个diff  [<file name>]查看具体某个文件而非整个目录)

$git diff [--cached] [--stat] [<file name>]
技术分享
admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
AM file1
A  file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git diff
diff --git a/file1 b/file1
index 1ebef96..d01e930 100644
--- a/file1
+++ b/file1
@@ -1,2 +1,3 @@
 ‘file1‘ was created at 2017-8-31 16:52:00
 ‘file1‘ was updated at 2017-8-31 16:57:00
+‘file1‘ was updated at 2017-8-31 17:37:00
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git diff --stat
 file1 | 1 +
 1 file changed, 1 insertion(+)
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git diff --cached file2
diff --git a/file2 b/file2
new file mode 100644
index 0000000..baa67ac
--- /dev/null
+++ b/file2
@@ -0,0 +1 @@
+‘file2‘ was created at 2017-8-31 16:53:00
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.
<例>

<git commit>将缓存区内容添加到仓库中(default:强制写入提交comment  [-m ‘XXX‘]:可以在命令行中写comment  [-a]:跳过<git add>提交缓存的流程直接提交)

$git commit [-m] [-a]
技术分享
admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git add file2
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git commit -m ‘commited by lyz‘
[master warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.
1166cb5] commited by lyz
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status
On branch master
nothing to commit, working directory clean

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git commit -am ‘commited by lyz‘
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
[master warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
944283e] commited by lyz
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status
On branch master
nothing to commit, working directory clean
<例>

<git reset HEAD 命令用于取消已缓存的内容(default:取消目录下已缓存的所有文件  [ -- <file name>] 取消其中一个的缓存)

$git reset HEAD [ -- <file name>]
技术分享
admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ vi file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
 M file1
 M file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git reset HEAD
Unstaged changes after reset:
M       file1
M       file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
 M file1
 M file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git add .
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git reset HEAD -- file1
warning: LF will be replaced by CRLF in file1.
The file will have its original line endings in your working directory.
Unstaged changes after reset:
M       file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.
 M file1
M  file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git reset HEAD
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.
Unstaged changes after reset:
M       file1
M       file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
 M file1
 M file2
<例>

<git rm <file name>>会将条目从缓存区中移除。这与<git reset HEAD>将条目取消缓存是有区别的。后者是将缓存区恢复为我们做出修改之前的样子。前者会将文件从缓存区和你的硬盘中(工作目录)删除。([--cached]在工作目录中留着该文件)

$git rm [--cached] <file name>
技术分享
admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git rm file1
rm ‘file1‘

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ ls
file2

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git rm --cached file2
rm ‘file2‘

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
D  file1
D  file2
?? file2
<例>

<git mv <old file name> <new file name>>命令用于移动或重命名一个文件、目录、软连接

$git mv <old file name> <new file name>
技术分享
admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git add file2
warning: LF will be replaced by CRLF in file2.
The file will have its original line endings in your working directory.

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
D  file1

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git mv file2 file2new

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ ls
file2new

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status -s
D  file1
R  file2 -> file2new

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git commit
[master e6c8a62] commit
warning: LF will be replaced by CRLF in file2new.
The file will have its original line endings in your working directory.
 2 files changed, 5 deletions(-)
 delete mode 100644 file1
 rename file2 => file2new (100%)

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ git status
On branch master
nothing to commit, working directory clean

admin MINGW64 /c/GitRepositoryTest/test1 (master)
$ ls
file2new
View Code

 


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

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

markdown 一堆Git的基本片段

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

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

markdown Git代码片段

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