Git, TortoiseGit detailed tutorial

View git version information
Here Insert Picture Description

1. Create Repository

什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,
这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,
以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。由于git是分布式版本管理工具,
所以git在不需要联网的情况下也具有完整的版本管理能力。

1,1 created using GitBash
First, select a suitable place, create an empty directory (D: \ temp \ git \ repository).
Select the Git Bash to start right-click in the current directory.
Here Insert Picture Description
Here Insert Picture Description
Or start in the Start menu. Note that if it is launched from the Start menu gitbash need to switch directories to the directory where the warehouse.
Here Insert Picture Description
Creating a warehouse execute the command:
$ git the init
the GUI mode:
Here Insert Picture Description
Here Insert Picture Description
TortoiseGit created using 1,2
Just click in the directory when using TortoiseGit context menu select "Create Repository here"
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
repository was successfully created, it creates a hidden directory .git in this directory, as follows:
Here Insert Picture Description
Concept:
repository: ". Git" directory is the repository, the future will need to save the file to the repository.
Working Directory: contains ".git" directory catalog, which is the parent directory .git directory is the working directory. Only the working directory of the file can be saved to the repository.

2, add files

Create a mytest.txt file \ temp \ git \ repository under the directory: D
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
text into a file with "+" icon number:
Here Insert Picture Description
This added to the staging area
to submit files: Right-click on mytest.txt again select "Submit "At this time, save the file to the repository.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

3, work area and staging area

Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。
什么是工作区(Working Directory)?
工作区就是你在电脑里能看到的目录,比如我的reporstory文件夹就是一个工作区。
有的同学可能会说repository不是版本库吗怎么是工作区了?其实repository目录是工作区,在这个目录中的“.git”隐藏文件夹才是版本库。这回概念清晰了吧。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
如下图所示:

Here Insert Picture Description

我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

4, modify the file

When you're done, right click on mytest.txt file, and then select the "Submit"
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

5, see the revision of history

Right-click on the file select "Show Log" to view the files modification history.
Here Insert Picture Description
Here Insert Picture Description

6, diff

Here Insert Picture Description
Here Insert Picture Description

7, Revert all changes

When a file is modified you do not want to commit the changes, would like to revert to the state before unmodified. At this point you can use the "Restore" function
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Note: This action will remove all uncommitted changes, so as to restore the operation is requires careful careful! ! !

8, delete files

Here Insert Picture Description
You can also delete the local repository, and then git submit.
Here Insert Picture Description

9, Case: java project will be submitted to the repository

The first step: Copy javaproject-test project to the working directory
Here Insert Picture Description
Step two: Add the project to the staging area.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Ignore files or folders
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
choose to keep the local file. Upon completion of this folder will be more of a .gitignore file, this file is the file to ignore files, of course, you can manually edit. Which is the content of the bin, out of the catalog and other useless ignored. Then .gitignore files are also added to the staging area, and then add the code to the repository.

10, ignore file syntax specification

空行或是以 # 开头的行即注释行将被忽略。
可以在前面添加正斜杠 / 来避免递归,下面的例子中可以很明白的看出来与下一条的区别。
可以在后面添加正斜杠 / 来忽略文件夹,例如 build/ 即忽略build文件夹。
可以使用 ! 来否定忽略,即比如在前面用了 *.apk ,然后使用 !a.apk ,则这个a.apk不会被忽略。
* 用来匹配零个或多个字符,如 *.[oa] 忽略所有以".o"或".a"结尾, *~ 忽略所有以 ~ 结尾的文件(这种文件通常被许多编辑器标记为临时文件); [] 用来匹配括号内的任一字符,如 [abc] ,也可以在括号内加连接符,如 [0-9] 匹配0至9的数; ? 用来匹配单个字符。 
看了这么多,还是应该来个栗子:
# 忽略 .a 文件
*.a
# 但否定忽略 lib.a, 尽管已经在前面忽略了 .a 文件
!lib.a
# 仅在当前目录下忽略 TODO 文件, 但不包括子目录下的 subdir/TODO
/TODO
# 忽略 build/ 文件夹下的所有文件
build/
# 忽略 doc/notes.txt, 不包括 doc/server/arch.txt
doc/*.txt
# 忽略所有的 .pdf 文件 在 doc/ directory 下的
doc/**/*.pdf

11, add a remote library

现在我们已经在本地创建了一个Git仓库,又想让其他人来协作开发,此时就可以把本地仓库同步到远程仓库,同时还增加了本地仓库的一个备份。
常用的远程仓库就是github:https://github.com/,接下来我们演示如何将本地代码同步到github。
国内类似GitHub的网站gitee.com码云

11,1 create a repository on github
Here Insert Picture Description
Here Insert Picture Description

点击“create repository”按钮仓库就创建成功了。

Github支持两种同步方式“https”和“ssh”。
如果使用https很简单基本不需要配置就可以使用,但是每次提交代码和下载代码时都需要输入用户名和密码。
如果使用ssh方式就需要客户端先生成一个密钥对,即一个公钥一个私钥。然后还需要把公钥放到githib的服务器上。
这两种方式在实际开发中都用应用,所以我们都需要掌握。接下来我们先看ssh方式。

ssh协议
SSH 为 Secure Shell(安全外壳协议)的缩写,由 IETF 的网络小组(Network Working Group)所制定。
SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。
利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。

