The steps and commands are all in Ubuntu (Linux) and should be almost same for Mac OS as well but haven’t tested it myself. And GIT server is GitHub itself. I am not going to give any theoretical definitions of anything here. Directly the commands are presented for different purposes.
Clone a Repository
Assumed that you have a repository given of a project. Two different URLs are provided by GitHub HTTPS and SSH. I prefer SSH and for that you need to add the SSH Key of your computer to the GitHub server, please look at the page here https://help.github.com/articles/generating-ssh-keys for more details about generating and adding SSH Keys to the GitHub. Now to clone the project using the repository. Go to the directory where you want to clone the project. Normally it should be /var/www:
$ cd /var/www
$ git clone <repository_link>
Note: Don’t create the directory because it will create itself
List branch and check your current branch.
$ git branch
That means you are in dev branch which is with *.
$ git checkout master
Now your working branch is changed to master.
Create a branch
$ git branch <branch_name>
$ git checkout -b <branch_name> <source_branch>
The later command creates a new branch and switches to newly created branch.
Status means to check if there are any changes made in the current repository/branch. To check the status:
$ git status
This will list the added/modified files.
Adding added/modified files
$ git add // or to add all the modified files than just use period (.)
Committing the changes
$ git commit -m “Give some commit message.”
Pull the updates from the server
$ git pull origin dev // get the updates from the server in dev branch, origin is the server name provided by GitHub
Push the commits to the server
$ git push origin dev
So basically when you need to get the updates from the server in a branch, you need to pull and when you need to push the changes that you have made locally then you will have to push it.
Merging means combining one task done in a branch to another. Normally the sub-branches are merged to the main branches. The branches checked out from a branch are sub-branches. For example, if you have taken a branch dev from master and you worked on dev. You need to merge the works done in dev branch to master.
$ git checkout master //<< first checkout to the target branch
$ git merge dev //<< merge the dev branch to master
Conflicts & resolving conflicts
Sometimes conflicts might happen in the files while merging or pulling the updates. Conflicts happens when there are changes in single file in two different branches at once. To resolve the conflicts, one can just open the file and remove/manage their codes but it is difficult to do in that way simply opening the file. Using GIT’s mergetool it is quite easier to resolve the conflicts in GUI. I normally use meld, so if you don’t have one in your Ubuntu then install it:
$ sudo apt-get install meld
Once installed, hit the following command:
$ git mergetool
Please run this command only when there are conflicts. Once you hit this command, you will have a three column editor opened. Three columns contain three copy of the files, base (branch) code, local copy and the remote copy.