Blog Pipers

The destination is just the beginning of another journey!

GIT repository with version control system

If you are associated with development and worked on big projects in a team, you might be already familiar with GIT. It is a distributed system for file repository with revision control system. During development phase of any project, team members often need to work on same file and keep track of the changes. GIT is the perfect system to maintain this process. It auto merges files based on lines, and also keep tracks of all the changes so that at any point of time we can go back to any revision. You might have heard or used SVN based sub-version system as well. But Git is a bit different than SVN and from my point of view, it’s much superior than SVN.

GIT is basically designed Unix/Linux based system, but it is available for Windows based system as well. You can setup your own GIT repository in a Linux based computer if you have or you can use third-party services like GitHub or Bitbucket. I personally prefer Bitbucket as it provides unlimited private repository at free of cost. The only constrain with Bitbucket is you can’t add more than five team members per repository until you go for a paid plan. But still it’s quite cool. Bitbucket paid plans also comes with very nominal charges. Whereas GitHub provides unlimited public repository with unlimited collaborators. But wait, it’s public! For private repositories in GitHub yoy need to go for paid plans. Both GitHub and Bitbucket provides desktop Git client for file pull/push of the files.

git windows app

GitHub Windows Client

sourcetree windows client

Bitbucket SourceTree Windows Client

If you don’t wish to use any of the clients above, you can simply use the command line tool with Git command. But yes, you need to know the proper commands for each each set of tasks.

All the modern IDE tools also has Git integrated with them, like Dreamweaver (need to add a separate extension), Aptana, NetBeans etc. I personally like to use NetBeans’ Git mechanism, it’s like a charm.

net-beans-git

NetBeans IDE Integrated Git Tool

How does the Git system works? It basically creates a .git directory in your project folder and keep tracks of all the changes into that folder, including change of files content, file/folder deletion, file/folder addition, renaming of file/folder etc. One important thing I would like to mention here. There are some issues in Windows when you rename a file/folder to just change the case. Windows based git doesn’t recognize this change well. So the trick here is to rename the file/folder to a different temporary name, and then rename again to the previous one with the changed case. There is a local repository in your computer and there is a remote repository of the same, e.g. in GitHub or Bitbucket repository. Now whenever you do some changes, you need to commit that change to your local repository first. Then pull from remote repository to check if there are any updated file in it. After the pull if Git is able to auto merge the files, it will, but if it can’t then it will show conflict and asks you to review and resolve. It happens sometimes if two collaborators worked on same file on same line but the contents are different. Resolve means, you need to manually review the local changes and remote changes and keep the final one. Then you need to commit the merged changes again to your local repository and push the changes to remote repository as well so that other collaborators can also pull the changes in. And this way all the collaborators have a local copy each and a central remote copy and by doing periodical pull/push all the team members will have the same set of files.

Now it’s time for some useful Git commands which we should know.

To setup a git repository:

git init
git remote add origin [https://github.com/your-account/your-repository.git OR [email protected]:your-account/your-repository.git]
git fetch --all
git checkout [branch name]

To check the current file/folder changes:

git status

Add file/folder to repository:

git add [file/folder path] (git doesn’t track empty directory)

Add all file/folder to repository:

git add . OR git add --all

To commit and push the change to repository:

git commit -m "commit message"
git pull origin [branch name]
git push origin [branch name]

Discard all local changes and rebase to Remote Branch:

git fetch --all
git reset --hard origin/[branch name]
git checkout [branch name]

How to add SSH Keys to your repository service?

At first check if a SSH key already exists in your system. To check that run the command below:
ls -a ~/.ssh

If you see id_rsa and id_rsa.pub already exists that means you already have a SSH Key generated. But if these files doesn’t exists, then you need to generate one. Run the following command to generate a SSH Key and copy it.
ssh-keygen
cat ~/.ssh/id_rsa.pub

Add the SSH public key to your repository service.
For Bitbucket
For Github

Now check if the key is installed properly by running the command below:
ssh -T [email protected]

Create a SSH config file:
vi ~/.ssh/config

Add the lines below to add identity for bitbucket/github:
Host bitbucket.org
IdentityFile ~/.ssh/id_rsa

Add this file for auto start the SSH agent:
vi ~/.bashrc

SSH_ENV=$HOME/.ssh/environment

# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi

Start the SSH agent and add key:
eval ssh-agent
ssh-add -l

If you see “Could not open a connection to your authentication agent.” error, then follow these steps:
Find git path: which git
Copy output path (except git): /usr/bin/git
Run the following commands:
cd /usr/bin/
bash
exec ssh-agent bash
ssh-add -l

Scroll Up