Create a new repository
To create a new directory, open it and perform a
git init
Checkout a repository
To create a working copy of a local repository by running the command
git clone /path/to/repository
When using a remote server, your command will be
git clone username@host:/path/to/repository
Add & Commit
You can propose changes (add it to the Index) using
git add <filename>
git add *
This is the first step in basic git workflow. To actually commit these changes use
git commit -m "Commit message"
Now the file is committed to the HEAD, but not in your remote repository yet.
Workflow
Your local repository consists of three "trees" maintained by git. the first one is your Working Directory which holds the actual files.
the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you've made.
Pushing changes
Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute
git push origin master
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <server>
Now you are able to push your changes to the selected remote server
Branching
Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
Create a new branch named "feature_x" and switch to it using
git checkout -b feature_x
switch back to master
git checkout master
and delete the branch again
git branch -d feature_x
a branch is not available to others unless you push the branch to your remote repository
git push origin <branch>
Update & Merge
To update your local repository to the newest commit, execute
git pull
In your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
git merge <branch>
In both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
git add <filename>
Before merging changes, you can also preview them by using
git diff <source_branch> <target_branch>
Tagging
git log
git whatchanged -n 1
git log --stat
git diff HEAD^ HEAD
git show HEAD~2 HEAD~2
git remote -v //get remote link
Revert local changes
git clean -f -d
git reset --hard
git checkout -- HEAD
git checkout -- <filename>
git reset --hard HEAD~1
git reset --hard HEAD@{1}
HEAD~1
means "go to the commit before the commit that HEAD currently points at",
HEAD@{1}
means "go to the commit that HEAD pointed at before it pointed at where it currently points at".
Detailed
Here
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
git fetch origin
git reset --hard origin/master
If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):
git reset --soft HEAD~1
Do not use the
--hard flag unless you want to destroy your work in progress since the last commit.
To Remove remote commit changes:
To remove git commit after git push:
git reset --hard HEAD~1
git push -f
To switch among the commits, use
"$ git reflog" and then using
"$ git checkout commit_id" jump to specific commit id back and forth
To merge branches
$ git fetch
$ git checkout a (you will switch to branch a)
$ git merge b (this will merge all changes from branch b into branch a)
$ git commit -a (this will commit your changes)
$ git push
To checkout remote branch
Before you can start working locally on a remote branch, you need to fetch it.
To fetch a branch, you simply need to:
$ git fetch origin
This will fetch all of the remote branches for you. With the remote branches in hand,
you now need to check out the branch you are interested in, giving you a local working copy
$git checkout -b test origin/test
To create remote branch
1. First, you create your branch locally
git checkout -b your_branch
e.g. git checkout -b master-devel-v8.0
2. Then push it to the remote server
git push <remote-name> <branch-name>
e.g. git push -u origin master-devel-v8.0
To delete a remote branch
git push origin --delete <branchName>
e.g. git push origin --delete master-devel-v8.0
To delete Local Branch
git branch -D <branchName>
e.g. git branch -D master-devel-v8.0
To create development branch from master
1. first checkout master
2. then checkout devel
To create development branch from master
If two people are working on same branch in git
1. First should commit and push his changes. Then
2. Second should commit his changes and pull first's changes from server and then push everything.
To rebase branch:
1. First checkout working branch
2. Then, rebase from master branch
e.g. git checkout mutex
git rebase component-integration
To changes of a file in all commit:
git log -p -- path/to/file
Useful Hints:
built-in git GUI
gitk
use colorful git output
git config color.ui true
show log on just one line per commit
git config format.pretty oneline
use interactive adding
git add -i
https://stackoverflow.com/questions/8801729/is-it-possible-to-have-different-git-config-for-different-projects
https://stackoverflow.com/questions/4523496/is-it-possible-to-pull-from-one-repo-and-push-to-other-one
http://gitref.org/creating
http://git-scm.com/book
http://rogerdudler.github.io/git-guide/
http://www.vogella.com/articles/Git/article.html
http://stackoverflow.com/questions/2745076/what-is-the-difference-between-git-commit-and-git-push
http://www.cyberciti.biz/faq/appy-patch-file-using-patch-command