Use git for version control
The following is a very brief introduction. Codecademy just launched a new course on Git, the industry standard for version control. Following the link to learn in on Codecademy
Installing Git on Mac OS
On Mac OS X, there are two ways to install git.
- Install xcode in the app store, then run
xcode-select --install
in terminal to install command line tools. In the future, update will show up in app store. In this way, you will be using the git curated by Apple. - Install Homebrew, then run
brew install git
in terminal. Homebrew is a nice package manager for OS X.
Configuring Git
A couple of very basic configurations should be made before you get started. For example, set your name and email address as follows:
$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"
Create a local repository
You can use the following commands to creat an empty local repository:
$ cd path/to/project/folder
$ git init
Now look at the files in that directory (including any hidden files):
$ ls -la
You’ll see that a new, hidden folder .git
was added.
Clone a remote repository
You can clone a remote repository on a server by using git clone
command, for example:
$ cd your/development/folder/
$ git clone https://github.com/gittower/git-crash-course.git
By doing this, Git will now download a complete copy of this repository to your local disk - under the condition that you’re allowed to access this repository.
Working on a project
Staging area
In Git, simply making some changes doesn’t mean they’re automatically committed to the local repository. The staging area allows you to determine which of your local changes shall be committed.
You can stage some changes with the git add
command:
$ git add new-page.html index.html css/*
With this command, we added the new “new-page.html” file, the modifications in “index.html”, and all the changes in the “css” folder to the Staging Area.
If you want to record the removal of some file in the next commit, you may use
$ git rm <filename>
You can also see what you’ve changed since your last commit using:
$ git status
Committing changes
Now you are ready to commit changes to local repository. The following command wraps up your changes:
$ git commit -m "First commit"
the commit message after “-m” is usually a short summary of your changes (up to 50 characters as a guideline). It will make it easier to understand what happened for you, as well as your teammates.
The following command is used to display the project’s commit history:
$ git log
Branching and Merging
You are always working on a certain branch (the currently active, or “checked out”, or “HEAD” branch). “git status” will show you which branch is HEAD in its first line. The default is “master”.
You can create a new branch using
$ git branch newfeature
Now there exists two branches, namely “master” and “newfeature”. You may switch to the new branch using:
$ git checkout newfeature
Running git status
, you’ll see the HEAD is now “newfeature”. Now you can work on this new branch, with
any other branches unaffected.
You can merge the changes from the new branch into “master” using:
$ git checkout master
$ git merge newfeature
Share work via remote repository
Suppose you are connected to a remote repository “origin”. You may integrate the remote changes into local repository using:
$ git fetch origin
or directly integrates them into your working copy using
$ git pull
It’s actually a “fetch” command (which only downloads data) and a “merge” command (which integrates this data into your working copy) combined.
In case no tracking connection was established for your local HEAD branch, you will have to tell Git from which remote repository and which remote branch you want to pull (“git pull origin master”, e.g.)
The following command uploads all the new commits from our current HEAD branch to its remote counterpart branch:
$ git push