Useful git commands with Sourcetree GUI
Tags: git, github
Published: 2019-10-05 06:36:29

## Introduction

Remembering many git commands and also doing it in the terminal can be hard if you don't have too much practice. Graphical User Interfaces (GUI's) can make your life easier and improve your productivity. One of that is the [Sourcetree](https://www.sourcetreeapp.com/).

Sourcetree is a free Git client for Windows and Mac that simplifies how you interact with your Git repositories so you can focus on coding. It enables you visualize and manage your repositories through a simple Git GUI. 

In this post we will show how to run some git daily basis commands with the Sourcetree GUI.

## Commmands

### Log

Shows the commit logs.

Command: `git log` ([doc](https://git-scm.com/docs/git-log))

In Sourcetree we can check the log in the **History** tab.

![Sourcetree history](https://thepracticaldev.s3.amazonaws.com/i/oa0e9i86jpuscois6otz.jpg "Sourcetree history")

### Commit

Record changes to the repository

Command: `git commit -m <message> [--amend]` ([doc](https://git-scm.com/docs/git-commit))

In Sourcetree you can achieve the same result clicking in the rounded Commit button at the top left corner, write the message in the text box and commit it by clicking the right bottom button.

![Sourcetree commit](https://thepracticaldev.s3.amazonaws.com/i/5vaaimwylk2vnb4pyqvv.jpg "Sourcetree commit")

Adding the **amend** option you will replace the tip of the current branch by creating a new commit. For doing this in Sourcetree you need select the **Commit Options** in the right top of the commit input box and select the option **Amend last commit**.

![Sourcetree commit amend](https://thepracticaldev.s3.amazonaws.com/i/hvlnzcnouhtvqj8br99v.jpg "Sourcetree commit amend")

### Diff

Show changes between commits, commit and working tree, etc

`git diff <commit1> <commit2>` ([doc](https://git-scm.com/docs/git-diff))

For doing the same in Sourcetree you just need select 2 commits in the History.

![Sourcetree diff](https://thepracticaldev.s3.amazonaws.com/i/a19abrm2n5mt69zoythf.jpg "Sourcetree diff")

### Stash

Use `git stash` when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the `HEAD` commit.

`git stash push -m <message>` ([doc](https://git-scm.com/docs/git-stash))

In Sourcetree for push a new stash you need select the **Stash** button in the top toolbar.

![Sourcetree stash push](https://thepracticaldev.s3.amazonaws.com/i/xoli1w7twbe7z0hojyao.jpg "Sourcetree stash push")

For apply or delete a stash you can use the **Stashes** menu in the sidebar. 

![Sourcetree stash apply/delete](https://thepracticaldev.s3.amazonaws.com/i/cz1j27jm3e30p7sj325f.jpg "Sourcetree stash apply/delete")

### Checkout

Switch branches or restore working tree files

`git checkout -b <branch>` ([doc](https://git-scm.com/docs/git-branch))

In Sourcetree first you need right click in the commit you want and select the **Branch** option.

![Sourcetree checkout](https://thepracticaldev.s3.amazonaws.com/i/frgd88tivkupsv2menhy.jpg "Sourcetree checkout")

Then you just need add the branch name and confirm.

![Sourcetree checkout - new branch](https://thepracticaldev.s3.amazonaws.com/i/g2aidsb9bjjgoc7dxpji.jpg "Sourcetree checkout - new branch")

### Add Remote

Adds a remote named `<name>` for the repository at `<url>`.

`git remote add <name> <url>` ([doc](https://git-scm.com/docs/git-remote))

For adding a remote you need first create a remote repository in some host service like [Github](https://github.com/) or [Bitbucket](https://bitbucket.org/). For example, after create a repository on Github you have some like that:

![Github new repository](https://thepracticaldev.s3.amazonaws.com/i/7xy0fdvh0ack7o5rftf5.jpg "Github new repository")

To add this remote in Sourcetree first you need right click on the Workspace menu in the sidebar.

![Sourcetree new remote](https://thepracticaldev.s3.amazonaws.com/i/7qu700s1qowqqy3r7tbo.jpg "Sourcetree new remote")

Then add the remote `<name>` (usually defined as "origin") and `<url>` in the fields and click in **OK**. That's it. Your new branch is ready.

![Sourcetree new remote form](https://thepracticaldev.s3.amazonaws.com/i/az3iug1816c5renpnjfj.jpg "Sourcetree new remote form")

### Merge

Join two or more development histories together

`git merge <branch>` ([doc](https://git-scm.com/docs/git-merge))

For merge branch in Sourcetree you just need right click in the branch commit that you want and choose the option **Merge**.

![Sourcetree merge](https://thepracticaldev.s3.amazonaws.com/i/mccdpbchzs36u5zxf174.jpg "Sourcetree merge")

If you have some conflict you can also fix it by right clicking in the conflicted file, select the **Resolve Conflicts** and pick the desired option. `Resolve Using 'Mine'` means you want use the solution of your current branch and ``Resolve Using `Theirs'`` means you want replace the solution for the selected commit. 

![Sourcetree resolve conflicts](https://thepracticaldev.s3.amazonaws.com/i/25p8mt8hen8nd530lbaz.jpg "Sourcetree resolve conflicts")

### Squash

To Squash commits you should use rebase command in interactive mode. Rebasing interactively means that you have a chance to edit the commits which are rebased. You can reorder the commits, and you can remove them (weeding out bad or otherwise unwanted patches).

Start it with the last commit you want to retain as-is:

`git rebase -i <after-this-commit>` ([doc](https://git-scm.com/docs/git-rebase))

An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart’s content, and you can remove them. The list looks more or less like this:

```
pick deadbee The oneline of this commit
pick fa1afe1 The oneline of the next commit
...
```

You can squash commits changing the word `pick` into `squash` (or just `s`) next to it. The result would be:

```
pick deadbee The oneline of this commit
squash fa1afe1 The oneline of the next commit
...
```

For squash commits in Sourcetree first you need right click in the commit before that you want and select the option `Rebase children of <commit> interactively...`.

![Sourcetree rebase interactively](https://thepracticaldev.s3.amazonaws.com/i/gorqvl221ar8770o44e8.jpg "Sourcetree rebase interactively")

Then select the commits you want squash and confirm. You can also edit the commit message if you want.

![Sourcetree squash commits](https://thepracticaldev.s3.amazonaws.com/i/ac3ycf5i1x5z5uzikkey.jpg "Sourcetree squash commits")

## Conclusion

In this post we learned some useful git daily commands (like commit, merge, stash, etc.) using the Sourcetree GUI. It enables you visualize and manage your repositories improving your productivity and making you focus on what matters: coding.

If you believe there is some command missing or have any other suggestion please let me know commenting below :).