Git学习教程

Git是一个分布式的文件版本控制系统,每一个电脑都有一个完整的文件库和版本库,文件库之间能够互相推送和抓取版本信息。CVS和SVN是集中式的文件版本控制系统,文件库和版本信息集中存放在服务器上,每一个电脑只跟服务器交互信息。html

 

1. Git的安装git

操做系统:Ubuntu 12.04LTSgithub

Git的安装命令: sudo apt-get install git编程

 

2. Git的配置vim

2.1. 设置Git的配置服务器

    --local option: read and write from .git/config 编程语言

    配置信息的做用域为当前库,配置信息存放在.git/config。编辑器

    --global option: read and write from ~/.gitconfig. 分布式

    配置信息的做用域为当前用户,配置信息存放在~/.gitconfig。工具

    --system option: read and write from /etc/gitconfig, that contains value for every user.

    配置信息的做用域为整个系统的全部用户,配置信息存放在/etc/gitconfig。

2.1.1 设置用户名

    git config --global user.name "user name" 

2.1.2 设置用户的邮箱

    git config --global user.email "user email"

2.1.3 设置文本编辑器(editor)

    git config --global core.editor vim

2.1.4 设置文本比较工具和合并工具(Diff Tool, Merge Tool)

    git config --global diff.tool vimdiff

    git config --global merge.tool vimdiff

2.1.5 彩色显示git命令的输出

    git config --global color.ui true

 

2.2. 设置Git命令的简写,相似于C语言里面的宏命令   

2.2.1 设置经常使用命令的简写

    $ git config --global alias.st status

    $ git config --global alias.co checkout

    $ git config --global alias.cm commit

    $ git config --global alias.br branch

    $ git config --global alias.unstage 'reset HEAD'

2.2.1 设置复杂命令的简写,例如建立一个复杂的显示日志信息的命令lg

    $ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)  <  %an  >  %Creset' --abbrev-commit"

 

2.3. 设置不进行版本控制的文件类型列表

    若是不想对有些类型的文件进行版本控制,例如编译过程当中产生的临时文件或中间文件等,能够建立Git的忽略文件列表.gitignore,将不须要进行版本控制的文件类型加入到列表中。Git将自动地忽略工做区中的这些类型的文件,不进行跟踪和版本管理。github的网站上有各类类型编程语言的忽略类型列表的模版,网址为https://github.com/github/gitignore。

    个人.gitignore内容以下:

# Python:

*.py[cod] # *.pyc, *.pyo, *.pyd

 

# Tex:

*.aux

*.lof

*.log

*.lot

*.fls

*.out

*.toc

*.dvi # Tex Intermediate documents:

*.bbl # Tex Bibliography auxiliary files (bibtex/biblatex/biber):

*.bcf

*.blg

 

#Vim

*.swp

 

3. Git的本地库操做(Local Repository)

 

3.1. 建立本地文件仓库: git init

    git init把当前文件夹建立为一个本地文件仓库Repository,其中包括两部分:工做目录(Working Directory)和版本库(.git).

    工做目录就是当前的文件夹,包括其中的全部文件,但不包括.git文件夹,也不包括.gitignore中列出的文件;

    版本库存储在.git文件夹中,.git是一个隐藏文件夹,其中记录了工做目录中文件的各个版本信息。

 

3.2. 把文件的修改提交到本地库:git add ; git commit

    提交文件修改到本地库有两个步骤:

3.2.1 git add file_name, 提交文件的修改到暂存区staging area or index)。

    暂存区中能够存放多个文件的修改信息和一个文件的屡次修改信息。

3.2.2 git commit -m "comments",将暂存区的修改生成一个新的版本commit),并清空暂存区。

     -a option will let you skip the git add part. -a可使工做区的修改直接提交成新的版本,再也不通过暂存区。

 

3.3 查看本地库的文件状态。

    git status(to check the status. '-s' option will give the short output.)能够查看工做区中文件的状态, 其输出内容包括4个部分:     

    1. 当前工做目录所在的分支,例如“on branch master”表示当前工做目录在正在名为master的分支。

    2. 已经提交到暂存区(staging area)的修改(Changes to be committed)

    3. 未提交到暂存区的修改(Changes not staged for commit)

    2. 在工做目录中,可是未进行版本控制的文件(Untracked files)

 

Git学习教程(逐渐更新中)
 

图1. 文件在Git版本控制过程当中的状态周期

fig1. File Status Lifecycle

 

3.4 查看文件的修改信息

    git diff能够查看一个文件的内容修改,包括如下3种修改信息:

3.4.1 工做区文件相对于版本库的修改内容

    git diff HEAD [filename] // Changes in the working tree since your last commit.

3.4.2 工做区文件相对于暂存区的修改内容。

    git diff [filename] // Changes in the working tree not yet staged for the next commit.

3.4.3 暂存区相对于版本库的修改内容

    git diff --cached [filename] // Changes between the index and your last commit

3.4.4 版本库的不一样版本之间的修改内容

    git diff commit_id1 commit_id2 [filename] // compare between two different commits

 

    **git difftool可使用指定的文本比较软件进行文件比较,例如vimdiff。difftool的设置在2.1.4小节,使用git config --global diff.tool vimdiff。

    **能够设置difftool的简写为d,例如git config --global alias.d difftool。这样,使用git difftool ...时能够直接使用 git d ...

 

3.5 撤销文件内容的修改

3.5.1 撤销工做区的修改(to discard changes in the working directory)

    git checkout --filename 将工做区的文件恢复到最近一次提交到暂存区中的版本或者提交到版本库中的版本。

