preface

As I have been using Sourcetree for more than 3 years, I am familiar with every function of Git and flexibly use Git to solve various scenarios in development. I have also shared my experience in using Sourcetree many times among different teams in the company. Now I would like to share this tool with you.

This article is probably the most comprehensive visualization of using git tool Sourcetree on the Web.

Perhaps for many developers, it is good to understand git push, pull, and solve some simple conflict commands. In fact, Git is very powerful, but it is difficult to memorize every logic by memorizing commands. Of course, different IDE development environment has a variety of Git plug-in, operation method is different, it is better to learn cross-platform, is not restricted by the IDE Git management tool Sourcetree. It doesn’t matter if you work in Java, Python or Android.

introduce

Official introduction is as follows:

A free Git client for Windows and Mac Sourcetree simplifies how you interact with your Git repositories so you can focus on coding. Visualize and manage your repositories through Sourcetree’s simple Git GUI.

The translation:

Sourcetree, a powerful free Git client for Windows and Mac, simplifies how you work with your Git repository so you can focus on coding. Visualize and manage your knowledge base through Sourcetree’s simple Git GUI.

A little hole

Download www.sourcetreeapp.com/ this software is free, but you need to register, login authorization; The login and authorization steps are fine, but for registration you need scientific Internet access to register.

Some scenarios

I would like to share some advanced git application scenarios with you:

  1. A project includes development branch, integration branch, integration branch (stable version), production environment branch, etc
  2. A project includes base branches, which are assigned to each branch according to functions, each development management branch (more than ten branches), integration branch and production environment branch. (The ten or so branches only have code for their own module)
  3. One product has one main branch, and each company has personalized needs for implementation, and the needs are difficult to implement in the form of plug-ins, or plug-ins also need various personalized modifications, so each project implementation corresponds to each branch.

Here, I will briefly describe these usage scenarios, and the details will be introduced step by step later. Check out the Git branch:

Common use

This article will combine the Windows and MAC environments, but there are some differences in the interface or terminology between the two platforms, which will be explained as best as possible.

Add the warehouse

MAC clone from the URL

Git remote add orgin http://10.10.100.6:3000/sl/shaolei.gitCopy the code

Clone from the URL in Windows

Grab and obtain branch information

Grab (MAC’s name) for (name of the Windows) refers to the server change in the git repository information, such as backward several versions, advance several versions, we common people collaboration project, need to obtain the first before committing code, if there is a new server, then pull off the others change, conflict can reduce the code. (Menu bar)

Pull the code

$ git fetch
$ git merge orgin/masterCopy the code

I first divided the interface into menu bar, left bar, file bar, change bar and bottom bar for the convenience of the following introduction.

Click the pull button in the menu bar directly to pull the remote code changes to the local.

Submit code:

1. Submit the file properly

  1. First, select the file that is not temporarily saved, and click “Temporarily saved”. Or just hit Save All. (File bar)
  2. Enter a description of this code change in the text box below. (Bottom bar)
  3. Sourcetree does not push to the remote repository by default. Check to immediately push changes to Origin. (Bottom bar)
  4. Click submit (if you do not check to push changes to remote immediately, you need to manually click push in the menu bar)



Change the column
+



<dt></dt>
<dt></dt>



+






File bar

Temporary code

Similar to discarding, a code fragment is staged into a local staging area for commit, and the code that is committed is the staging area code. When a function has been implemented, we want to optimize the code, but we are worried that the code will be lost if the optimization is not successful. If there is no Git, we may back up a copy of the code, and we only need to save the code here. After temporary saving, the modification is normal, and the subsequent changes will appear in the change column of the untemporary file.




Submit code

1. Conflict resolution

When I and others change the same line of the same file, (most of us change a file, Git will help us to deal with it, automatically merge, but when changing the same line of the same file, there will be conflicts when pulling) as shown in the figure


storage
The menu bar

$ git stashCopy the code





The opening of new branch

In a project, we may have a development branch, an integration branch, a generation environment branch, etc. In this case, we just need to right-click on a node and select a branch

Push the branch

Newly opened branches are not displayed remotely, so you need to push the branch to remote.

$git push orgin test branchCopy the code

Switch branch

$ git checkout 3e1e7fcCopy the code


Pull the remote branch

Similar to switching branches, double-click the remote branch.

Roll back to submit

Sometimes we submit code that has problems and needs to be rolled back.

git revert *Copy the code

Example Roll back a commit

Right-click a change (in the case of sourcetree, a node) and select Rollback Commit.

Roll back the file

Roll back line

Ignore files

There are some folders in the project that need to be ignored rather than submitted to the repository, such as files in the bin and target directories.




Merging branches



Store (temporary) code

$ git stashCopy the code

As described above, it mainly means to temporarily store all changes so that temporary storage (called storage under Windows) can be applied.

Review the documents

Let’s say a file has been modified by a different developer and there’s a piece of code that needs to find the person who changed it. We can right-click through the file to see the author of each line.

$ git blameCopy the code



View file change history

From time to time, we need to see the history of changes in a file. Here, we can right-click to see the history of changes in a file

$ git logCopy the code




Open the historical version by file

Sometimes, we also need to view the history version of the file, here can select the node, select the changed file, right-click to open the history version


Use of labels

Sometimes we need to add a tag to a change, such as the stable 1.0 tag

git tag v1
$ git tag -dV1 // Remove the labelCopy the code


The archive

Archiving means packaging the current version into a ZIP package.

Check out the branch

The check out branch means to return the current project to the selected version, so we can easily go back to any version to compile the project or check for problems with the project at that time.

other

  1. A merge is typically used to merge all the changes from one commit into the current branch between branches.
  2. Rebase and interactive change are all changes to the base, mainly to change the identity of the remote branch name, most scenarios will not be used
  3. Reset to current node: This is a nice way to roll back the current branch to that branch and show all changes to the file, which is the same as when it was ready to commit (including all subsequent changes).
  4. Select a single commit from a different branch and merge it with your current branch, similar to patching, for example, to fix a bug that all branches have.
$ git revert
$ git cherry-pick 4a1fd5Copy the code

  1. Branch pull and push: pull from remote
  2. Trace branch: If not tracked, no connection is established between the local and remote branches
  3. Renaming a branch
  4. Delete branches, etc.

How do I view the commands corresponding to operations on the interface




Some good habits

  1. Discard as many meaningless changes as possible
  2. Assign developer tasks by module whenever possible
  3. Conflicts need to be resolved immediately after merging branches
  4. Reduce the number of blocks of code changes in a single file
  5. If you use my version to solve the problem, you need to inform the other party

Application scenarios

Space is limited, more next time!

conclusion

Through this article, you can solve more than 90% of the various code submission problems, as to what IDE environment, it doesn’t matter, use the IDE development tool to open the project in Sourcetree.