git的使用(一)
来源:互联网 发布:热血江湖 2.0发招优化 编辑:程序博客网 时间:2024/06/05 14:09
作为一名合格的开发者,对代码的管理是无可厚非的,由于github上大牛们的显现,也使得git渐渐的走到了舞台上。
Git教程
本教程通过命令行来阐述分布版本控制系统Git的使用。演示系统选取的是Linux(Ubuntu),但是在其他系统上也能功能,例如Windows系统。
内容索引
- 1. Git
- 1.1. Git是何方神圣?
- 1.2. 重要的术语
- 1.3. 索引(stage)
- 2. 安装
- 3. 配置
- 3.1. 用户信息
- 3.2. 高亮显示
- 3.3. 忽略特定的文件
- 3.4. 使用.gitkeep来追踪空的文件夹
- 4. 开始操作Git
- 4.1. 创建内容
- 4.2. 创建仓库、添加文件和提交更改
- 4.3. diff命令和commit修改
- 4.4. Status, Diff和Commit Log
- 4.5. 更正提交的信息 - git amend
- 4.6. 删除文件
- 5. 远端仓库(remote repositories)
- 5.1. 设置一个远端的Git仓库
- 5.2. 推送修改到其他的仓库
- 5.3. 添加远端仓库
- 5.4. 显示已有的远端仓库
- 5.5. 克隆仓库
- 5.6. 拉取(Pull)修改
1. Git
1.1. Git是何方神圣?
Git是用C语言开发的分布版本控制系统。版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态)。另一个状态可以是不同的文件,也可以是不同的文件内容。举个例子,你可以将文件集合转换到两天之前的状态,或者你可以在生产代码和实验性质的代码之间进行切换。文件集合往往被称作是“源代码”。在一个分布版本控制系统中,每个人都有一份完整的源代码(包括源代码所有的历史记录信息),而且可以对这个本地的数据进行操作。分布版本控制系统不需要一个集中式的代码仓库。
当你对本地的源代码进行了修改,你可以标注他们跟下一个版本相关(将他们加到index中),然后提交到仓库中来(commit)。Git保存了所有的版本信息,所以你可以转换你的源代码到任何的历史版本。你可以对本地的仓库进行代码的提交,然后与其他的仓库进行同步。你可以使用Git来进行仓库的克隆(clone)操作,完整的复制一个已有的仓库。仓库的所有者可以通过push操作(推送变更到别处的仓库)或者Pull操作(从别处的仓库拉取变更)来同步变更。
Git支持分支功能(branch)。如果你想开发一个新的产品功能,你可以建立一个分支,对这个分支的进行修改,而不至于会影响到主支上的代码。
Git提供了命令行工具;这个教程会使用命令行。你也可以找到图形工具,譬如与Eclipse配套的EGit工具,但是这些都不会在这个教程中进行描述。
1.2. 重要的术语
仓库(Repository)
一个仓库包括了所有的版本信息、所有的分支和标记信息。在Git中仓库的每份拷贝都是完整的。仓库让你可以从中取得你的工作副本。分支(Branches)一个分支意味着一个独立的、拥有自己历史信息的代码线(code line)。你可以从已有的代码中生成一个新的分支,这个分支与剩余的分支完全独立。默认的分支往往是叫master。用户可以选择一个分支,选择一个分支叫做checkout.标记(Tags)
一个标记指的是某个分支某个特定时间点的状态。通过标记,可以很方便的切换到标记时的状态,例如2009年1月25号在testing分支上的代码状态提交(Commit)
提交代码后,仓库会创建一个新的版本。这个版本可以在后续被重新获得。每次提交都包括作者和提交者,作者和提交者可以是不同的人URLURl用来标识一个仓库的位置修订(Revision)
用来表示代码的一个版本状态。Git通过用SHA1 hash算法表示的id来标识不同的版本。每一个 SHA1 id都是160位长,16进制标识的字符串.。最新的版本可以通过HEAD来获取。之前的版本可以通过"HEAD~1"来获取,以此类推。1.3. 索引
Git 需要将代码的变化显示的与下一次提交进行关联。举个例子,如果你对一个文件继续了修改,然后想将这些修改提交到下一次提交中,你必须将这个文件提交到索引中,通过git add file命令。这样索引可以保存所有变化的快照。
新增的文件总是要显示的添加到索引中来。对于那些之前已经提交过的文件,可以在commit命令中使用-a 选项达到提交到索引的目的。
2. 安装
在Ubuntu上,你可以通过apt来安装git命令行工具
sudo apt-get install git-core
对于其他的Linux版本,请查看相关的软件包安装工具使用方法
msysgit项目提供了Windows版本的Git,地址是http://code.google.com/p/msysgit/
3. 配置
你可以在.gitconfig文件中防止git的全局配置。文件位于用户的home目录。 上述已经提到每次提交都会保存作者和提交者的信息,这些信息都可以保存在全局配置中。
后续将会介绍配置用户信息、高亮显示和忽略特定的文件
3.1. 用户信息
通过如下命令来配置用户名和Email
# Configure the user which will be used by git# Of course you should use your namegit config --global user.name "Example Surname"# Same for the email addressgit config --global user.email "your.email@gmail.com"# Set default so that all changes are always pushed to the repositorygit config --global push.default "matching"
获取Git配置信息,执行以下命令:
git config --list
3.2. 高亮显示
以下命令会为终端配置高亮
git config --global color.status autogit config --global color.branch auto
3.3. 忽略特定的文件
可以配置Git忽略特定的文件或者是文件夹。这些配置都放在.gitignore文件中。这个文件可以存在于不同的文件夹中,可以包含不同的文件匹配模式。为了让Git忽略bin文件夹,在主目录下放置.gitignore文件,其中内容为bin。
同时Git也提供了全局的配置,core.excludesfile。
3.4. 使用.gitkeep来追踪空的文件夹
Git会忽略空的文件夹。如果你想版本控制包括空文件夹,根据惯例会在空文件夹下放置.gitkeep文件。其实对文件名没有特定的要求。一旦一个空文件夹下有文件后,这个文件夹就会在版本控制范围内。
4. 开始操作Git
后续将通过一个典型的Git工作流来学习。在这个过程中,你会创建一些文件、创建一个本地的Git仓库、提交你的文件到这个仓库中。这之后,你会克隆一个仓库、在仓库之间通过pull和push操作来交换代码的修改。注释(以#开头)解释了命令的具体含义
让我们打开命令行开始操作吧
4.1. 创建内容
下面创建一些文件,它们会被放到版本控制之中
#Switch to homecd ~/# Create a directorymkdir ~/repo01# Switch into itcd repo01# Create a new directorymkdir datafiles# Create a few filestouch test01touch test02touch test03touch datafiles/data.txt# Put a little text into the first filels >test01
4.2. 创建仓库、添加文件和提交更改
每个Git仓库都是放置在.git文件夹下.这个目录包含了仓库的所有历史记录,.git/config文件包含了仓库的本地配置。
以下将会创建一个Git仓库,添加文件倒仓库的索引中,提交更改。
# Initialize the local Git repositorygit init# Add all (files and directories) to the Git repositorygit add .# Make a commit of your file to the local repositorygit commit -m "Initial commit"# Show the log filegit log
4.3. diff命令与commit更改
通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看git diff命令输出什么,然后提交这个更改到仓库中
# Make some changes to the fileecho "This is a change" > test01echo "and this is another change" > test02# Check the changes via the diff command git diff# Commit the changes, -a will commit changes for modified files# but will not add automatically new filesgit commit -a -m "These are new changes"
4.4. Status, Diff 和 Commit Log
下面会向你展示仓库现有的状态以及过往的提交历史
# Make some changes in the fileecho "This is a new change" > test01echo "and this is another new change" > test02# See the current status of your repository # (which files are changed / new / deleted)git status# Show the differences between the uncommitted files # and the last commit in the current branchgit diff# Add the changes to the index and commitgit add . && git commit -m "More chaanges - typo in the commit message"# Show the history of commits in the current branchgit log# This starts a nice graphical view of the changesgitk --all
4.5. 更正提交的信息 - git amend
通过git amend命令,我们可以修改最后提交的的信息
上述的提交信息中存在错误,下面会修改这个错误
git commit --amend -m "More changes - now correct"
4.6. 删除文件
如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成
# Create a file and put it under version controltouch nonsense.txtgit add . && git commit -m "a new file has been created"# Remove the filerm nonsense.txt# Try standard way of committing -> will not work git add . && git commit -m "a new file has been created"# Now commit with the -a flaggit commit -a -m "File nonsense.txt is now removed"# Alternatively you could add deleted files to the staging index viagit add -A . git commit -m "File nonsense.txt is now removed"
5. 远端仓库(remote repositories)
5.1. 设置一个远端的Git仓库
我们将创建一个远端的Git仓库。这个仓库可以存储在本地或者是网络上。
远端Git仓库和标准的Git仓库有如下差别:一个标准的Git仓库包括了源代码和历史信息记录。我们可以直接在这个基础上修改代码,因为它已经包含了一个工作副本。但是远端仓库没有包括工作副本,只包括了历史信息。可以使用--bare选项来创建一个这样的仓库。
为了方便起见,示例中的仓库创建在本地文件系统上
# Switch to the first repositorycd ~/repo01# git clone --bare . ../remote-repository.git# Check the content, it is identical to the .git directory in repo01ls ~/remote-repository.git
5.2. 推送更改到其他的仓库
做一些更改,然后将这些更改从你的第一个仓库推送到一个远端仓库
# Make some changes in the first repositorycd ~/repo01# Make some changes in the fileecho "Hello, hello. Turn your radio on" > test01echo "Bye, bye. Turn your radio off" > test02# Commit the changes, -a will commit changes for modified files# but will not add automatically new filesgit commit -a -m "Some changes"# Push the changesgit push ../remote-repository.git
5.3. 添加远端仓库
除了通过完整的URL来访问Git仓库外,还可以通过git remote add命令为仓库添加一个短名称。当你克隆了一个仓库以后,origin表示所克隆的原始仓库。即使我们从零开始,这个名称也存在。
# Add ../remote-repository.git with the name origingit remote add origin ../remote-repository.git # Again some changesecho "I added a remote repo" > test02# Commitgit commit -a -m "This is a test for the new remote origin"# If you do not label a repository it will push to origingit push origin
5.4. 显示已有的远端仓库
通过以下命令查看已经存在的远端仓库
# Show the existing defined remote repositoriesgit remote
5.5. 克隆仓库
通过以下命令在新的目录下创建一个新的仓库
# Switch to homecd ~# Make new directorymkdir repo02# Switch to new directorycd ~/repo02# Clonegit clone ../remote-repository.git .
5.6. 拉取(Pull)更改
通过拉取,可以从其他的仓库中获取最新的更改。在第二个仓库中,做一些更改,然后将更改推送到远端的仓库中。然后第一个仓库拉取这些更改
# Switch to homecd ~# Switch to second directorycd ~/repo02# Make changesecho "A change" > test01# Commitgit commit -a -m "A change"# Push changes to remote repository# Origin is automatically maintained as we cloned from this repositorygit push origin# Switch to the first repository and pull in the changescd ~/repo01git pull ../remote-repository.git/# Check the changesless test01
- Git(一)、Git的安装、使用
- git的使用(一)
- Git的使用(一)
- git的使用(一)
- git使用(一)
- Git 使用(一)
- Git使用(一)
- git使用(一)
- git使用(一)
- Git使用(一)
- Git使用(一)
- git 命令的使用(一)
- Git的简单使用(一)
- git 命令的使用(一)
- git的使用知识点总结(一)
- Git学习笔记(一) Git的安装与使用
- Git学习笔记(一) Git的安装与使用
- Ubuntu git使用(一)git服务器的配置
- spring mvc 数据层mysql 数据库连接数处理 配置处理
- 超级芯片电路板问世 比现有电脑快9000倍
- HDU 2767 Proving Equivalences 强连通
- POJ 1051: P,MTHBGWB
- 广西南宁商会商务运作合法吗?能不能做吗?政府不管吗?
- git的使用(一)
- 黑马程序员_C#中字符串的一些操作
- HNOI2014D1T2 worldtree 题解
- weka 中加载model文件
- java操作excel系列
- 中国为何会陷入结构性通缩的漩涡?
- 缩小页面比例,想让内容居中的。
- PowerShell读取不同数据格式并生成自己数据
- SDK解码器中的SPS和PPS