3.5.2 撤销暂存区的修改

    git reset HEAD filename 能够把暂存区的修改撤销掉(unstage)

3.5.3 撤销版本库的修改(版本库的回退)

    git reset --hard HEAD^ //回退到前一个版本。

    git reset --hard commit_id // 回退到以前指定的一个版本。

    git log // 查看版本的提交记录,以选择回退到以前哪一个版本。

    git reflog // 查看命令历史,能够查询有哪一个版本回退到当前的版本。

 

3.6 删除文件

3.5.1 从版本库和硬盘上同时删除文件 git rm filename 

    to remove files from the staging area entirely and also off the disk.

3.5.2 只从版本库中删除文件,工做区中还保留文件 git rm --cached filename

    --cached option leaves the file in the working dictionary but not have Git track it anymore.

3.5.3 强制删除文件 git rm -f filename

    -f option: if you modified the file and added it to the index already, you must force the removal with the -f option.

    若是文件的修改已经被提交到暂存区,就须要使用强制删除文件。

3.5.4 删除文件夹 git rm -r dir

    -r option: remove recursively

3.5.5 恢复被删除的文件 git checkout -- filename

     与撤销工做区的修改相似,使用git checkout 命令能够从版本库中恢复被删除的文件。

 

4. Git的分支(Branches)

    我我的认为Git的分支和暂存区是理解Git工做过程的两个重点。版本库中的每一个版本能够按照其提交时间排列到一个时间轴上。分支能够理解成一个项目被分为不一样的部分同时开发,每个并行开发的部分就是一个分支。开发过程当中,分支能够合并,也能够产生新的子分支。Git的分支是指向时间轴上版本的指针,而HEAD是一个指向分支的指针。被HEAD指向的分支是当前的工做分支,每一个本地库只有一个当前工做分支。

 

4.1 查看分支:git branch

4.1.1 查看本地分支 git branch

    分支列表中,有*标记的为当前工做分支。

4.1.2 查看远程分支 git branch -r 

    to list remote-tracking branches

 

4.2 建立分支:git branch name

    一般,master分支是项目的主分支,文件的修改不会在主分支上进行,而是建立一个dev分支为当前工做分支。dev分支的版本完成后,才把dev分支合并到master分支。

    建立的新分支指向的版本是当前工做分支指向的版本。

 

4.3 切换分支:git checkout name

    将当前的工做分支切换到指定的分支上。git checkout 命令还能够用于恢复工做区文件的修改,恢复工做区被删除的文件。

    建立并切换分支:git checkout -b name

 

4.4 合并某分支到当前分支:git merge name

    若是合并时出现冲突,须要手工解决不一样分支的差别,而后再从新提交。

 

4.5 删除分支:git branch -d name

 

4.6 查看分支信息:git branch

    to list local-working branches. git branch只列出本地的工做分支

    git branch -a  能够列出本地和远程的所有分支

    to list all branches including local-working and remote-tracking branches.

 

5. Git的远程库操做(Remote Repository)

 

5.1 克隆一个远程库到本地

    git clone remote_url [local_dir] 复制远程的Git代码库到本地文件夹。

    get a local copy of a remote Git repository so you can look at it and start modifying it.

    clone命令能够把一个远程的Git代码库复制到本地,包括history, 这样就能够查看里面的内容或者进行修改。若是指定了本地目录local_dir,就将Git库中的内容存入local_dir。若是没有指定本地目录,就创建与Git库名称相同的本地目录,而后将内容存进目录中。

 

5.2 将本地库与远程库创建关联

    git remote add [short name] [url] 将远程库与本地库创建关联。

    to add a new remote Git repository as a short name you can reference easily.

 

5.3 向远程库推送信息

    git push [remote-name] [branch-name] 将当前的工做分支(branch)推送到远程库。

    to push your work (branch name) back up to the server (remote name)

 

5.4 从远程库抓取信息

5.4.1 git fetch [remote-name] 获取远程库的更新,不与本地的文件合并。

    to get data from the specified remote Git repository.

    Note that fetch command doesn't automatically merge files with any of your work or modify what you're currently working on. You have to merge it manually into your work. 

    仅下载指定的远程库的更新,远程库的更新与本地文件的合并须要手工完成,不会直接覆盖本地文件。

5.4.2 git pull [remote-name] 抓取远程库的更新,并尝试与本地文件进行合并

   to automatically fetch and then merge a remote branch into your current branch.??

 

5.5 查看远程库的信息

    git remote 列出远程库的名称

    to list all short names of each remote handle you've specified.

    "origin" is the default name Git gives to the server you cloned from.

    克隆的远程代码仓库的默认名称为origin

    git remote -v shows the short names of remote handles and their URL.

    git remote -v 命令不只列出代码仓库的名称,并且列出其对应的URL

    git remote show remote-name

    to list the URL for the remote repository as well as the tracking branch information.

 

5.6 修改远程库的名称 

    git remote rename [remote-name] [remote-new-name]

    to change a remote repository's short name. Note that this changes your remote branch names too.

 

5.7 删除与远程库的关联

    git remote rm [remote-name]

    to remove the reference to a remote repository. 

 

5.8 显示远程库的版本信息

    git ls-remote origin

    show the references in a remote repository and their hash

 

6. Git的标签(Tags)

 

 

Force Git to overwrite local files on pull

强制覆盖本地文件

git reset --hard HEAD

git clean -f -d

git pull

或者使用fetch命令

git fetch --all 

git reset --hard origin/master

Explanation:

 

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master, so if you have any local changes, they will be lost. With or without --hard, any local commits that haven't been pushed will be lost.

相关文章
相关标签/搜索