Git(2):基本操作

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

时间: 2024-08-10 19:19:08

Git(2):基本操作的相关文章

# git常用基本操作

git常用基本操作 初始化git仓库git init 从远程仓库克隆git clone <远程仓库地址> git到暂存区git add . git提交git commit -m "<提交信息>" git提交到同步到远程仓库git push <远程仓库/仓库名> git查看提交历史git log ,在一行查看git log --pretty=oneline git查看所有分支git branch -l git创建分支git branch <新分支

Git 版本管理基本操作

Git是一个版本管理操作的工具 非常N,可以很智能的分布式管理, 安装 yum -y install git 本地设置全局 告知是谁提交代码 信息 # git config --global user.name "xxx" # git config --global user.email "xxx"颜色设置# git config --global color.ui true 初始化仓库 # mkdir oldboy # cd oldboy/ # git init

git的基本操作

克隆远程服务器项目到本地:git clone [email protected]:patgate/web.git 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b <name> 分支提交到服务器:git push origin dev 合并某分支到当前分支:git merge <name> 删除分支:git branch -d <

从VSS到SVN再到Git 记Git的基本操作

Source code control 一直是软件开发过程中重要的环节,从最初的纯文件备份,到使用工具进行管理.Source code control 工具的作用也不仅仅只是单纯的对同一个版本进行管理了.从目前主流的source code control工具当中不难发现里面的Branch, tag等功能的应用场景越来越多,特别是现在多数企业使用的敏捷编程,结合branch和tag等功能真的能够很好的做到多版本开发,快速迭代. 思考: 没有source code control我们如何快速的基于一

git flow 基本操作

开发人员常用的场景 完成常规项目中的功能 git flow feature start 888 使用日常git命令提交修改,直至功能完成 git flow feature finish 888 这个动作会把 feature/888分支合并到dev分支,如果功能完成,可以push到远程dev进行测试 完成单独一个功能并测试 步骤和常规开发相同,但是可以push到远程git库 git flow feature publish 888 测试并修复bug git flow feature finish

git客户端基本操作

首先下载git 一路next安装好了之后,打开任意盘符,右键打开git bash here 首先:初始首次的用户名和邮箱,之后就不用了. git config --global user.name "name" git config --global user.email "[email protected]" 创建本地仓库 文件夹-->右键git bash here-->git init //现在这个文件夹就是你的本地仓库了 打开代码编辑器,建立几个文

总结一下git的基本操作

总结一下自己在提交项目代码中常用的一些基本的git命令: 1.git init : 先在本地建一个文件夹,点开文件夹在里面打开cmd,然后输入这个命令: 2.git clone : 把远程代码克隆到本地,后面跟的是远程仓库的地址: 3.git branch lin : 创建lin分支: 4.git checkout lin : 切换到lin分支: 5.git add . : 提交代码到暂存区(注意add后面的“ .”): 6.git commit -m"提交" : 将暂存区的改动提交到

git学习(1)---基本操作

一.目的 本文将介绍git的基本操作,包括基本配置.新建和修改文件.删除和重命名文件.提交和恢复操作.比较文件差异.查看日志等内容. 本文涉及到的所有操作都是在Ubuntu14.04环境中进行的,git版本是1.9.1. 二.git安装和配置 在shell下使用sudo apt-get install git命令安装git软件:安装完成后,使用git init命令初始化本地仓库或者使用git clone克隆远程仓库. git在使用前需要进行简单的配置,经常使用的命令是: 配置全局用户名:git

《Pro Git》笔记3:分支基本操作

<Pro Git>笔记3:Git分支基本操作 分支使多线开发和合并非常容易.Git的分支就是一个指向提交对象的可变指针,极其轻量.Git的默认分支为master. 1.Git数据存储结构和分支 git提交时会将暂存文件的内容,暂存的目录结构,提交对象,含附注标签对象都以包含信息头的二进制文件形式存储到版本库中(.git/objects目录),存储的对象以其自身SHA1值作为唯一标识,SHA1前两位为存储对象所在目录名,SHA1后38位为存储对象的文件名.存储的数据对象类型有: blob(文件内

git(版本控制系统)的使用

git的简介 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 集中式版本控制系统(svn): 中间是中央服务器,其他所有电脑都需要从中checkout代码下来.只有中央服务器管理着各个版本的代码.其他电脑需要操作时都要和中央服务器交互.如果中央服务器离线或者故障,其他电脑就没法与中央服务器交互. 分布式版本控制系统(git): 分布式每一台电脑都有版本的Database,就不用担心中央服务器离线或者故障.因为本地就有版本的Database,所以可以实现离线操作