Ok, after seeing this post by PJ Hyett, I have decided to skip to the end and go with Git.
So what I need is a beginner's practical guide to Git. "Beginner" being defined as someone who knows how to handle their compiler, understands to some level what a Makefile is, and has touched source control without understanding it very well.
"Practical" being defined as this person doesn't want to get into great detail regarding what Git is doing in the background, and doesn't even care (or know) that it's distributed. Your answers might hint at the possibilities, but try to aim for the beginner that wants to keep a 'main' repository on a 'server' which is backed up and secure, and treat their local repository as merely a 'client' resource.
So:
Installation/Setup
- How to install Git
- How do you set up Git? Try to cover Linux, Windows, Mac, think 'client/server' mindset.
- How do you create a new project/repository?
- How do you configure it to ignore files (.obj, .user, etc) that are not really part of the codebase?
Working with the code
- How do you get the latest code?
- How do you check out code?
- How do you commit changes?
- How do you see what's uncommitted, or the status of your current codebase?
- How do you destroy unwanted commits?
- How do you compare two revisions of a file, or your current file and a previous revision?
- How do you see the history of revisions to a file?
- How do you handle binary files (visio docs, for instance, or compiler environments)?
- How do you merge files changed at the "same time"?
- How do you undo (revert or reset) a commit?
Tagging, branching, releases, baselines
- How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?
- How do you pull a particular 'release'?
- How do you branch?
- How do you merge branches?
- How do you resolve conflicts and complete the merge?
- How do you merge parts of one branch into another branch?
- What is rebasing?
- How do I track remote branches?
- How can I create a branch on a remote repository?
- How do I delete a branch on a remote repository?
- Git workflow examples
Other
- Describe and link to a good GUI, IDE plugin, etc. that makes Git a non-command line resource, but please list its limitations as well as its good.
- msysgit - Cross platform, included with Git
- gitk - Cross platform history viewer, included with Git
- gitnub - Mac OS X
- gitx - Mac OS X history viewer
- smartgit - Cross platform, commercial, beta
- tig - console GUI for Linux
- qgit - GUI for Windows, Linux
- Git Extensions - package for Windows, includes friendly GUI
- Any other common tasks a beginner should know?
- How do I work effectively with a subversion repository set as my source control source?
Other Git beginner's references
- Git guide
- Git book
- Git magic
- gitcasts
- GitHub guides
- Git tutorial
- Progit - book by Scott Chacon
- Git - SVN Crash Course
- Git from the bottom up
- Git ready
- gitref.org
- Git visual cheatsheet
Delving into Git
I will go through the entries from time to time and 'tidy' them up so they have a consistent look/feel and it's easy to scan the list - feel free to follow a simple "header - brief explanation - list of instructions - gotchas and extra info" template. I'll also link to the entries from the bullet list above so it's easy to find them later.
How to install Git
On Windows:
Install msysgit
There are several downloads:
This also installs a Cygwin bash shell, so you can use the
git
in a nicer shell (than cmd.exe), and also includes git-gui (accessible viagit gui
command, or theStart > All Programs > Git
menu)Mac OS X
Use the git-osx-installer, or you can also install from source
Via a package manager
Install
git
using your native package manager. For example, on Debian (or Ubuntu):Or on Mac OS X, via MacPorts:
…or fink:
…or Homebrew:
On Red Hat based distributions, such as Fedora:
In Cygwin the Git package can be found under the "devel" section
From source (Mac OS X/Linux/BSD/etc.)
In Mac OS X, if you have the Developer Tools installed, you can compile Git from source very easily. Download the latest version of Git as a
.tar.bz
or.tar.gz
from http://git-scm.com/, and extract it (double click in Finder)On Linux/BSD/etc. it should be much the same. For example, in Debian (and Ubuntu), you need to install the
build-essential
package viaapt
.Then in a Terminal,
cd
to where you extracted the files (Runningcd ~/Downloads/git*/
should work), and then run..This will install Git into the default place (
/usr/local
- sogit
will be in/usr/local/bin/git
)It will prompt you to enter your password (for
sudo
), this is so it can write to the/usr/local/
directory, which can only be accessed by the "root" user so sudo is required!If you with to install it somewhere separate (so Git's files aren't mixed in with other tools), use
--prefix
with the configure command:This will install the
git
binary into/usr/local/bin/gitpath/bin/git
- so you don't have to type that every time you, you should add into your$PATH
by adding the following line into your~/.profile
:If you do not have sudo access, you can use
--prefix=/Users/myusername/bin
and install into your home directory. Remember to add~/bin/
to$PATH
The script x-git-update-to-latest-version automates a lot of this:
How do you set up a shared team repository?
How to set up a normal repository is described here -- but how do you set up a team repository that everybody can pull and push from and to?
Using a shared NFS file system
Assuming your team already has for instance a shared group membership that can be used.
To start using this repository the easiest thing to do is start from a local repository you already have been using:
Others can now clone this and start working:
Using SSH
Set up a user account on the target server. Whether you use an account with no password, an account with a password, or use
authorized_keys
really depend on your required level of security. Take a look at Configuring Git over SSH for some more information.If all developers use the same account for accessing this shared repository, you do not need to use the
--shared
option as above.After initing the repository in the same way as above, you do the initial push like this:
See the similarity with the above? The only thing that might happen in addition is SSH asking for a password if the account has a password. If you get this prompt on an account without a password the SSH server probably has disabled
PermitEmptyPasswords
.Cloning now looks like this:
Commit Changes
Once you've edited a file, you need to commit your changes to git. When you execute this command it will ask for a commit message - which is just a simple bit of text that tells everyone what you've changed.
Will commit the file main.c in the directory ./source/
will commit all changed files (but not new files, those need to be added to the index with git-add). If you want to commit only certain files then you will need to stage them first with git-add and then commit without the -a flag.
Commiting only changes your local repository though not the remote repositories. If you want to send the commits to the remote repository then you will need to do a push.
For someone coming from CVS or SVN this is a change since the commit to the central repository now requires two steps.
A real good paper for understanding how Git works is The Git Parable. Very recommended!
How do I delete a branch on a remote repository?
Perform a push in your remote using
:
before the name of the branchbeing
origin
the name of your remote andmybranchname
the name of the branch about to be deletedhttp://help.github.com/remotes/
Workflow example with GIT.
Git is extremely flexible and adapts good to any workflow, but not enforcing a particular workflow might have the negative effect of making it hard to understand what you can do with git beyond the linear "backup" workflow, and how useful branching can be for example.
This blog post explains nicely a very simple but effective workflow that is really easy to setup using git.
quoting from the blog post: We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state:
The workflow has become popular enough to have made a project that implements this workflow: git-flow
Nice illustration of a simple workflow, where you make all your changes in develop, and only push to master when the code is in a production state:
Now let's say you want to work on a new feature, or on refactoring a module. You could create a new branch, what we could call a "feature" branch, something that will take some time and might break some code. Once your feature is "stable enough" and want to move it "closer" to production, you merge your feature branch into develop. When all the bugs are sorted out after the merge and your code passes all tests rock solid, you push your changes into master.
During all this process, you find a terrible security bug, that has to be fixed right away. You could have a branch called hotfixes, that make changes that are pushed quicker back into production than the normal "develop" branch.
Here you have an illustration of how this feature/hotfix/develop/production workflow might look like (well explained in the blog post, and I repeat, the blog post explains the whole process in a lot more detail and a lot better than I do.