Friday, February 14, 2014

Using Git for version control part 2

In the last part of the previous tutorial you already used some git commands like clone and push. Most of the git commands are pretty straightforward and do exactly as the name says, even though, I will still explain briefly what the command is used for and how to use it.

The Commit command will open the staging window, in which you can select all of the files you want to commit to your local branch (more on branching below). For now remember that this will not affect the remote repository but only save the changes to your local working space. Committing your changes is necessary if you want to switch branches. A commit is also good to just save your temporary changes to the code to come back working on it later.

This command will upload your committed work to the remote repository. Usually done in combination as "Commit & Push" from the previous tutorial.

The pull command will open a pull dialog in which you can select some options:

In the example above we want to checkout the remote branch 'master' to our local branch 'master'. We also specify that we only want to fetch the remote changes and not merge or rebase anything, since it is the same branch.

The Fetch command can be found in the pull drop-down menu:

It will update all pointers to existing branches from the remote repository. For example: someone else made a change to the master branch and committed to the remote master branch. If you press the fetch command, it will set the pointer from the origin/master branch to a new version shown below:

This says your local copy of the repository is still on the "out-dated" version of this branch on "Test", while there is a new commit already pushed to the origin/master branch shown above it as "New Test".

To retrieve the changes made in this commit you will have to checkout this branch shown below. You could also pull all changes in this branch by rebasing the origin/master in your local repository, the results will be the same (in this case).

Branches are what makes Git really useful to work with. For this tutorial we will assume we have two teams working on a piece of software: the first one will focus on the implementation part of the code and the second team is creating a GUI for it. To realise this in Git we can create two branches in our project by using the command "Create Branch" to create "Implementation" and "GUI" branches. The result so far will be shown as follows:

The GUI and Implementation branches are on the same commit as the master and origin/master branches. We will now change our current branch to the GUI branch, apply some minor changes, then commit and push them to the remote repository. When it asks if you want to add this branch to the remote repository, click yes.

Now checkout the Implementation branch and you will see that the changes you have just made to your GUI branch are undone! Talking in SVN terms: you have just created two "seperate" repositories next to each other, one called Implementation and one called GUI.

To continue with the next part of the tutorial, make some minor changes to the Implementation branch as well and commit and push it as you did before. The result will look something like this (you might have to click View -> Show all branches first):

In our example from branches, the GUI team has reached a point where they made a few buttons. The Implementation team wants to have these buttons in their Implementation branch, how do we do this?

Change to your Implementation branch, and choose Command->Merge branches (or Ctrl + M). You will see the following window pop up:

