- Create a new local repo:
git init
- Clone a repo from github
git clone [URL]
##Basic Snapshotting
1. `git diff` shows the patch (unstaged changes since last commit)
2. `git diff --cached` only shows the staged change since last commit (staged changes)
3. `git diff HEAD` show both staged and unstaged changes
4. `git diff --stat [other opts]` show stats and summary of changes
1.`git commit -am "message"` automatically stage all __tracked & modified__ files before commit
git reset HEAD -- [files]
to unstage files from cache and pointer to HEAD
e.g.
$ git status -s
M README
M hello.rb
$ git add .
$ git status -s
M README
M hello.rb
$ git reset HEAD -- hello.rb
Unstaged changes after reset:
M hello.rb
$ git status -s
M README
M hello.rb
- Recipe for unstage:
If you want to be able to just run git unstage, you can easily setup an alias in Git. Just run
git config --global alias.unstage "reset HEAD"
. Once you have run that, you can then just rungit unstage [file]
instead.
The first thing git reset does is undo the last commit and put the touched files back onto the stage.
for example, git reset --soft HEAD~ (the parent of the HEAD)
the last commit will be undone and the files touched will be back on the stage again.
$ git status -s
M hello.rb
$ git commit -am 'hello with a flower'
[master 5857ac1] hello with a flower
1 files changed, 3 insertions(+), 1 deletions(-)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git reset --soft HEAD~
$ git status -s
M hello.rb
it resets your staging area and working directory to the state they were in at the given commit. This is the most dangerous option and is not working directory safe. Any changes not committed will be lost.
replace your local changes; replace your local(working dir) changes to the last commit snapshot. Changes already added to the index are kept.
Summary: use git checkout -- <fn>
for syncing with current cache, use git reset HEAD -- <fn>
to unstage local changes, use git reset --hard HEAD~|commit#
to wind back to a given commit.
git fetch origin
git reset --hard origin/master
在Git中,总是有后悔药可以吃的。当你用$ git reset --hard HEAD^
回退到add distributed
版本时,再想恢复到append GPL
,就必须找到append GPL
的commit id。Git提供了一个命令git reflog
用来记录你的每一次命令:
$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file
see all branches
creates a new branch by NAME
See the last commit of branches
detele a branch
context switch, -b options creates a new branch and switch to it immediately
merge dev branch with master branch:
$git checkout master
$git merge dev
用git log --graph
命令可以看到分支合并图。
In a nutshell you can update your project with git fetch
and share your changes with git push
. You can manage your remote repositories with git remote
.
List your remote aliases
add a new repo to your project
git log --follow [file]
# 显示某个文件的版本历史,包括文件改名