This Tutorial Explains how to Download, Install and use the Git Version Control Client – TortoiseGit, a free Open-source Tool for Git-based Repositories:
In our previous tutorials in the GitHub series, we saw how to work directly on the remote repositories and also explored about working offline through Git commands and GitHub desktop.
In this tutorial, we will see another Git version control client called TortoiseGit that is installed as an extension to Windows shell. This is very similar to TortoiseSVN if you have worked with it.
What You Will Learn:
Introduction To TortoiseGit
TortoiseGit is a free open-source client tool for Git-based repositories and manages files along with tracking changes to it.
The latest release of TortoiseGit can be downloaded and installed from here
In this tutorial, we will focus on the developer activities by cloning the repository from GitHub and performing the following activities in the local repository.
- Basics of TortoiseGit
- Commit files
- Creating branches
- Resolving conflicts and merging.
- Push changes back to the repository.
- Comparing branches
- Stash changes
Basics Of TortoiseGit
TortoiseGit is installed as a Windows shell extension and can be accessed and invoked from the context menu by right-clicking on the local git repository or a folder.
Clone The Repository From GitHub
To begin with let’s start by cloning the repository from GitHub to work on the same in the local repository. Open the File Explorer in your Windows machine. Right-click in free space and select Git Clone.
Enter the GitHub repository clone https URL and the local directory to download and store the copy of the artifacts. Click Ok once done.
The contents of the GitHub repository that is cloned is now available locally.
Basic Commit And Push To GitHub
Now as the GitHub repository contents are available locally let’s modify a file, commit and push the changes to GitHub.
Open the file and make changes. Once done right-click and select + Add to stage the changes.
In the following screen, you can commit the changes by clicking on the Commit button.
Add a commit message and select a few other options as shown and click on Commit.
Once the commit is done, you can now push the changes as well to the GitHub. Click on the Push-button.
Click Ok. The changes would now be available in your GitHub repository.
Launch GitHub and look at the contents of the file. As seen above, the back to back operations of Add-Commit-Push can be done once the files are modified in the local repository.
To look at the history of changes for the file, right-click on the file and go to TortoiseGit => Show Log
To look at the Differences with the previous version, right-click on the file and select TortoiseGit =>Diff with the previous version.
To pull changes from the remote repository select TortoiseGit =>Pull
Click Ok on the Pull screen that comes up.
Master is the main branch for every repository that would typically contain code for production deployment or for your releases. So, the master branch in a way would be protected by the teams to avoid direct commits to it.
Thus, the development teams would normally create additional branches like feature, bug, enhancement, etc. to isolate their work from others and then merge the changes to the main master branch.
Let’s look at how to create branches in the local repository using TortoiseGit and push the changes back to the remote repository.
Right-Click in the File explorer within the repository and select TortoiseGit =>Create Branch.
Name it enhancement and select the checkbox Switch to the new branch.
Make a change to the file in the enhancement branch and commit the same.
In the commit screen, you can also diff with the file in the master branch. Right-click on the file and select Compare with the base which is master in this case.
Click on Commit and Push.
Click on Ok. The branch created is now visible in GitHub.
As the local branch is created, it also has a relationship with the remote branch when you do a push or pull or clone. To look at which remote branch the enhancement branch is connected to right-click and select TortoiseGit => Browse References
The local enhancement branch is connected to the remote branch origin/enhancement as shown below.
The same can be seen by running the Git command using ‘git branch-vv’.
If we create another local branch and have not yet pushed changes, then it would be shown as untracked on the GitHub server.
References are shown in TortoiseGit. If it is untracked, then right-click and select the tracked branch.
Switch To A Branch
As branches are created, to start working on the branch you can right-click in the file explorer and select TortoiseGit => Switch/Checkout.
Select the branch and click OK.
Looking At The Log
To look at the log, select TortoiseGit => Show Log
To compare branches, right-click on the file explorer and select TortoiseGit => Browse References
Click on the refs section and select 2 branches to compare. Right-click and choose to Compare selected refs.
The differences are shown below.
You can right-click on the file and select Show changes as unified diff.
From the command line, you can run ‘git diff enhancement master’ to compare the branches.
As the Dev team members work on their local copy of the repository and push their changes, it is imperative that when you pull the changes to update your local repository, conflicts would arise. Let’s see how to resolve the conflicts.
Scenario: Make changes directly in the GitHub repo and in the local copy of your repository as well in the enhancement branch.
Now there are changes to the same file both in the remote repository as well as in the local repository.
From the file explorer of your local repository directory Add the file to staging and commit the changes as well as shown in the previous section. Post the commit, you will need to push the changes. Click on the Push button.
Select the local and remote branch accordingly as enhancement since you know that the changes you made were in the enhancement branch only.
Click OK. So obviously you do see that the push is not successful due to conflicts.
Now you will have to Pull the changes as the remote repository that also contains changes.
Click on OK.
Click on Resolve. As there are conflicts, you will need to resolve those manually and then commit/push the changes to the remote repository. In the next screen, right-click on the file and select Edit conflicts.
In the Merge window that comes up, click on the appropriate change and select the change to be used. Right-click and select Use this text block as shown.
On the left is the remote repository changes and on the right is the local repository changes.
Do the same for all the differences and click on Mark as resolved, Save and exit the merge window.
We can now commit the file and push the changes. Right-click and select Git Commit => “enhancement”
Click on Commit and Push.
The changes are now pushed to the GitHub remote repository.
If a developer is working on new changes to the set of files but then suddenly, he has to fix a few bugs reported, then at this stage, there is no point to commit the half-done work. It is better to stash the work or suspend the current work going on. Fix the bug and re-apply the earlier changes.
Let’s see how we can stash changes using TortoiseGit. Suppose that you have modified a file that is not tracked yet.
At this stage, I need to stash my changes.
Add a message and click on OK.
Click on Close. At this stage, I can also select stash pop and reapply the last saved change.
The changes are now stashed.
To re-apply the last changes, right-click in the file explorer and select TortoiseGit Stash Pop. Stash List can also be selected to apply other changes.
Click on Yes to look at the changes.
We hope that you would have enjoyed and got some perspective on Software Configuration Management (Version Control) through this series on usage of GitHub and Git client (GitHub Desktop and TortoiseGit).
Through this series of tutorials, we have tried to cover what a developer would need to work on using these tools from a Git usage perspective.