Git for noobs, what other how-to’s assume you know
Do you ever lose bits and pieces of your source code? End up with bits on 3 computers, 8 USB drives, Dropbox and Google Drive?
For me that is what programming in Python is like. I make some regular backups to my Google Drive, but I'm never quite sure which copy is the newest,so I edit over old files, just to find I have corrected some bugs before, ending up wasting valuable time.
This is not a sales pitch, but GIT is not only for collaborative coding, you can
create a repository with your project and have sole ownership of it.
I did not want to embrace GIT as I didn't want others to have access to my
source code until I feel it is a usable project.
Then I found Atlassian BitBucket, where you can create unlimited repositories
for your code. If you work on various small projects, mostly for yourself, then
hosting sites such as Sourceforge, GNU Savannah and CodePlex might not be
for you, although you can still make use of them. I wanted a place where I can
have a private repository that I can change to public later on, and maybe give
permission to one other person to my private repo...
Wel, Bitbucket allows you unlimited repo's (one for every small project I'm
working on) under one username, and you are allowed to add up to 5 users
(you and 4 others) to your private repo's.
What is git
git is "the stupid content tracker". Basically a stupidly easy place where you can store your code and anyone with access can download, edit and upload parts of it. This anyone can be restricted to yourself on multiple PC's. As always Wikipedia gives a great definition, but the part that is important to you is:
All of your projects will be sorted in a neat fashion
You have anytime, anywhere access to your code
You can edit your code and upload it when done, never lose your code again
A friend can make changes and request you to allow it first (pull request)
Great history and diff support
One of the important points for me is the diff support. Why would you need diff support?
Well, lets say you have accidentally written over one of your important files... eish, yes, that does happen. Now you still have the correct file on BitBucket, but you do not notice your error and commit the changes to BitBucket... All is not lost, just go to your repo, choose the affected file, view the diff (side-by-side mode), and there you have your old file back.
Now there ought to be a better method, but I'm still new to BitBucket, so I will find it in time.
How do you use it?
Using git can be a daunting task if you have a look at the documentation. Well, for me it was until I started. WOW it's so easy!!
First things first, set up your repository:
Here you can use your own local repo on a home server or your own PC even, but I will not go into that now, I want to look at web services. You can use GitHub
, or any of these.
I’m using and loving BitBucket, but haven’t tried the rest, so any will do.
Login to BitBucket with your OpenID
or create a new login. Create a new Repo, answer the questions the ask, and, well, that’s it for setting up the repo! Really!
Next, install git:
Ubuntu users should be able to
$ sudo apt-get install git
while those on Windows have to download Git or PortableGit from msysgit
Install and follow the steps.
Setup your local repo:
Your local repo is a local folder where the source code will be downloaded to. You will edit files in this folder and push back to the server. More on that later.
Here is some settings to personalize your Git. Note, these are commands within Git Bash for Windows. Linux commands should be roughly the same. You are welcome to use the Git Gui for Windows, but try Bash, it's fun.
$ git config --global user.name "Your Name"
$ git config --global user.email "Your_Name@gmail.com"
If you are working behind a proxy, add this as well:
$ git config --global http.proxy PROXY.ADDR.ESS:PORT
or for authenticated proxy:
$ git config --global http.proxy USERN:PASSW@PROXY.ADDR.ESS:PORT
Lastly, make a directory for all of your projects and clone your first project from BitBucket. Yes, it might be empty if you are just starting out, but it will help set things up.
$ mkdir ~/projects
$ cd ~/projects
$ git clone https://DigiGram@bitbucket.org/DigiGram/project.git
You will get the path to your .git file on your Project's Overview page.
Working with git:
First you need to understand how the git structure work. For me it was a total hit and miss, issuing commands without knowing what they do until something stick. Well, this image from Wikipedia actually helps you a lot. You will see why in a while.
The remote repo is on BitBucket and the Working directory is the one on your hard drive that you can edit. Please note, *only* the Remote repo is not on your computer. To get a file from the Remote repo, you pull, to add your files to it, you push, but in between is some extra steps.
For now, lets only add files to the repo.
to add everything in the directory to the index. Once a file is added, you do not need to add it again, even after you have changed it.
Now you can make some changes to your files.
commit these changes to your local repo. The -a tag will commit ALL files that has changed, not just those you specify.
$ git commit -a -m "Give it a good reason"
A good reason is important if you work with somebody else, so they know what you did to the code. Maybe something like "fixed the Search page bug". It can even be helpful if you work alone, so start learning a good habit.
Lastly you can push your changes to BitBucket. Here you will need the repo password.
$ git push
You have created a remote repository, and you have added files to it. To get files from your repo on another computer, first clone the repo as above, and afterwards just do
$ git pull
More on this to follow later on.
For more info, scoure the web, or look at this awesome tutorial from BitBucket