It was not until a few months back that I was given the opportunity to work with GIT. GIT is a distributed version control system. Simply put, it is a piece of software that manages source code.
Having worked with SVN previously, switching to GIT was a pleasant change. The most noticeable difference between SVN and GIT lies in the fact that GIT is distributed whereas SVN is not. This means that Git does not depend on a centralized server, but does have the ability to synchronize with other Git repositories. Once a repository is cloned using GIT, all the commits, diffs, merges and more can be done entirely offline. I also noticed that it takes longer to type in GIT commands than it does to execute, whereas in SVN commands take longer to execute. In short, GIT makes for a much better work experience.
I found that GIT was a bit complicated to use, especially in the beginning. Even after a few months into working with it, I still find that there is a lot more to learn about GIT (evidently proven by some of the blunders I have committed), than I already know. A few important lessons I learnt while working with GIT were:
- It is always good practice to work with the latest most updated version of code, and to do so, a git pull before starting work on any branch is a must.
- Double checking the branch I was working on really helped because discovering that I was working on a wrong branch was not fun.
- Using the remove command with caution, because once files are deleted from a particular branch there is no way of getting them back, EVER, unless of course I reverted to previous commit level and re-did all my changes.
- Merging one branch into another can be very tricky. The most important thing in resolving conflicts between files during merging is knowing what caused the conflict, not to mention the patience to resolve them.
- Checking in the wrong files can cause problems, and therefore making sure that the right files are being checked in is the right way to go.
- Tagging in GIT can be very useful especially when referring to a particular commit is necessary.
- The git status command is really helpful in pointing out what files have been changed or updated.
An interesting thing I noticed about GIT is that it is an intelligent software, in the sense that it prompts for the right commands in cases where commands were misspelt. It is also interactive and helps in describing what each command does. Using GIT commands in the wrong way is common, in such cases the git help command is really useful.
Below is a list of some of the GIT commands I regularly use.
1) git clone : Creates a local git repository copy from a remote source.
git clone git@github.com:user/test.git
2) git pull : Pulls in the latest changes from the origin
git pull
OR
git pull origin <branchName>
3) git add : Adds new or updates files to the current branch
git add <fileName>
4) git commit : Commits all local changes
git commit -a
5) git status : Checks for files that are changed in the local directory.
git status
6) git push : Pushes local changes to the origin
git push origin <currentBranchName>
7) git branch : Gives the current working branch.
git branch
master
* new
The * here denotes the current working branch.
— To create a new branch off an old branch :
git checkout -b <newbranch> <oldbranch>
— To Switch to a different branch from the current working branch :
git checkout <differentBranch>
— To get a list of all branches:
git branch -a
— To delete a branch locally :
git branch -d <branchName>
– To delete a branch remotely
git push origin :<branchName>
8) git tag : To create a tag that refers to a particular commit
git tag <tagName>
– To push tags to the origin
git push –tags
– To delete a tag locally
git tag -d <tagName>
– To delete a tag remotely
git push origin :<tagName>
9) git revert : To revert to a specific commit
git revert <specificCommit>
10) git diff : Displays differences between two commits
git diff <oldCommit> <newCommit>
11) git log : To get a history of changes
git log
12 ) git rm : Removes files from the current working directory and branch
git rm <fileName>
13) git merge : Merges a branch into another.
Make sure to do a git pull on the branch that is being merged into and then run the git merge command.
git merge <newBranch>
In conclusion, as a developer, GIT has definitely made life easier. I hope to write soon about other softwares and tools I use.
Kruthika M Kumar