Collaborative software with git

  1. Git basics ...
  2. Collaborative software

Git basics ...

The most difficult thing in a big project, is to manage conflicts with files modified by others users. Git can do this work for us.

I can create a project on my local computer

$ git init

I can add files to it

$ git add .
$ git commit -m 'A little story to understand changement'

I can create a repository on github (or bitbucket). I can add this remote repository to my own local project:

$ git remote add origin git@github.com/<yourusername>/<yourrepository>.git

And then I can push my committed files to the remote repository.

$ git push origin master

Collaborative software

But what if we work with others developer? What if we make changes to the same file? I'll give an answer to this question.

Better case: no conflicts

In better cases, there are no conflicts. This means that a developer can change the view. Another one the model. Both can run the sequence:

$ git add .
$ git commit -m 'committed some stuff'
$ git push origin master
Worst case: conflicts

There are different ways to solve conflicts in git. I'll explain two.

Manage versioning of our software

Imagine to have your new project:

$ git init
$ touch readme
$ git add .
$ git commit -m 'first commit'

Now, if you want, you can see the graph of your project:

$ git log --graph --oneline

The output will be like this:

* 719b3f2 first commit

This is your version 0.0. You can create a branch 0.0.

$ git checkout -b 0.0

So you'll have a branch in all life of this project. But what if you want to add some features for version 0.1? You can call this new feature like 'add an index file' and 'add a changelog file'. Point to 0.0 version and create a branch. Add some files. Commit. ... follow these steps:

$ git checkout 0.0
$ git checkout -b 0.1.index 
$ touch index.php
$ git add .
$ git commit -m 'Added index file'

Take a look again to the graph of your project:

* 21cb1bc Added index file
* 719b3f2 first commit

Come on!!! =) We can add new feature before release 0.1 version!

$ git checkout 0.0
$ git checkout -b 0.1.changelog
$ touch changelog.md
$ git add .
$ git commit -m 'Added changelog'

Again to the graph

* e9fe3ad Added changelog
* 719b3f2 first commit

We have two steps because with "$ git checkout master" we have started this branch from 0.0.

Now we have 0.0, 0.1.changelog and 0.1.index branch. What you need, now, is create your version 0.1:

$ git checkout master
$ git checkout -b 0.1
$ git merge 0.1.index
$ git merge 0.1.changelog

And now?

$ git log --graph --oneline

*   3dbd397 Merge branch '0.1.changelog'
|\  
| * e9fe3ad Added changelog
* | 21cb1bc Added index file
|/  
* 719b3f2 first commit

Now we can see our two branch.

Manage conflicts with other developers

It's very dangerous to manage conflicts on merge with others developers. I suggest to use a good practice. First you can clone your project. Then you can commit on your local master. But ... what you can do to update your version to the remote version?

$ git pull origin master

Pull execute 2 command. The first is "git fetch" that update your code. And the second one is "git merge". So, your code is merged with the one the is online. Now you can push your code to remote repository stored on github.

$ git push origin master