Git- Part IV

git branch management

Use git in the IntelliJ IDEA

Chapter One: Creating a branch merge

Every time we submit, Git regard they strung together a timeline, this timeline is a branch. Up to now, only one timeline, in Git, this branch is called the main branch, that is, the master branch. HEAD pointer strictly speaking is not directed to submit, but point to the master, master is the point of submission, so, HEAD is pointing to the current branch.

In the beginning, master branch is a line, Git with master points to the latest submission, and then point to master HEAD, will be able to determine the current branch, and the submission of the current branch point:

Each submission, master branch will move one step forward, so, as you continue to submit, master branch lines are getting longer and longer.

When we create a new branch, for example, dev, Git built a pointer called dev, points at the same master, and then point to HEAD dev, says that the current branch on the dev:

You see, Git create a branch soon, because in addition to adding a dev pointer to change to change HEAD point, the workspace file have not changed!

However, from now on, to modify the workspace and submission is for a dev branch, such as new submissions after the first, dev pointer moves one step forward and the master pointer unchanged:

If we work on dev completed, the dev can be incorporated into the master. Git how merge it? The easiest way is to point directly to the master dev current submission, it completed the merger:

So Git merge branches soon! To change to change hands, workspace content is the same!

After merging branches, or even delete the dev branch. Dev branch is to delete the dev pointer to delete it, delete, we will rest a master branch:

Chapter II: TortoiseGit achieve branch management

2.1 Creating a branch

Right-click in the local repository folder, and then select "Create a branch" from the menu

If you want to create complete switch directly to the new branch can check the "switch to the new branch" option from the menu or choose "Switch / check-out" to switch branches:

2.2 merging branches

分支切换到dev后就可以对工作区的文件进行修改,然后提交到dev分支原理的master分支不受影响。例如我们修改mytest.txt中的内容,然后提交到dev分支。

切换到master分支后还是原来的内容

将dev分支的内容合并到master分支,当前分支为master。从右键菜单中选择“合并”:

再查看mytest.txt的内容就已经更新了:

第三章:解决冲突

两个分支中编辑的内容都是相互独立互不干扰的,那么如果在两个分支中都对同一个文件进行编辑,然后再合并,就有可能会出现冲突。

例如在master分支中对mytest.txt进行编辑:

然后提交到版本库。

切换到dev分支,对mytest.txt进行编辑:

最后进行分支合并,例如将dev分支合并到master分支。需要先切换到master分支然后进行分支合并。

出现版本冲突。

冲突需要手动解决。

在冲突文件上单机右键选择“解决冲突”菜单项:

把冲突解决完毕的文件提交到版本库就可以了。

第四章:在IntelliJ IDEA中使用git

4.1-在Idea中配置git

安装好IntelliJ IDEA后,如果Git安装在默认路径下,那么idea会自动找到git的位置,如果更改了Git的安装位置则需要手动配置下Git的路径。

选择File→Settings打开设置窗口,找到Version Control下的git选项:

选择git的安装目录后可以点击“Test”按钮测试是否正确配置。

4.2-将工程添加至git

第一步:在idea中创建一个工程,例如创建一个java工程,名称为idea-git-test,如下图所示:

第二步:创建本地仓库

在菜单中选择“vcs”→Import into Version Control→Create Git Repository...

选择工程所在的上级目录。本例中应该选择idea-projects目录,然后点击“OK”按钮,在工程的上级目录创建本地仓库,那么idea-projects目录就是本地仓库的工作目录,此目录中的工程就可以添加到本地仓库中。也就是可以把idea-git-test工程添加到本地仓库中。

选择之后在工具栏上就多出了git相关工具按钮:

第三步:将工程添加至本地仓库

直接点击commit按钮,将工程提交至本地仓库

然后点击“commit”按钮,将工程添加至本地仓库。

第四步:推送到远程

在github上创建一个仓库然后将本地仓库推送到远程。

在工程上点击右键,选择git→Repository→push,

或者在菜单中选择vcs→git→push

点击“Define remote”链接,配置https形式的URL,git形式的无法通过。然后点击OK

点击“push”按钮就讲本地仓库推送到远程,如果是第一次配置推送需要输入github的用户名和密码。

4.3-从远程仓库克隆

关闭工程后,在idea的欢迎页上有“Check out from version control”下拉框,选择git'

此处仍然推荐使用htts形式的url,点击“test”按钮后显示连接成功。

点击OK按钮后根据提示将远程仓库克隆下来,然后倒入到idea中。

4.4-从服务端拉取代码

如果需要从服务端同步代码可以使用工具条中的“update”按钮

Guess you like

Origin www.cnblogs.com/lpl666/p/12405289.html