一半君的总结纸

听话只听一半君

Git Tips常用命令备忘录

智硬,免得再忘了

# 建一个空的 bare repo
git init --bare

# 设全局的或者当前repo的用户名和email
git config --global user.name fucker
git config user.email fucker@fuck.com

# 看全部config
git config --list

# fetch 怎么用
git fetch
git diff master origin/master
git pull

# 自己另设 diff/merge 查看工具
git config --global diff.tool xxdiff
git config --global merge.tool xxdiff
git config --global --add difftool.prompt false

# 然后你就可以
git difftool 

# 让本地的master的 upstream branch是 origin/master, 也就是让跟着他一起变
# 如果你是clone来的,这一步已经自动做了,但如果你是自己加的remote repo,当你pull的时候,会看到类似
# Your configuration specifies to merge with the ref 'master'
# from the remote, but no such ref was fetched.
# 的提示,这是因为你没有
git branch --set-upstream master origin/master

# 把本地的branch push到remote,并且同时设置tracking
git push -u origin feature_branch_name

# checkout remote branch
# 新版本的git (>= 1.6.6)已经不需要加 --track了,因为默认加了
git fetch
git checkout your-branch-name

# 取消所有staging的changes
git checkout -- .

# 临时保存工作
git stash
# 恢复工作
git stash apply
git stash list
# apply的同时删掉
git stash pop

git stash drop <id>
# 删所有
git stash clear
git stash apply stash@{1}

# lightweighted tag
git tag example
# annotated tag
git tag -a v1 -m "Version 1 release"

# 一行搞定 删掉所有已经merge进master的branch
git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

# push一个特定的commit
git push origin <thelonghash>:master

# Shows the changes between the working directory and the index. This shows what has been changed, but is not staged for a commit.
git diff

# Shows the changes between the index and the HEAD(which is the last commit on this branch). This shows what has been added to the index and staged for a commit.
git diff --cached

# Shows all the changes between the working directory and HEAD (which includes changes in the index). This shows all the changes since the last commit, whether or not they have been staged for commit or not.
git diff HEAD

# 如何在 git status 的时候只显示tracked files
git status --untracked-files=no # 或者
git status -uno

# 显示master下所有被tracking的files
git ls-tree -r master --name-only

# 删掉所有untracked files连目录一起,最好先-n 或 --dry-run 测试下...
git clean -f -d

# 推送tags
git push --follow-tags

如何加一个空目录到git,但是不要他下面的所有文件夹

# Ignore everything in this directory
*
# Except this file
!.gitignore

带submodule的repo,clone过后是空的

# 一开始就加上--recursive就可以
git clone <repo> --recursive

# 如果忘了,可以在最顶层
git submodule update --init

# 删除已经push到github的上一个commit
git push -f origin HEAD^:master

# 删除本地多个commit
git reset --hard HEAD~n

# 修改上一个commit message
git commit --amend

# 如果像修改已经push了的上一个commit message
git push --force example-branch

GitDiffSimple

参考:
Got 15 minutes and want to learn Git?
git – the simple guide
Git Clean: Delete Already-Merged Branches
How do I show the changes which have been staged?
365git
How can I add an empty directory to a Git repository?
How can I remove a commit on GitHub?

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: