Let’s talk about Git. If you are writing software in any way and not yet using it, you should start now. I was quite a novice with Git for a while, and I need to give credit where it’s due. Thanks to my co-working Nick Whiting I have learned so much about it just by watching him.
I’m not a git meistro and I prefer the command line much more over the GUI. Anyways, let me break down a few concepts that were difficult for me to grasp at first so you can get on this gravy train.
I’m going to assume you already have some type of Git shell on your computer. I will also assume you already know that you can type
$ git init to create an empty repository. Furthermore, I must assume you know how to connect to your git service. This is about Git commands.
This is just a fancy name for pretty much a dedicated folder to your project. You shold notice that all git repositories have a
.git folder that’s usually hidden. Whenever you upload files through FTP, you never want to include that folder as it keeps track of local history and can get insanely large.
When you commit your code it doesn’t mean it goes to your git server. Committing means you’ve added all the changes and files you wanted to, and you are ready to commit those changes right before you push them (or merge).
First you need to make sure there is something to commit. If you haven’t changed any files, you’ll need to in order to get a git status going:’
$ git status
$ git add . $ git add -u $ git commit -m "A brief description of files updated.."
Anytime you hear push, that means you have committed data that is going to be pushed to the server. It’s tricky to get all the lingo down at first but you’ll get it once you type it over and over. Suppose I type
$ git status and it shows me that I have code I can push. I would do it this way:
$ git push origin master
If I was on another branch (which you’ll see below) you would just change the name of the branch like so:
$ git push origin dev
Lets say you have a project for a client going well and running smoothly. And you want to refactor a lot of code. It would be a bad idea to refactor all the main code that’s stable. So, you would want a separate section dedicated to that code. This is what a branch is.
An example would be, you master branch is where all the live code goes. And your dev branch is where you are doing your refactoring. That’s a great way to do things.
$ git branch dev $ git checkout dev
And, so you aren’t lost here’s how you go back to master:
$ git checkout master
Also, how you can delete a branch. Please be careful with this one!
$ git push origin --delete dev
Have you ever backup up your computer to a certain state? If you have a tag is the same thing. It’s a snapshot of your current code base nicely zipped up for you so you can keep track of versions.
$ git tag 0.1 $ git push --tags
What’s strange is that deleting a tag is a lot different, here’s the syntax:
$ git tag -d 0.1 $ git push origin :refs/tags/0.1
Git Unadding Files
Have you ever just done a
$ git add . and realized there are files you don’t want in there? Well you can simply unadd a file by doing this:
$ git reset HEAD file.txt
You might be asking yourself what does HEAD mean? It is the latest commit to that given file you made. So if you updated that file 5 times in the past, by restting the head you would get the newest revision.
Many times there will be files you never want to add to Git. Such as Caching files and file uploads as they will just waste space. Git ignore will allow you to ignore certain files, to do so create a file in your root directory:
$ touch .gitignore
Within the .gitignore file, lets pretend you wanted to ignore all jpg files and a folder;s c called upload. Inside .gitignore you would add:
What’s more is that you can put .gitignore within subfolders and apply the same rules if you choose. Just make sure to put one rule per line, and as always * is a wildcard!
Sometimes you may find that your .gitignore is not working, this is likely due to your local cache. To fix that run these commands:
$ git rm -r --cached $ git add . $ git commit -m "Fixing my ignore files" $ git push origin master
The first line in the code above recursively removed the cached files, hence the -r. So then you must re-add your local files and re-commit them and push them to the server.
I used to use SVN but I love Git much more. One thing I haven’t covered it merge conflicts and stashing. But there are other resources ready for you when you get into more complexities. For now checkout BitBucket and get free public/private repositories, all unlimited.