.
===== Revert =====
git checkout -- filename
git checkout v1.2.3 -- filename # tag v1.2.3
git checkout stable -- filename # stable branch
git checkout origin/master -- filename # upstream master
git checkout HEAD -- filename # the version from the most recent commit
git checkout HEAD^ -- filename # the version before the most recent commit
===== Committer =====
Committer: Thuy Dang
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
===== origin =====
==== Show remote repos ====
git remote --verbose
==== Migrate to other git repo ====
* create other repo.
* init an empty repo by "git push remote_repo master"
* then following:
# In this example, we use an external account named extuser and
# a GitHub account named ghuser to transfer repo.git
git clone --bare https://githost.org/extuser/repo.git# Make a bare clone of the external repo to a local directory
cd repo.git
$ git push --mirror https://github.com/ghuser/repo.git# Push mirror to new GitHub repo
cd ..
$ rm -rf repo.git# Remove temporary local repo
===== git add =====
Note that empty directories are ignored.
git add -A stages All
git add . stages new and modified, without deleted
git add -u stages modified and deleted, without new
===== Branching, tagging =====
source: http://learn.github.com/p/branching.html
==== View available branches ====
git branch
git branch -a
==== Create a new branch ====
We can use ‘git branch (branchname)’ which will create a branch at the point we’re currently at.
git branch experiment
==== Switch to that branch ====
So that the work we do is saved to it instead of the ‘master’ branch, we run the ‘git checkout’ command’
git checkout experiment
Switched to branch "experiment"
git branch
*experiment
master
==== Push branch to server ====
git push origin experiment
==== Merge branch ====
Merge a brach with current branch (master).
git merge experiment
Auto-merging lib/simplegit.rb
Merge made by recursive.
lib/simplegit.rb | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Merge multiple times.
For example, let’s say we switched back to the ‘experiment’ branch, made a few changes, then at some point merged it back into ‘master’ again, making our history look something like this.
==== Delete branch ====
When you are done with a branch, say in this case the ‘experiment’ branch, we can simply delete it with ‘git branch -d’
git branch -d experiment
If the branch has not been merged in at some point, in which case deleting the branch would lose changes, Git will not allow you to do it. If you want to lose the changes, simply use the ‘-D’ flag instead - that will force the deletion.
===== Update/pull remote:branch to local:branch =====
source StackOverflow.
===== History =====
==== Rebase to merge / remove history ====
History of commit may contain sensitive data. remove them.
Show all commit:
git log -v
commit 1
commit n
Interactively choose what to do with the commit:
git rebase -i commit_1~8 <---- ~8 numbers of history
Then choose action on each commit, e.g., drop, squash
Push this branch overwriting remote:
git push --force
git push -f
==== New Git ====
git clone git://example.com/myproject
cd myproject
Next, look at the local branches in your repository:
git branch
* master
But there are other branches hiding in your repository! You can see these using the -a flag:
git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
If you just want to take a quick peek at an upstream branch, you can check it out directly:
git checkout origin/experimental
But if you want to work on that branch, you'll need to create a local tracking branch:
git checkout -b experimental origin/experimental
and you will see
Branch experimental set up to track remote branch experimental from origin. Switched to a new branch 'experimental'
That last line throw some people "New branch" - huh? What it really means is a new local branch that gets the branch from the index and creates it locally for you. The previous line is actually more informative as it tells you that the branch is being set up to track the remote branch, which usually means the origin/branch_name branch
Now, if you look at your local branches, this is what you'll see:
git branch
* experimental
master
You can actually track more than one remote repository using git remote.
$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
remotes/win32/master
remotes/win32/new-widgets
At this point, things are getting pretty crazy, so run gitk to see what's going on:
gitk --all &
=== Checkout tag to a new branch ===
git tag (-l)
git checkout -b td_up_tagxx tagxx
==== Old git ====
You have set the upstream of that branch
(see:
"How do you make an existing git branch track a remote branch?" and
"Git: Why do I need to do --set-upstream all the time?"
)
git branch -f --track my_local_branch origin/my_remote_branch
# OR:
$ git branch --set-upstream my_local_branch origin/my_remote_branch
That means your branch is already configured with:
branch.my_local_branch.remote origin
branch.my_local_branch.merge my_remote_branch
Git already has all the necessary informations.
In that case:
# if you weren't already on my_local_branch branch:
git checkout my_local_branch
# then:
git pull
is enough.
If you hadn't establish that upstream branch relationship when it came to push your 'my_local_branch', then a simple git push -u origin my_local_branch:my_remote_branch would have been enough to push and set the upstream branch.
After that, for the subsequent pulls/pushes, git pull or git push would, again, have been enough.
===== Sources =====
* http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging |