|
Table of Contents
|
Setup
Setup Name and Email
The first time you need to set up git to work properly.
Set up your name and email address for Git to use to sign your commits.
$ git config --global user.name "Your Name"
$ git config --global user.email "your_email@email.com"
This wil set up file ~/.gitconfig
Setup Line Ending Preferences
- Unix/Mac users:
git config --global core.autocrlf input
git config --global core.safecrlf true
- Windows users:
git config --global core.autocrlf true
git config --global core.safecrlf true
New Project
Initializing a new git repository
git init
Add files to repository
git add file
If you do a git status again you will see:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: hello.cpp
#
- The changes to the hello.cpp has been staged. This means that git knows about the change, but the change hasn't been permanently recorded in the repository yet.
- The next commit will include the staged changes.
- If you decide you don't want to commit that changed after all, git reset can be used to unstage that change.
Staging and Committing
- A separate staging step is useful to make changes without worrying about source control.
- When you want to interact with source control, git allows you to record your changes in small commits that record exactly what you did.
Example
Suppose you edited three files (a.cpp, b.cpp, and c.cpp). Now you want to commit all the changes, but you want the changes in a.cpp and b.cpp to be a single commit, while the changes to c.cpp are not logically related to the first two files and should be a separate commit.
You could do the following:
git add a.cpp
git add b.cpp
git commit -m "Changes for a and b"
git add c.cpp
git commit -m "Unrelated change to c"
By separating staging and committing, you have the ability to easily fine tune what goes into each commit.
Cloning a Repository
To clone a repository
git clone git://git.kernel.org/pub/scm/git/git.git
Making Changes
To add some files use:
git add file1 file2 file3
Use diff to see what is about to be committed:
git diff --cached
Check the status of the repository
git status
You should see
$ git status
# On branch master
nothing to commit (working directory clean)
The status command reports that there is nothing to commit. This means that the repository has all the current state of the working directory.
Or if you changed something you will see
$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: hello.cpp
#
no changes added to commit (use "git add" and/or "git commit -a")
Git knows that hello.cpp file has been modified, but git has not yet been notified of these changes.
Also the status message gives you hints about what you need to do next. If you want to add these changes to the repository, then use the git add command. Otherwise the git checkout command can be used to discard the changes.
Commit changes:
git commit
Alternatively, instead of running git add beforehand, you can use
$ git commit -a
Commit messages are not required but it's a good idea to summarize your change.
Log
To view the changed history for a specific file:
git log -p filename
To view changes in the one line format:
git log --pretty=oneline
To control which entries are displayed try the following:
git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<your name>
git log --pretty=oneline --all
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
Type man git-log for more.
Reset
Undo published commits with new commits
git revert 0405dd16bfcdb0aba355148318765c6da2f61f50
gitk - The git repository browser
To see all changes:
gitk filename
Git workflow for single developer
The workflow is the following:
IDE > local git repository > remote bare git repository > git repository (live).
First set up the local repository:
cd projects/example
git init
//create or copy files
git add .
git commit -m "first commit"
Next, set up the bare repository on the server:
cd ~
mkdir git/example.git
git --bare init
Next, tell your local repository about this remote repository. Run the following locally:
git remote add exampleserver ssh://exampleuser@example.com/~/git/example.git
To push the data to the server run locally:
git push exampleserver master
Then create the repository which will contain the live application. In the live server run:
mkdir ~/www/example.com
cd www/example.com
git init
git remote add bare ~/git/example.git
The last step is the pull operation. In directory of the live application run:
git pull bare master
So, to deploy new changes do the following (locally):
//let's say you changed file.c
git add file.c
git commit -m "message"
git push exampleserver master
To put the changes on the live directory run:
git pull bare master
Theory
Repository Contents
- The purpose of Git is to manage a project. Git stores this information in a data structure called a repository.
- A git repository contains
- A set of commit objects.
- A set of references to commit objects, the heads.
- The Git repository is stored in the same directory as the project itself, in a subdirectory called .git.
Commit Objects
A commit object contains three things:
- A set of files, reflecting the state of a project at a given point in time.
- References to parent commit objects.
- A unique name.
Heads
- A head is simply a reference to a commit object. Each head has a name.
- By default, there is a head in every repository called master.
Working with github
First, generate ssh keys between your Linux box and github. (tutorial)
Then run the command
git clone git@github.com:username/project.git
Basic Branching and Merging
A single git repository can maintain multiple branches of development. To create a new branch named "experimental", use
git branch experimental
If you run
git branch
you will get a list of all existing branches.
To merge the changes made in experimental into master, run
git merge experimental
To delete the experimental branch
git branch -d experimental
This command ensures that the changes in the experimental branch are already in the current branch.
To force a deletion use
git branch -D experimental
github
To clone a project from github copy the HTTP URL in the github project page and type
git clone moc.buhtig|tig#moc.buhtig|tig:username/project





