ITeye博客

Pro Git Tips

standalone · 2011-07-14 17:53 · linux · 研发管理 · Markdown

1. git init

Create a new repo with exsiting project.

 

2. git diff

This command compares what's in your working directory with what's in your staging area. The result tells what changes have not been staged.

 

3. git diff --cached

Shows what you've stage that will go into your next commit. In Git v1.6 or later, you can use git diff --staged.

 

4. git comit -a -m 'comment'

skip staging area. You do not need run git add before you commit.

 

5. git rm

Reimoves the file from your working directory and you don't see it as an untracked file next time.

 

6.  git rm --cached

Remove the file from Git but keep it on your hard disk.

 

7. git mv file_a file_b

 

 8. git log -p -2

This example shows last two commit log. -p shows diff introduced in each commit.

 

9. git log --pretyy=format:"%h %s" --graph

shows branch and merge history. More options:

 

Option	Description of Output
%H	Commit hash
%h	Abbreviated commit hash
%T	Tree hash
%t	Abbreviated tree hash
%P	Parent hashes
%p	Abbreviated parent hashes
%an	Author name
%ae	Author e-mail
%ad	Author date (format respects the –date= option)
%ar	Author date, relative
%cn	Committer name
%ce	Committer email
%cd	Committer date
%cr	Committer date, relative
%s	Subject

 

10. git commit --amend

You can redo your last commit. This can be useful when you forgot to git add some files or you want to edit your commit message.

 

 

11. git reset HEAD

Change file to unstage.

 

12. git checkout -- filename

This discards your current changes. It looks like when you last committed.

 

13. git remote add shortname remote_repo

This command adds a remote repo. git remote -v shows your current remote repo.

 

14. git fetch shortname

This fetches all information you don't have in your repo but in shortname. After this, you have all refs to all branches from that remote. you can run git merge shortname/master to merge it into your master branch.

Notify git fetch pulls all new commit to your working directory but does not automatically merge it.

 

15. git pull

This fetches data and merges.

 

16. git push [remote-name] [branch-name]

E.g., "git push origin master" pushes your master branch to origin repo.

 

17. To be continued...

评论(2)

  1. #2 楼 · standalone · 2011-08-14 14:38

    Figure 3-31. A history with a topic branch off another topic branch. Suppose you decide that you want to merge your client-side changes into your mainline for a release, but you want to hold off on the server-side changes until it’s tested further. You can take the changes on client that aren’t on server (C8 and C9) and replay them on your master branch by using the --onto option of git rebase:

    $ git rebase --onto master server client This basically says, “Check out the client branch, figure out the patches from the common ancestor of the client and server branches, and then replay them onto master.” It’s a bit complex; but the result, shown in Figure 3-32, is pretty cool.

  2. #1 楼 · standalone · 2011-08-12 22:37
    1. git rm --cahced 从git里面去除但是保留在磁盘上;
    2. git rm 删除文件并从git里面去除;
    3. git reset HEAD back到unstaged状态;
    4. git checkout -- file 丢弃修改,返回到上次commit的样子。

    所以 如果你要放弃修改,回到干净的上次commit的版本,只能用第四个; 如果你git add了一个文件,现在反悔了,那么用第三个可以撤销,不会影响你的更改; 如果你自己的一个test文件误加进了git,你只希望从git里面拿掉,但不希望从磁盘上删除,用第一个; 如果你根本不想要这个文件了,那么用git rm吧。

    不知道上面总结的对不对。

评论功能维护中,当前仅展示历史评论。