Introduction to Git - 091 Labs
Git is an SCM system.
- Fast
- Efficient
- Easy to use
- Distributed
Not centralized like many other SCM systems - most
operations happen locally
Install
- yum or apt-get install git
- Others (YMMV):
http://git-scm.com/book/en/Getting-Started-Installing-Git
Make a repository.
cd my_cool_project
git init
makes it a git repo - creates .git
Some basic commands
- Check status of repo:
git status
- Track a file:
git add myfile
- Commit changes:
git commit
- See history: git
log
git add can take a file or a subdirectory
git add <file>
- Add a file to be 'tracked'
- 'Stage' changes to a tracked file
git add .
To stage changes means to add them to the index or
cache or 'staging area'
git commit
A commit is a record of changes to a repository
tracked/untracked files - states
- modified
- staged
- committed
- untracked
- ignored
See what state a file is in using 'git status' -
won't show ignored
Making changes
- Edit a file as normal, then:
git diff
git add <file>
git diff --cached
git commit
'--cached' shows diff between HEAD and index,
without it, we see modified files
Le commit
- A commit is an object in git that has a record of tracked
files and a message describing the changes made at that point. See git log
- SHA-1 hash (representing index at commit time)
- Committer details
- Comment
- Date
Changing anything will lead to different hash;
different commit
Commit message guidelines
Capitalized, short (50 chars or less) summary
More
detailed explanatory text, if necessary. Wrap it to about 72
characters or so. Leave blank line between summary (first line) &
body.
Body can be many detailed paragraphs long. Bullet
points are also
acceptable, using either - or *. Also, don't
end summary with a
period.
Part two
Working with branches.
One of the Git's killer features. Easy and
efficient.
Some terms & commands
- master - default branch.
- HEAD - pointer to the tip of current branch
- List local branches:
git branch
- List remote branches:
git branch -r
Why branch
- Create disposable 'topic' branches for focused units of work
- Create long-term branches for various workflows
- Have different branches to track different remote
repositories
Bad ideas; Fedora package collection;
github/gitorious
Create a branch and 'check it out'
Work just like before, only changes here won't be
reflected in existing branch
Merging branches
- Add changes from one branch to another branch where they
haven't been applied yet.
- A few types. Most common:
- fast-forward - move tip of current branch forward
- true merge - results in an extra commit (2 parents)
Examples
- ff: branch, add new commit to one, merge into the other
- true: branch, add different commits to both sides, merge one
into other
Merge Conflicts
Merge conflicts usually happen when 2 branches are being merged
which have overlapping changes
Options when conflicts happen
git merge --abort
- Go back to before you called
merge
git mergetool
- graphical tool to try to help you
solve differences
- Solve conflicts by hand,
git add each resolved file, then git commit
'Rebase'
A rebase is like a merge, except it tries to keep things cleaner
so the history is more linear.
'Rebase'
git rebase <branch>
- Put new commits on current branch on top of new commits on
<branch>
Conflicts/problems during a rebase
git merge --abort
git rebase --abort
THIS IS IMPORTANT
Don't use rebase if you've already pushed commits that it will
change.
Part three
Working with remotes.
Git on the internet.
Some free Git repository hosting
- gitorious.org
- repo.or.cz
- github.com
- bitbucket.com
- sourceforge.net
- code.google.com
Putting stuff into a remote repo: pushing
git remote add <remote-nick> <remote-ssh-address>
git remote add origin git@github.com:grdryn/example.git
git push -u origin master
Getting stuff from a remote repo
- In an existing repository (that has a remote):
git fetch
git pull
git pull origin master
- Grab an entire repository that you don't have:
git clone <repo-url>
Advantages of remote repositories
- Collaborate with others
- Backup your project
- A place for people to download your project
- Find cool projects by others
Other useful commands to try
Places to get help/tips
- http://git-scm.com
- http://www.ndpsoftware.com/git-cheatsheet.html
- https://help.github.com/
- http://gitguys.com
- http://coderwall.com/n/git
- http://stackoverflow.com/questions/tagged/git