Git使用详解 - Go语言中文社区

Git使用详解


说明:本文转自 技术非宅男的博客

原文出处:http://www.vogella.com/articles/Git/article.html

翻译说明:个人出于兴趣爱好翻译这篇Git教程(google 搜索git tutorial排名第一的文章)。学习git最初的原因是为了了解GitHub,译者水平有限,有不足之处欢迎指正。希望这边译文对你能有帮助。转载请链接出处。

 

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)修改
6. 还原更改
7. 标记
8. 分支和合并
8.1. 分支
8.2. 合并
8.3. 删除分支
8.4. 推送(Push)一个分支到远端仓库
9. 解决合并冲突
10. 变基(Rebase) 
10.1. 在同一分支中应用Rebase Commit
10.2. Rebasing 多个分支
10.3. Rebase最佳实践
11. 创建和应用补丁
12. 定义同名命令
13. 放弃跟踪文件
14. 其他有用的命令
15. 安装Git服务
16. 在线的远端仓库
16.1. 克隆远端仓库
16.2. 添加远端仓库
16.3. 通过http和代理服务器进行远端操作
17. Git服务提供商
17.1. GitHub
17.2. Bitbucket
18. Git的图形接口
19. Kindle版本教程
20. 问题与讨论
21. 链接和文章

Git是用C语言开发的分布版本控制系统。版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态)。另一个状态可以是不同的文件,也可以是不同的文件内容。举个例子,你可以将文件集合转换到两天之前的状态,或者你可以在生产代码和实验性质的代码之间进行切换。文件集合往往被称作是“源代码”。在一个分布版本控制系统中,每个人都有一份完整的源代码(包括源代码所有的历史记录信息),而且可以对这个本地的数据进行操作。分布版本控制系统不需要一个集中式的代码仓库。

当你对本地的源代码进行了修改,你可以标注他们跟下一个版本相关(将他们加到index中),然后提交到仓库中来(commit)。Git保存了所有的版本信息,所以你可以转换你的源代码到任何的历史版本。你可以对本地的仓库进行代码的提交,然后与其他的仓库进行同步。你可以使用Git来进行仓库的克隆(clone)操作,完整的复制一个已有的仓库。仓库的所有者可以通过push操作(推送变更到别处的仓库)或者Pull操作(从别处的仓库拉取变更)来同步变更。

Git支持分支功能(branch)。如果你想开发一个新的产品功能,你可以建立一个分支,对这个分支的进行修改,而不至于会影响到主支上的代码。

Git提供了命令行工具;这个教程会使用命令行。你也可以找到图形工具,譬如与Eclipse配套的EGit工具,但是这些都不会在这个教程中进行描述。

你可以在.gitconfig文件中防止git的全局配置。文件位于用户的home目录。 上述已经提到每次提交都会保存作者和提交者的信息,这些信息都可以保存在全局配置中。

后续将会介绍配置用户信息、高亮显示和忽略特定的文件

通过如下命令来配置用户名和Email 

复制代码
# Configure the user which will be used by git
# Of course you should use your name
git config --global user.name "Example Surname"
# Same for the email address
git config --global user.email "your.email@gmail.com"
# Set default so that all changes are always pushed to the repository
git config --global push.default "matching"
复制代码

 

获取Git配置信息,执行以下命令:

 

git config --list

 

后续将通过一个典型的Git工作流来学习。在这个过程中,你会创建一些文件、创建一个本地的Git仓库、提交你的文件到这个仓库中。这之后,你会克隆一个仓库、在仓库之间通过pull和push操作来交换代码的修改。注释(以#开头)解释了命令的具体含义

让我们打开命令行开始操作吧

下面创建一些文件,它们会被放到版本控制之中

 

复制代码
#Switch to home
cd ~/
# Create a directory
mkdir ~/repo01
# Switch into it
cd repo01
# Create a new directory
mkdir datafiles
# Create a few files
touch test01
touch test02
touch test03
touch datafiles/data.txt
# Put a little text into the first file
ls >test01
复制代码

 

每个Git仓库都是放置在.git文件夹下.这个目录包含了仓库的所有历史记录,.git/config文件包含了仓库的本地配置。

以下将会创建一个Git仓库,添加文件倒仓库的索引中,提交更改。

 

复制代码
# Initialize the local Git repository
git init
# Add all (files and directories) to the Git repository
git add .
# Make a commit of your file to the local repository
git commit -m "Initial commit"
# Show the log file
git log
复制代码

 

通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看git diff命令输出什么,然后提交这个更改到仓库中

 

复制代码
# Make some changes to the file
echo "This is a change" > test01
echo "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 files
git commit -a -m "These are new changes"
复制代码

 

下面会向你展示仓库现有的状态以及过往的提交历史

复制代码
# Make some changes in the file
echo "This is a new change" > test01
echo "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 branch
git diff

# Add the changes to the index and commit
git add . && git commit -m "More chaanges - typo in the commit message"

# Show the history of commits in the current branch
git log
# This starts a nice graphical view of the changes
gitk --all
复制代码

 

如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成

复制代码
# Create a file and put it under version control
touch nonsense.txt
git add . && git commit -m "a new file has been created"
# Remove the file
rm 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 flag
git commit -a -m "File nonsense.txt is now removed"
# Alternatively you could add deleted files to the staging index via
git add -A . 
git commit -m "File nonsense.txt is now removed"
复制代码

 

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