Basics to get started
1. Setup
For Mac, brew install git
.
For Linux, sudo apt-get install git
(you can try sudo apt-get install git-core
if Git is not installed) or yum install git
.
For Windows, download the bash client here and install it, and you get bash commands for free, happy days!
Even though I tend to avoid GUIs cause they are confusing, inefficient and most importantly not hipster, you can still find something from GitHub or Tower.
2. Create a repository
Easy peasy.
Copy a remote repository
You can copy either from a server or most likely GitHub, you can have a quick scan through this to decide which remote URL to use.
I personally use SSH because I used to work behind a corporate proxy.
Stage changes
Before commit, you need to group your changes and add them into stage.
To add files one by one.
Or don’t care, just add them all.
Some really cool stuff you should try with stage.
Stash changes
Not ready to commit, or before re-synchronising your code with remote and don’t want to lose your local changes? Stash them.
It does what it says, to keep your things away from destruction.
You can always find the things you stashed here.
And applying last change set back to codebase.
Or applying a specified change set.
Unless you want to both apply a change set and keep the stash in the stack.
Commit changes
Happy with the changes? Commit them and give them a meaningful message. And I seriously mean it!
Commit logs
Like showing commit history in any version control system, Git is just more powerful in every single way.
To see the commits from a specific user.
To show the files changed.
To view a compact one liner commit history.
Even more crazy things you can do, like displaying an ASCII art tree commits decorated with tags and branches.
Push changes
So far it is all great, but only in your machine. If you want to be colleborative, push your commits to the central repository so that your colleagues can criticise them.
In case you haven’t setup a remote repository for your current branch yet, hook it up with one.
Then do the push again.
Branch
It’s important to get codebase organised using dedicated branches for stuff like new feature, bugfix or release candidate. This has been proven to be a very successful branching model for the majority.
Branching in Git is fast. Faster than subversion.
To list all the branches.
To switch to a branch and start working.
This is a short hand for creating a branch, switching to it, and bring all unstaged changes with you.
Want to share your branch?
-u
is to establish a tracking relationship between a branch and its remote branch
Update
It is a good idea to get a cup of tea in the morning, and update your code!
Merge and Rebase
And because you haven’t done what I suggested above often enough, you are more than likely to end up with conflicts after a short while.
Keep calm and use merge, and Git will automatically integrate the changes for you.
And this is still faster than subversion, yet another win for Git.
Alternatively, use rebase to fast-forward your branch.
Git rebase works like a transplant
Be aware that rebase will replace current branch’s commit history with the other. And that is why a timely update is so important to avoid situation like this.
A little case study just to see if you are still awake: how to synchronise a fork with the source of truth.
First add the source repository as an upstream.
Then fetch all its content.
Switch to master branch, and rebase the master with upstream.
If you just want the changes, but don’t necessarily need the change history, merge is another option. I would not recommend this as it could cause more pain later.
Push the new codebase to your fork.
Tagging
Tag a specific commit for release.
The power of undoing
Use with caution.
Undo changes
Before changes are staged, you can revert a single file.
Or revert all of them.
Undo stage
What happens if you put the wrong file into stage, or don’t want it to be part of the commit?
Undo commits
Revert a commit by creating a new commit to rollback last commit.
Rollback to a specific commit, and reserve changes.
Abandon ship.
Reset is similar to revert under the hood, the removed info is still kept in git database for 30 days. So you can always unearth them if you want.
Rewrite last commit message
Wrote something inappropriate? Rewrite it. Limited to last commit only.
Delete a branch
Remove a branch locally.
And remotely.
It’s all about Flexibility
Git is highly configurable in itself.
And indeed whatever is listed here and more, you can adjust it based on your own preferences.
If you have an extremely small terminal, typically less than 72 characters wide, your display will be truncated like following (left) before 2.1.0.
Git log displayed with truncati (left) and normal (right)
You don’t have to wait for the 2.1.0 to able to display a wrapped log like the example to the right. Even though git log
uses less -S
by default, it doesn’t mean you can’t make it more user friendly (as to the right):
Some things to bear in mind
Git is a powerful tool, but it doesn’t prevent you from shooting your own foot if you don’t work under some basic principles. In order to make everyone’s life easier, it is useful to have some common sense when working with Git.
Commit only related changes
Write good commit messages
Use branches extensively
Never commit half-done work
Never amend published commits