Podcast: Play in new window | Download
A brief podcast on how to get started with this extremely popular source code manager.
What is Git?
Git is an open source distributed version control system. It is fast, and small making it an extremely popular subversioning software SCM (Source Code Manager). Some of its competitors are Subversion (SVN), CVS, Perforce, and Clear Case.
you use it for tracking updates and versions of files and text documents. You can track all types of files, but it is most useful for Software source code, weapplications.
Lets get started
– Git can be a little tough
– Understand it best through the command line
– A good resource is Git Essential Training with Kevin Skoglund
– installing git tutorials from git-scm.com or github.com
Preview of Git Essential Training on Lynda.com
Get 10 days of free unlimited access to lynda.com.
Git on Mac
– install apples command line tools.
– access it with Terminal
Git on Windows
– In the main Git web site, it’s going to be at http://git-scm.com.
– download the exe. walk you through the steps. stick with the defaults
– Git Bash (Bash is unix environment – very similar to environment that unix users look at. Git Bash has the same commands that Unix users are used to. This is how you can control Git in a command line environment
Now that we have looked at how to get Git and install it let’s get up and running with it. I would like to point out that you can set configurations and settings for your installation, but I’m not going to go into that. At this point you can use Git and This tutorial is more about getting your feet wet and using it. I feel that if you can get used to using it, it will make learning the intricacies of a program much easier. For this tutorial I’ll focus primarily on the basics and let you know where you can go to dig deeper.
The basic work flow of using Git is to navigate to the folder you want track with git, initialize it, add files to your staging area, then commit those changes. As you make changes to files you’ll commit those changes to your project. You can comment on those commits making it easy to keep track your updates. If you work as part of a team – members can check out files, then commit them back to the master repository. As you are developing you can revert back to any previous version of the project. To do this you would check out a previous version, make your changes, then merge them or keep them separated as a branch of that project. Branches allow you to maintain completely independent versions of the project; this way you can test or work on potential ideas for the project without affecting the main branch of project – this encourages experiementation; create a branch, experienment and if it isn’t going to work, just abondin it – it isn’t going to hurt your master, but if it does, just merge the changes into your master and delete the master.
Git is great because it allows you develop in one folder. All your ideas, experiments, testing and team involvement can all be done in one folder. Git is part of your working directory. As you check out and develop different versions of your project, Git will add or remove files that are associated with that version in real time – when you first get started it might seem like magic. I know when I started working with branches it seems crazy to see files drop in or out of my working directory – one second they were there and the next they weren’t. It makes testing things out so easy. I will say that working with Git takes some getting used to.
So a quick tip – commit often. As you are working it might seem best to wait until a feature is completely finished before you stage and make a commit – but by committing often you can create more revisions, which offer you more points to revert back to.
Say you are working on a new website feature – you think it is good and ready for release to your live server. You commit the change, then launch it to your live server. It works, but it has broken something else – you overlooked something. You can restore the previous version, but you will have to completely remove all of your work. If you had made multiple commits it could make tracking down bug easier without needing to start from scratch.
Go lets talk commands. Remember these are for using Git through Git Bash or through Terminal.
To start a git repository navigate to the directory your project is at.
Navigate using cd then the path to the folder. CD stands for change directory
Once in the directory type.
Another way to start a repository is to download one from a remote repository like Github. in that case you would use
Git clone url
To see the status of your working directory at any time you can type
it will let you know if you have any uncommitted changes – if there are Git Status will let you know of what files have been updated
To stage a file use the
Git add file
Git add .
Git commit -m "comment"
Allows us to commit the staged items and marks the commit with the comment “comment”
Git commit -am "comment"
Allows us to both add all the files to the stage and commit them with the comment “comment”
Untracked Unmodified modified staged commit.
Git stage = in working directory but not committed
Git checkout sha -- filename
Git revert sha
This is a new commit that reverts a commit. This is a committed revert
Git revert -n sha
This will just stage the revision and then you will have to actually need to commit.
Git reset sha
Changes the head pointer.
Shows difference between last commit and working directory
Git diff sha
Show diff between working directory and that sha
Git diff sha..sha2
Shows diff between 2 different commits
Shows all available branches. Master is the default branch
Git branch newBranch
Would make a new branch called newBranch
Git checkout newBranch
Will allow us to checkout our new branch
Git diff master..newBranch
Will show you the differences between branches
To incorporate the changes of newBranch into the master branch checkout the master branch then use the merge command
Git checkout master
Git merge newBranch
Now the additions from newBranch are part of master
At this point you can delete the newBranch branch if you want to
Git branch -d newBranch
This will delete your newBranch branch
When you merge branches conflicts can occur. Those conflicts usually happen when there are two changes in one file that are on the same line.
You will have to resolve the conflict before you can merge
Everything up to now has been for local use of Git. If you plan on collaboration or sharing you can set up a remote repository on github. This allows you to share your projects and collaborate with a team. GitHub is the most popular remote Git repository. They offer free accounts for public repos and paid for private.
To push to a remote repository you will need to register and set up a github account. I’m not going to go into that process since it should be easy to just goto github.com and setup the account
To start a remote repository github will give you the startup script which will give us a script to create a local repository or the connect script for a repo that already exists
The script should look something like
Git remote add
The alias for your remote usually is origin
Once you run this script your local repo will be connected. You can view your remote repos by running
To sync your local repo to the remote use
Git push -u
Git push -u origin master
The -u sets the repo to track the remote repo
To pull down a remote repo to use locally
To push any additional changes from your local repo to the remote just use
Git push origin master
This works because it is tracking
To grab changes from a remote repo use
Git fetch origin
Or if you only have one remote you can use
Now fetching doesn’t actually add the changes to our local branches
Fetch before you work
Fetch before you push
To view all branches including local and remote
Git branch -a
To put fetched repo changes into your local repo you’ll need to goto the branch you want to merge into
Git checkout master
Then merge in the branch you want to merge
Git merge origin/master
This is merging the remote branch with the local branch