As you can see you're currently in the Implementation branch and you want to merge it with the origin/GUI branch (where the other team has pushed their changes). Shown in the example to the left you can see what will happen, the C and D nodes represent changes to your current branch (we have one, called "Test3"). The E and F nodes represent changes the GUI branch, in our example shown as "Test2". This merge will combine these changes to your local branch of Implementation. If you don't run into any conflicts (e.g. you haven't changed anything on the same line in any file) it will merge automatically and the Implementation team will have their fancy buttons. If however you did change the same line in a file, you will run into a conflict.

File conflicts:
We tried to merge two branches and Oops! We got a file conflict! Click "Resolve conflicts" to get to this next window:

As you can see, there is a conflict in the .gitignore file. File conflicts are solvable withing Git Extensions (actually they are solved by KDiff3). Click on the "Open in kdiff3" button to get this next screen:

You can see you have 3 files, shown with red underlines they are your Base file, Local file (from your local branch, Implementation) and the Remote file (the file from the GUI branch). You can see that there are different versions in both branches. The output will be the bottom most screen. To solve the conflict you can press the blue A, B and C buttons on top or even manually type it in the bottom code if you want a little bit of both. If you have multiple conflicts you can click on the arrow buttons to automatically scroll to the next conflict to solve. If you are done solving conflicts, save your changes and close this screen.

You will now be asked if you want to commit and push these newly made changes from the conflicts, press commit. After you've done that you successfully merged the GUI branch in our Implementation branch, with the result showing up as follows:

After doing this you can still use your GUI branch to continue working on the GUI. The Implementation branch will have the changes made in the GUI branch from the time of the merge. Even though this example was good to illustrate merging, I'd advise against it and instead merge your changes from both Implementation and GUI branches to the master branch (if it all compiles of course!).

These were the basics of working with Git in the Git Extensions GUI, you can leave any questions or comments below.

Wednesday, February 12, 2014

Using Git for version control part 1

This tutorial is about version control using Git. For this tutorial I will assume you are familiar with some kind of source control, for example SVN. We will skip the basics of how all source control works and just focus on working with Git in this case. The Git workflow is most commonly used for open source projects, because it's excellent for communication and coordination. In this tutorial, however, we're aiming to use Git for a private repository, and in order to achieve that you will need an account on Bitbucket. If you're a student, you can get some free private repositories on GitHub too!

Installing required software:
Git Extensions: The GUI we will use in this tutorial, Git Extensions, is the most suitable GUI for larger software projects, because it uses KDiff3 to solve conflicts (more on conflicts in the next part of the tutorial). If you work alone, or you know you will not bump in any file conflicting scenario's it's easier to download Github's GUI. This will not be covered in this tutorial, but it's fairly easy and straightforward to use once you know the basics of Git.

Git Extensions has its own version of Git included, if you want the newest Git, you can download it here: Git for windows. This installs the basic source control management.

Setting up a repository:
If you are joining an already existing project you can skip this step! Just make sure you are invited to the project (if it was a private one) and that you can reach the actual repository.

Once you log in to your account on Bitbucked you will see your dashboard and from there click the Create or Create repository button. You will see a screen like this:

Here you will have to name your project and choose which kind of source control you are going to use, in this case it will be Git. The Access level is automatically set to private, which means (at first) only you can reach this repository, which is desired. You can also choose to have Issue tracking which is basically a neat function to report bugs and couple them to this repository. The Wiki option allows you to create documentation for the current repository. Finally there are some language specific options which will set you up with some basics for the programming language in your repository.

Once you have done this, you're done setting up your repository and it's time to clone it and get started.

Cloning an existing project:
From here on, we will use the Git Extensions GUI. The first time you start Git Extensions you will probably have to fill in some personal information like your username. After that you will arrive at your Dashboard, which will most probably be empty. Listed among the Common Actions, or alternatively in the Start menu, you will find the "Clone Repository" option. You will see something like this (I already filled it in):

The "Repository to Clone" is the project you want to create a local copy of (or a local directory you want to create a Git Repository of). The rest of the options are about where you want to locate this repository on your PC. The rest of the options (should) be automatically set as seen above. You can find the link to your repository on Bitbucket by going to your repository page and clicking "Clone". Copy&Paste the HTTPS into Git Extensions as I did in the picture and make sure to remove the "git clone" commands at the start (these are done automatically for you by Git Extensions).

Important: If you get the error "fatal: could not read Password for" etc. You will need to run the following command in the Git Bash (Tools -> Git Bash, or Ctrl + G):git config --global core.askpass /usr/libexec/git-core/git-gui--askpass

This will re-enable the notification which asks you to fill in your password instead of directly declining your request.

Making your first Commit and Pushing it to the repository:
If you followed the steps above you will be in your currently empty repository in Git Extensions, looking at the following screen:

Click the "Edit .gitignore" button which will open a new window with a text editor. For now, just click the "Add default ignores" and save the file. This file will tell Git which files should be ignored on each commit so they won't bother showing up on staging each commit. After you've done that, click the "Commit" button, which will open the staging window. Select your newly made .gitignore file and press the "Stage" button with the purple arrow pointing down. By doing this you want to commit only the .gitignore file and nothing else, which is what we want.

To finish this tutorial, enter some commit message and press the "Commit & Push" button which will stage the file to commit and push it to the remote repository on Bitbucket (which you can see on Bitbucket itself if you click on source).

This was the first part of the tutorial showing you how to set up your private repository using Bitbucket. In the next part I will show you the power of Git by using branches and solving file conflicts on commits.

Continue to part 2