git简单操作

git 版本控制系统

git是一个版本控制系统

一、什么是版本控制系统

1、概念

版本控制是一种 记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统

(*)记录文件的所有历史变化
(*)随时可恢复到任何一个历史状态
(*)多人协作开发或修改
(*)错误恢复
(*)多功能并行开发

产品–> 新加功能A —> 单独拉一个新分支 –> 开发完成后合并到master或者丢弃

2、分类

本地版本控制系统
集中化版本控制系统 SVN
分布式版本控制系统 Git

3、基本概念

repository :存放所有文件及其历史信息
checkout :取出或切换到执行版本的文件
version :表示一个版本
tag :记录标识一个主要版本。2.0 3.0。用来标识一个特定的version

4、不同版本控制系统优缺点

本地
优点:
简单,很多系统中内置。适合保存文本文件(配置文件、文章、信件)

缺点:
只支持管理少量的文件,不支持基于项目的管理
支持的文件类型单一
不支持网络,无法实现多人协作

集中式版本控制系统
优点:
适合多人团队协作开发
代码集中化管理

缺点:
单点故障
必须联网工作,无法单机工作

解决:
分布式版本控制系统
集合集中式版本控制系统优点
支持离线工作,先提交到本地仓库,再在某个时间上传到远程仓库
每个计算机都是一个完整仓库:强备份

二、git分布式版本管理系统

由Linux创始人开发,作为Linux内核代码管理系统使用

Git在设计时考虑了很多方面设计目标

特点:
速度
简单的设计
对非线性开发模式的强力支持(允许上千个并行开发的分支)
完全分布式
有能力管理超大规模项目(挑战:速度和数据量)

Git原理:保存快照而非保存区别
Git保存时,相当于保存了当下所有文件的一个整体快照
所以,每个版本都是独立的。随时想取某一个版本,可以很快取出来

三、安装git

Git 的工作区域:

Git repository:: 最终确定的文件保存到仓库,作为一个新的版本
staging area: 暂存已经修改的文件
woking directory: 工作目录

安装git
https://git-scm.com/ 下载windows版本git
全使用默认值,一直下一步

四、创建仓库和基本操作

git安装好后,需要一些基本设置
设置用户名:git config –global user.name “hsiehchou”
设置邮箱:git config –global user.email “417952939@qq.com”

查看所有设置:git config –list

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git init
Reinitialized existing Git repository in C:/Users/hsiehchou/Desktop/git demo/.git/

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ ll -a
total 52
drwxr-xr-x 1 hsiehchou 197121 0 4月 24 12:45 ./
drwxr-xr-x 1 hsiehchou 197121 0 4月 24 10:44 ../
drwxr-xr-x 1 hsiehchou 197121 0 4月 24 12:46 .git/

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use “git add” to track)
+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ touch README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ vi README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git status
On branch master

No commits yet

未追踪的文件
Untracked files:
(use “git add <file>…” to include in what will be committed)
README

nothing added to commit but untracked files present (use “git add” to track)

+++++++++++++++++++++++++++++++++++++++++++++++++++++
新建文件,默认是未追踪的文件
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git add README
warning: LF will be replaced by CRLF in README.
The file will have its original line endings in your working directory.

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git status
On branch master

No commits yet

Changes to be committed:
(use “git rm –cached <file>…” to unstage)
new file: README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
git add 提交到了暂存区域
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git commit -m “add README”
[master (root-commit) f50e736] add README
1 file changed, 1 insertion(+)
create mode 100644 README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
查看历史
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git log
commit f50e7362b5573e9b9862f7fb65a9e3f6fa98913a (HEAD -> master)
Author: hsiehchou <417952939@qq.com>
Date: Wed Apr 24 12:52:51 2019 +0800
add README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
修改文件
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ vim README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
检测到被修改了
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ 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: README

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++
提交到仓库
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git commit -a -m “modify README”
warning: LF will be replaced by CRLF in README.
The file will have its original line endings in your working directory
[master 278ec6a] modify README
1 file changed, 1 insertion(+)

+++++++++++++++++++++++++++++++++++++++++++++++++++++
删除文件
rm README
git rm README
git commit -m “delete README”

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ rm README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git rm README
rm ‘README’

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git commit -m “delete README”
[master b82ec4f] delete README
1 file changed, 2 deletions(-)
delete mode 100644 README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
checkout 某个版本
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git log
commit b82ec4f7ccf718abac6dc630b7049618d179418f (HEAD -> master)
Author: hsiehchou <417952939@qq.com>
Date: Wed Apr 24 12:56:45 2019 +0800

delete README

commit 278ec6a869f73af71539785f6893259726f9902e
Author: hsiehchou <417952939@qq.com>
Date: Wed Apr 24 12:56:00 2019 +0800

modify README

commit f50e7362b5573e9b9862f7fb65a9e3f6fa98913a
Author: hsiehchou <417952939@qq.com>
Date: Wed Apr 24 12:52:51 2019 +0800

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo ((b82ec4f…))
$ git checkout 278ec6a869f73af71539785f6893259726f9902e
Previous HEAD position was b82ec4f delete README
HEAD is now at 278ec6a modify README

You are in ‘detached HEAD’ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 278ec6a modify README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo ((278ec6a…))
$ ll
total 1
-rw-r–r– 1 hsiehchou 197121 22 4月 24 12:58 README

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo ((278ec6a…))
$ cat README
Hello World!
fsdggd

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo ((278ec6a…))
$ git checkout master
Previous HEAD position was 278ec6a modify README
Switched to branch ‘master’

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ ll
total 0

五、git远程仓库

实现代码共享
远程仓库实际保存了本地.git文件夹下的东西,内容几乎一样
+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ touch README2

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ vim README2

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git add README2
warning: LF will be replaced by CRLF in README2.
The file will have its original line endings in your working directory

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git commit -a -m “README2”
[master 0c4f333] README2
1 file changed, 1 insertion(+)
create mode 100644 README2

git 远程仓库访问协议
ssh协议
git协议
http https协议:一般用于开源项目

常用远程仓库实现:
1、github
2、自己搭建git仓库服务器 gitlab、码云

举例:在自己的github中,关联本地仓库
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ cd ~/.ssh
+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/.ssh
$ ll
total 9
-rw-r–r– 1 hsiehchou 197121 1823 4月 15 23:35 id_rsa
-rw-r–r– 1 hsiehchou 197121 398 4月 15 23:35 id_rsa.pub
-rw-r–r– 1 hsiehchou 197121 1197 4月 16 14:09 known_hosts

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/.ssh
$ cat id_rsa.pub

创建公钥
ssh-keygen -t rsa -C “417952939@qq.com”命令

测试是否能够正常连接github
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ ssh -T git@github.com
Hi hsiehchou! You’ve successfully authenticated, but GitHub does not provide shell access.

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git remote add origin git@github.com:hsiehchou/git-test.git

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ ll
total 1
-rw-r–r– 1 root 197121 15 4月 21 22:13 README2

+++++++++++++++++++++++++++++++++++++++++++++++++++++
hsiehchou@DESKTOP-KJDN870 MINGW64 ~/Desktop/git demo (master)
$ git push -u origin master
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (11/11), 826 bytes | 103.00 KiB/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To github.com:hsiehchou/git-test.git

  • [new branch] master -> master
    Branch ‘master’ set up to track remote branch ‘master’ from ‘origin’.

原文地址:https://www.cnblogs.com/hsiehchou/p/10767862.html