11,2ssh key generation
In windows we can use Git Bash.exe to generate a key, you can open Git Bash through the start menu or right-click menu
Here Insert Picture Description
git bash execute a command to generate public and private key
commands: ssh-keygen -t rsa
finished on Return Return It will create a folder and generate the file.
Here Insert Picture Description
After executing the command completes, the window the local user .ssh directory C: \ Users \ username .ssh the following public and private keys generated following names:
Here Insert Picture Description
11,3ssh key configuration
After the key generation need to configure the key locally on github can smoothly access.
Here Insert Picture Description
Here Insert Picture Description
The contents of the file id_rsa.pub added to it in the key section, and click "Add SSH key" button to complete the configuration.
11,4 synchronized to a remote repository
Can be used to synchronize remote repository git bash can also be used tortoiseGit
11,4,1 use git bash
Warehouse located in the directory (D: \ temp \ git \ repository) right click select "Git Bash Here", start git bash program.
Here Insert Picture Description
Then execute the following statement in the git bash:
git the Add Remote [email protected] Origin: sublun / mytest.git
git the Push -u Origin Master
Note: The red font need to be replaced as part of a personal user name.
If there are mistakes:
Here Insert Picture Description
you can first execute the following command, and then execute the command above
$ git remote rm origin
Here Insert Picture Description

11,4,2 using synchronous TortoiseGit
First, because the TortoiseGit use ssh tool is "PuTTY" git Bash to use ssh tool is "openSSH", if you want TortoiseGit also use the key just generated can do the following configuration:
Use ssh Synchronization:
Here Insert Picture Description
Click manage remote url:
Here Insert Picture Description
View network is currently configured SSH client:
Here Insert Picture Description
Putty key to select the private key :( own test configuration can not push past)
Here Insert Picture Description
management configuration remote path:
distal origin easily play is the default.
Url is the address of GitHub
Click Save to add:
Here Insert Picture Description
finished Click Push yes:
Here Insert Picture Description
HTTPS synchronization method:
This method does not require key pair. Enter the user name and password on the line.
Need to create a remote link
Here Insert Picture Description
Here Insert Picture Description
to enter a user name and password:
Here Insert Picture Description
Here Insert Picture Description

12, was cloned from a remote repository to local

Clone remote repository that is, from a remote copy to a local warehouse, will create a new local repository cloned. Select an arbitrary deploy directory in the repository, then clone the remote repository.
12,1 use git bash:
$ git clone [email protected]:sublun/mytest.git
ssh方式:
Here Insert Picture Description

12,2 use TortoiseGit:
Right-click in any directory:
Here Insert Picture Description
HTTPS mode:
Here Insert Picture Description
SSH ways:
Here Insert Picture Description
Here Insert Picture Description

13, taking the code from a remote repository

Git中从远程的分支获取最新的版本到本地有这样2个命令:
1.git fetch:相当于是从远程获取最新版本到本地,不会自动merge(合并代码)
2.git pull:相当于是从远程获取最新版本并merge到本地
上述命令其实相当于git fetch 和 git merge
在实际使用中,git fetch更安全一些
因为在merge前,我们可以查看更新情况,然后再决定是否合并
如果使用TortoiseGit的话可以从右键菜单中点击“拉取”(pull)或者“获取”(fetch)

Here Insert Picture Description
Access: different local and server content, get less.
Pull: the latest code can go back to the server.
More than one person operating a file server might have a conflict, the unsuccessful push phenomenon, you need to pull, manually resolve the conflict, resolve conflicts over mark, and then submit the repository.

15, using TortoiseGit achieve branch management

15,1 switching detection branch
Click switching branches can be detected in or create, here choose to check this is to create and switch to a new branch
Here Insert Picture Description
Here Insert Picture Description
after the new Well you can see two branches:
Here Insert Picture Description

15,2 create a branch
Can also choose to create a branch, this is not only to create a selection in the past:
Right-click in the local repository folder, and then select "Create a branch" from the menu:
Here Insert Picture Description
Here Insert Picture Description
to switch if you want to create a new branch directly to the finish can check the "switch to the new branch "option from the menu or select the" switching / detection "to branch switching:
Here Insert Picture Description
15,3 merging branches
After the switch to the dev branch can modify the workspace file, and then submitted to the master branch is not affected dev branch principle. For example, we modify the content mytest.txt, and then submitted to the dev branch.
Here Insert Picture Description
Here Insert Picture Description
After switching to the master branch or the contents of principles:
Here Insert Picture Description
the dev branches merged into the master branch content, the current branch is master. Select "Merge" from the context menu:
Here Insert Picture Description
Here Insert Picture Description
then view the contents of mytest.txt has been updated:
Here Insert Picture Description
15,4 resolve conflicts
Two branches editorial content are independent of each other without disturbing each other, then if all were the same in both branches to edit a file, and then merge, there may be a conflict.
For example, edit mytest.txt in the master branch:
Here Insert Picture Description
then submitted to the repository.
Here Insert Picture Description
Dev switch to branch to edit mytest.txt:
Here Insert Picture Description
Here Insert Picture Description
final merging branch, for example, dev branch into master branch. Need to switch to the master branch and branches merge.
Here Insert Picture Description
Version conflict.
Here Insert Picture Description
Conflict needs to be resolved manually.
First pull
to manually modify your content:
finished click on resolving conflicts in the submission, pushed to the remote repository.

Here Insert Picture Description
Here Insert Picture Description
Stand-alone right choice "to resolve the conflict" menu item in the file conflict:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
the conflict resolution is completed files to the repository on it.
Here Insert Picture Description
15,4 deleted branches
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Published 81 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_36205206/article/details/104534934
Recommended