Git使用指南(加精)

来源:互联网 发布:乒乓球 知乎 编辑:程序博客网 时间:2024/05/24 05:16

一. what is git?

Git是目前世界上最先进的分布式版本控制系统

二. SVN与Git的区别

   SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。   Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需 要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

三. 如何操作Git?

1. 创建版本库

     什么是版本库?版本库又名仓库,英文名repository,你可以简单的理解一个目录, 这个目录里面的所有文件都可以被Git管理起来,每个文件的修改,删除,Git都能跟踪, 以便任何时刻都可以追踪历史,或者在将来某个时刻还可以将文件”还原”。所以创建一个 版本库也非常简单,      如下我是 E盘/GitTest 目录下新建一个版本库。命令解释:
          1.pwd 命令是用于显示当前的目录。          2.git init 把当前目录变成git可以管理的仓库

创建版本库

   这时候你当前testgit目录下会多了一个.git的目录,这个目录是Git   来跟踪管理版本。

.git

2. 把文件添加进版本库

   首先要明确下,所有的版本控制系统,只能跟踪文本文件的改动,比如txt文件,网页,所有程序的代码等,Git也不列外,版本控制系统可以告诉你每次的改动,但是图片,视频这些二进制文件,虽能也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是知道图片从1kb变成2kb,但是到底改了啥,版本控制也不知道。
Demo演示:
在版本库GitTest里新建一个文件,内容为hello word

一. 使用 git add 文件名 添加到暂存区里面,如下图;
git add

如果没有任何提示,和上图一样,说明添加版本库成功

二. 使用git commit告诉git,把文件提交到仓库
git commit -m “第一次提交实验”
这里写图片描述
现在我们已经提交一个文件了,接下来用
git status 查看是否还有文件为提交
这里写图片描述
说明没有任何文件未提交,但是我现在继续来改下test.c内容,比如我在下面添加一行int a=0;内容,继续使用git status来查看下结果,如下:

这里写图片描述

使用 git diff 文件名 来查看哪里修改了,如下图:
这里写图片描述
知道了对文件修改了哪些地方后,就放心提交到仓库了,提交修改和提交文件是一样的步骤(1.git add 2.git commit)
这里写图片描述

3.版本回退

如上,我们已经学会了修改文件,现在我继续对test.c文件进行修改,再增加一行  int b=0;继续执行命令如下:

这里写图片描述
现在我已经对这个文件修改了三次,接下来我要查看历史版本,命令如下: git log

这里写图片描述

现在我要想版本回退,使用命令如下:

git reset  –hard HEAD^         //上一个版本git reset  –hard HEAD^^      //上上一个版本git reset  –hard HEAD^^^      //上上上一个版本git reset  –hard HEAD~100   //前一百个版本

Git指令大全

“`
usage: git [–version] [–help] [-C ] [-c name=value]
[–exec-path[=]] [–html-path] [–man-path] [–info-path]
[-p | –paginate | –no-pager] [–no-replace-objects] [–bare]
[–git-dir=] [–work-tree=] [–namespace=]
[]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status

grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects

‘git help -a’ and ‘git help -g’ list available subcommands and some
concept guides. See ‘git help ’ or ‘git help ’
to read about a specific subcommand or concept.

四.添加远程库(Github)

`

0 0
原创粉丝点击