Mini Git Tutorial
Posted on Do, 2013-04-04 in other
Okay I decided to copy & paste some basic git commands and add some short descriptions, so that people who never used git before but have used svn can get a basic workflow running. (So I can tell people I work with: "read this to get started. If you need to know more: try other tutorials")
First Steps
So getting a repository over ssh is as easy as.
git clone git@example.com:repository.git
It's called clone, because git always fetches the whole repository and not just a single version like svn.
You need to set up you name and e-mail address for commiting stuff. You can change it per repository with this commands or in (./.git/config).
git config user.name "Firstname Lastname"
git config user.email "firstname.lastname@example.com"
For global configuration (~/.gitconfig) use:
git config --global ...
Basic Workflow
To get all new changes from repository:
git pull
Then you work on your files. The you are to commit those changes. To commit a file you need to explicitly mark it to be commited (git calls this staging).
git add $new_or_changed_files
To reset what you will commit (unstage in git language) use the following command.
git reset
Finally you will commit your changes.
git commit
To commit all changed files use the following command:
git commit -a
Commits in git are created locally, so nobody but you can access that commit. You need to push your commits back to the server:
git push
Interesting Commands
To view the status of the current files (staged, not staged, changed, etc.)
git status
To view the difference between the current files and the last commit in diff format use:
git diff
For viewing the last commits and their commit messages.
git log
Branching
Branches in git are awesome! Fast branching, merging, etc.
# create new local branch
git branch mynewbranch
git checkout mynewbranch
# make changes and commit new stuff
git add $newstuff
git commit
# push local branch to remote location
git push origin mynewbranch
Switching between branches is easy:
git checkout master
git checkout mynewbranch
Retrieve changes from another branch
You can get the changes of one commit in another branch by using the cherry-pick command. First you locate the commit hash (git commits are identifeid by their hashes) by usin git log for example.
git cherry-pick *commit_hash*
You can pull changes from another remote branch by using the git pull command:
git checkout mybranch
git pull origin master
If you work on a local feature branch for example, it's probably better you use the rebase command to apply your changes, i.e. all commits in your local branch on top of the commits of another branch. Think of it as branching from that other branch again, and adding all your commits on top of it.
git checkout mybranch
git rebase master
More Info
git man pages http://book.git-scm.com/ http://help.github.com/remotes/
Clients
I recommend to use the command line tools with *nix. gitk is nice to view the history.
Unfortunately I don't know much about Windows clients. Maybe I'll add someting sometime.
Errata
Check out this git cheat sheet: http://ndpsoftware.com/git-cheatsheet.html