TortoiseGit Tutorial – How To Use TortoiseGit For Version Control

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.

=> Check ALL GitHub Tutorials Here.

TortoiseGit Tutorial

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.

Basics of TortoiseGit

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.

Clone the repository from GitHub

Enter the GitHub repository clone https URL and the local directory to download and store the copy of the artifacts. Click Ok once done.

Enter the GitHub repository clone https

The contents of the GitHub repository that is cloned is now available locally.

Contents of the GitHub repository

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.

Select + Add to stage the changes

In the following screen, you can commit the changes by clicking on the Commit button.

You can commit the changes

Add a commit message and select a few other options as shown and click on Commit.

Add a commit message

Once the commit is done, you can now push the changes as well to the GitHub. Click on the Push-button.

Click on the Push button

Push all branches

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.

Launch GitHub and look at the contents of the file

To look at the history of changes for the file, right-click on the file and go to TortoiseGit => Show Log

Show Log

To look at the Differences with the previous version, right-click on the file and select TortoiseGit =>Diff with the previous version.

Diff with previous version

To pull changes from the remote repository select TortoiseGit =>Pull

To pull changes from remote repository

Click Ok on the Pull screen that comes up.

Click Ok on the Pull screen that comes up

Creating Branches

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.

Create Branch

Name it enhancement and select the checkbox Switch to the new branch.

Switch to new branch

Click Ok.

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.

Compare with base

Click on Commit and Push.

Commit and Push

Click on Ok. The branch created is now visible in GitHub.

Branch created is now visible in GitHub

Tracking Branches

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

Browse References

The local enhancement branch is connected to the remote branch origin/enhancement as shown below.

Local enhancement branch

The same can be seen by running the Git command using ‘git branch-vv’.

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.

Git branch develop

References are shown in TortoiseGit. If it is untracked, then right-click and select the tracked branch.

References shown in TortoiseGit

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.

Switch Checkout

Select the branch and click OK.

Select the branch and click OK

Looking At The Log

To look at the log, select TortoiseGit => Show Log

Looking at the Log

Comparing Branches

To compare branches, right-click on the file explorer and select TortoiseGit => Browse References

Browse References

Click on the refs section and select 2 branches to compare. Right-click and choose to Compare selected refs.

Click on the refs section

The differences are shown below.

Differences are shown

You can right-click on the file and select Show changes as unified diff.

Show changes as unified diff

From the command line, you can run ‘git diff enhancement master’ to compare the branches.

Resolving Conflicts

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.

Push the changes

Select the local and remote branch accordingly as enhancement since you know that the changes you made were in the enhancement branch only.

Select Local and Remote branch

Click OK. So obviously you do see that the push is not successful due to conflicts.

Push Not successful due to conflicts

Now you will have to Pull the changes as the remote repository that also contains changes.

Remote repository also contains changes

Click on OK.

Remote Compressing object

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.

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.

Use this text block

Click on Mark as resolved

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”

Git Commit “enhancement”

Commit to enhancement

Click on Commit and Push.

Click on Commit and Push_Remote Enhancement

The changes are now pushed to the GitHub remote repository.

GitHub Remote Repository

Stash Changes

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.

Modified a file which is not tracked yet

At this stage, I need to stash my changes.

Stash Changes

Add a message and click on OK.

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.

Stash Pop

The changes are now stashed.

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.

Stash List

Stash Pop successful

Click on Yes to look at the changes.

Click on Yes to look at the changes

Conclusion

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.

=> Read Through The Easy GitHub Training Series.