Version Control Using GITAbout Blog Projects
There are many tools out there for applying versioning to your work, which is a great way to keep on top of large projects. My favourites and probably the most popular are TortoiseSVN and GIT. I will not discuss the pros and cons of version control in this post, as I am more interested in talking about the benefits of GIT, but if you would like this to be discussed further, feel free to leave a comment below.
To get started, you will need to download it from here. You have the option to install the command line version or the user interface, but since this is a tutorial on the GIT cmd, you will at least need that for now. Once this is installed, create a folder called "work" and create two text files in it called "doc1.txt" and "doc2.txt". Make sure they are not empty by populating them with some text of your chioce.
If GIT was installed correctly, you should now be able to right-click the folder and click "Git Bash", which will open the GIT command line in that location. To initialise the repository, type:
user@host:~$ git init
To test this has worked, you can view your current branch status by typing:
user@host:~$ git status
This should show you are in the master branch and that there are two files that are currently not versioned.
Adding/Committing Files To The Repository
There is a two stage process to adding and versioning files. Simply adding them only puts them into staging, which can be accessed from within all branches. Staging is used as an initial addition and allows you to make other changes before a more substantial commit. This allows you to bundle individual changes together when committing to the branch. To add all text files to staging type:
user@host:~$ git add *.txt
When all changes are ready, you can commit to the branch by typing:
user@host:~$ git commit -m "First commit"
The text inside the quotes is a message and can be changed to describe your commit. To view your commit history, type:
user@host:~$ git log
Creating Branches And Tags
When you need to work on a new project, it is a good idea to make a branch from the master branch, to keep any changes seperate. Then this can be merged into the main project when it is more complete/stable. To branch from master into a new branch type:
user@host:~$ git branch newBranch
Always make sure you are in the branch that you want to branch from and that it doesn't already exist. To move branches simply type:
user@host:~$ git checkout newBranch
Tags can be used as solid markers between builds. If for example you feel that you have completed a branch and merged it into master, you may want to create a tag to save the most recent, completed and working state. Merge a branch into master and create a tag by typing:
user@host:~$ git merge newBranch
user@host:~$ git tag newTag
Again, make sure that you are in the branch that you want to merge to, in this case it is master. Try creating a third document in the new branch and add/commit it using the instructions mentioned above.
Linking Branches Remotely
With GIT you have the option to backup your branches online. You will need to sign up for an account here, which is free and allows you to share your code with the community. Once you have created a new repository, make a note of the URL to bind your branches to.
To connect a branch to the new remote resource type:
user@host:~$ git remote add origin <repoLocation>
Where origin is the external repository. In order to use this however, we need to bind a local repository to it.
user@host:~$ git push origin master
You can include the flag "-u" after the word "push" to save the source and destination for future use. From then on, you will just need to type "git push". You will need your GIT account login details to run this command. To request data from the remote resource, you can use the pull command instead of push:
user@host:~$ git pull origin master
To view changes that haven't yet been committed to a branch type:
user@host:~$ git diff
To revert these changes back to their committed state, first run "reset", then use the "checkout" command to comit to the branch.
user@host:~$ git reset doc.txt
user@host:~$ git checkout doc.txt
Alternatively you may want to remove the file altogether. This can be done using the linux command "rm" or:
user@host:~$ git rm 'doc3.txt'
To clone a repository into a specic folder, type
user@host:~$ git clone <repo-location> <folder-name>
There are many other useful commands available with GIT. Please refer to this list, or leave a helpful comment below.