Collaborative software with git

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

  1. Git basics ...
  2. Collaborative software

Git basics ...

The most difficult thing in big project, is to manage conflicts with files modified by others users. Git ca 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//.git

And then I can push my commited files to remote repo.

$ git push origin master

Collaborative software

But what if we work with others developer? What if we make changes in same files? 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 con 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 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 poroject:

  • 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 start 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

Is very dangerous to manage conflicts in merge with others developer. I suggest to use a good practice. The first time you can clone your project. Then you can commit in your local master. But ... what you can do to update your version to remote versione?

$ 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, you 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