jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Version Control and Git – Part Three

A long time ago (like 6 months ago) I promised that I would complete my git aka version control tutorial. Looks like I get to finally do it. Meanwhile, you can find the first two parts, here and here.

This post talks about actual usage, while the earlier ones talked about some other stuff. There are two things happening here. One, is the online repository. There are many folks who provide repository services, and I use Bitbucket. You can create a free account at them, and they allow any number of repos. Further, they also provide private and public repos. So, that is good because some services don’t allow private repos without having to fork out a lot of money.

So, you will visit Bitbucket website and then create an account. I won’t write how to create an account, because if you want steps for that, you should probably not be a developer. After the account is created, you can create teams (and then add members). You can create projects, and then within projects, you can create repos. All this is pretty self-explanatory once your account on bitbucket is ready.

Once a new account is created, there are two scenarios – you already have code on your programming PC, or you are starting from scratch. Right now, I am looking at the first scenario because that is the most likely scenario for those who begin code on the PC and then wish to push it to the online repo.

Start at the online dashboard of bitbucket. Create a repo. You should probably create a project folder first, and then, create a new repo. This allows better organization as you begin working on multiple projects.

So yeah, you use the ‘create a new repository’ option and will be presented with the standard set of options.

·         Choose an owner – this is useful if you are part of a team. Choose a repo you own, or a repo that will become part of a team, and hence available to team members.

·         Choose project – the repo will become part of a project. Choose a project of your choice, or create a new project.

·         Repo name – the repo name of your choice. As always, try and use meaningful names.

·         Private or Public repo – self explanatory

·         Repo type – use Git. I don’t know much about the other option, which is mercurial.

Once you create a repo, the site should give you two options (and two links with actual steps). Use the first option, which is about mapping existing code files online. Here is a sample of what you might see.

  1. Step 1: Switch to your repository's directory

cd /path/to/your/repo

  1. Step 2: Connect your existing repository to Bitbucket

git remote add origin https://Jay_sn@bitbucket.org/thechalakas/testforblog.git

git push -u origin master

As you can see from the above commands, it is clear that these are the commands we must use on your work PC to get things moving. Now that this is ready in the web browser, it’s time to shift our focus to your PC.

Start off by getting the necessary tools from this link. https://www.git-scm.com/downloads

If you are feeling like a small baby, you can also download the GUI tools, which is okay.

Once you have installed the GIT SCM tools, you should have the git tools installed in your PC. Hopefully, you checked the box that says command line integration. If not, you better go back and do it. After the whole thing is setup, under applications, find ‘git bash’ and open it.

Now, use the step 1 and step 2 from above. Unless the solution is already initiated with the ‘repo’ stuff, the steps will not work and will give an error. To see if git is actually setup for your code files, I always use ‘git status’ command. If git is not setup, you will get this.

fatal: Not a git repository (or any of the parent directories): .git

So, you go ahead and initiate the whole git thing by using ‘git init’. This will do some stuff, and then, you should try ‘git status’ and you should get a different set of things. Mostly in red color. You see, the red color files and folders are those that are now detected by git. The red color means, the files are not being ‘tracked’ for changes. This is of course what we want because the whole point of version control or git is to track what changes you are doing to the code.

Right now I am going to assume that you wish to track everything in this code folder of yours. So, you will use the command ‘git add *’. This will add everything to be ‘tracked’. Now if you try, ‘git status’, boom, all the files will be in green. That’s the universal color for go, so we are good to go. Green also means, we are good to commit stuff, which is another way of saying, we are on the right direction to push things to the online repo.

Before we can push, we must commit the changes. For the now, the biggest change is the new file that has been detected. So, use the command ‘git commit’. This will bring up a ‘vi editor’ which will be familiar to you. If you are not familiar, then, perhaps you should not be practicing for a career in programming. Here, you will write some commit notes (by using a combination of Esc key, arrow keys, a key and i key, and constantly using Esc key, until you figure things out) and then save and quit the vi editor using ‘wq’.

Once you do wq, you are back in the command line, and the committing is done. Now, its time to push. As per the two steps from above, we should set the origin. So, lets do that by using a command that looks something like this.

git remote add origin https://Jay_sn@bitbucket.org/thechalakas/testforblog.git

Since this is the first time you are doing this, you will be prompted for a username and password. That will be your bitbucket login information. Once you do that, the command should work as expected, and then, you are back to the command line. Note that we are still setting things up, so the code is still not yet pushed to the online repo. Now, as per the two steps above, we need to actually push stuff.

 For that, we will use a command that looks something like this.

git push -u origin master

Now, you will get some internet upload type stuff. Depending on the number of files you have, this could take a while. If you are on a slow internet connection, this could take a serious amount. While you are waiting for this to finish, here are some things you can think about.

·         How productive is it to watch a movie while programming?

·         Is it better to work with 2 monitors or 3 monitors?

·         Will there ever be a sequel to Mad Max Fury Road?

·         If you two mice are connected to the same PC, and moved at the same time, which will be given priority while deciding cursor movement on the PC?

Okay, has the code been pushed online? Good. Now, go back to the bitbucket repo in the browser, and you will see all the code which was on your PC, a copy of it, is now online. This was the setting up part.

Now comes the actual usage part. On your PC, go ahead and make some changes to the code file. Or multiple files. Either way is fine. Once again, do ‘git status’. You will see the files you made changes to, in red color. As before, you need to add the file you wish to commit and push. I will simply do ‘git add *’ for I wish to add all files. Then, try ‘git status’ and make sure that the files you wish to push are in green. Now do the ‘git commit’, go through the whole vi editor save and quit thing, and then use ‘git push’. Once again, you should see the whole commit upload files thing happening.

Now, if you are using multiple PCs, it is possible that when you push stuff, you will get some unknown name on the repo. This is a high possibility. In which case, on the web bitbucket page, the name should be attached as an alias to your actual account. Do this and you will get your name (or whoever is doing the commit) in the alias connecting page. Of course, only admins can do it. If you created the repo, then you are obviously the admin. If you are working with someone else’s repo, then ask them to do it for you.

Alright then, good luck on your journey to become a better developer.

Follow me on twitter, facebook and instagram for more updates. Thanks!
Comments are closed