Source Control Management With Git/Overview
From Wikibooks, the open-content textbooks collection
This chapter is devoted to a quick-and-dirty tutorial. It is aimed at those that want to get a quick run down of how git works. The real beauty of git lies in its multi-user functionality, but that is omitted here for brevity.
Contents |
[edit] Creating a git repository
Creating a new git repository is simple. There are two commands that cover this functionality: git-init, and git-clone, because this is a single-developer overview git-clone is reserved for a later section. In the directory you want to track run git-init. Or, if your playing around create a new directory.
mkdir mygit cd mygit git init
Git should immediately come back and tell you:
Initialized empty Git repository in .git/
The totality of your repository will be self-contained within the .git directory. Conversely, some SCMs leave droppings all over your working directory (ex, .svn, .cvs, .attic, etc), git refrains, and puts all things in a subdirectory of the repository root aptly named .git.
[edit] Adding Files
To add files use the command git-add.
git add myFile git commit
To remove files, use the "rm" subcommand of git.
git rm myFile
git commit
The second variant only deletes the file from the cache, leaving the physical file in place.
To add a directory use git add ., and not git add *, the latter will die if any files violate your gitignore file.
[edit] Branching
Branching is the core concept of most distributed version control systems, and git is no exception. For that reason there are several commands for dealing with branches. Many of these commands have more complex and powerful alternatives, they are again left out for brevity. Full details of all of these commands and their optional arguments can be found in your supplied man pages.
[edit] Creating
To create a new branch, use the git-branch command with the given name. This only creates the new branch, it leaves your current HEAD where you remain.
git branch myBranch
This command will create a new branch and attach your HEAD to that branch.
git checkout -b myBranch
[edit] Changing current branch
To checkout a new branch simply use git-checkout and then the branch name.
git checkout myBranch
[edit] Listing branches
To list what branches you have use a naked git-branch.
git branch
[edit] Deleting
To delete the current branch, again use git-branch, but this time send the -d argument.
git branch -d myBranch
[edit] Merging
Branching is the central concept to DSCM, but without good merging support, branches would be of little use. Without pointing fingers, this is an idea that certain other source control systems got wrong.
git merge myBranch
This command merges the given branch into the current branch. If the current branch is a direct ancestor of the given branch, a fast-forward merge occurs, and the current branch head is redirected to point at the new branch. In other cases, a merge" commit is recorded that has both the previous commit and the given branch tip as parents. If there are any conflicts during the merge, it will be necessary to resolve them by hand before the merge commit is recorded.