---
title: "git"
author: "weina"
date: "2011-11-21T11:42:18Z"
tags: []
categories: ["行业应用"]
slug: "git"
draft: false
lastmod: "2012-02-21T10:27:52Z"
id: 1270605
url: "https://weina.iteye.com/blog/1270605"
---

1\. git add 添加文件到staged area.  
2\. git commit 会commit staged file ，有几个选项比较重要：-a 跳过staged area,直接commit，后面加文件名也会使那个文件跳过stage，直接commit。但是前提是文件已经在index中了，新文件不会自动加入。  
3.git status 查看从上次commit到目前的变化，git diff会更详细，但是它只包含应该要包含在staged area中却还未包含进去的变化，git diff --staged则列出来从上次commit到staged area的变化。  
4\. .gitignore文件可以设置哪些文件被排除进去，通常可以设.o, .a等文件。  
5\. git rm 用来remove文件。  
6\. git mv可以用来重命名文件。  
7\. git log用来查看日志，git log -p查看详细，gitk 是个图形化的查看工具。  
8.Unstaging a Staged File可以用`git reset HEAD <file>。` Unmodifying a Modified File可以用`git checkout -- <file>。` `git commit --amend用来取消上次的commit。 9. git remote显示所有的remote(加-v显示详细信息)。` `git remote add [shortname] [url]用来添加remote。` `git fetch [remote-name]只会pull下来全部的更动，但不会自动merge，但是git pull会自动merge。` `git remote show [remote-name]可以看到一个remote的详细信息。` `git remote rename用来改变一个remote的名字。` `git remote rm删除一个remote。 10.git tag列出所有的tag。git show tag-name可以查看tag详细信息。git tag [-a] tag-name -m message [commit-checksum]如果有-a 就添加一个annotated tag，如果没有-a，就添加一个lightweight tag,如果后面要在某个commit之后添加一个tag，就加上checksum好了。 git push有个选项 --tags会把tags信息也push上去。`

`11.` `git branch testing添加一个testing的branch.` `git checkout testing切换到testing branch.`

`git branch -b branchname tagname`

`git push origin branchname               // push remote branch for one tagname`

`git branch --track branchname   |git baanch branchname origin/branchname`

` `

`// checkout branchname`

```
git checkout --track origin/branchname
```

`12. 切换branch的时候，当前的branch不能有和要切换过去的branch冲突的uncommitted changes. 13. git merge branch-name 将branch-name merge到当前branch.如果有conflict，可以用status查看状态，解决conflict之后用add 通知git已经解决。然后用commit commit the merge。 14.`  `git branch -d branch-name,但是这个branch必须被全部merge到HEAD中了。如果选项是-D的化，就不管它的merge status了。 15. git branch --merged只显示被HEAD完全包含的branch，--no-merged只显示没被HEAD完全包含的branch。 16.` `git push (remote) (branch)把本地某个branch push到remote上去。` `git push (remote) (local-branch-name):(remote-branch-name)可以把本地branch push到remote branch上去。 17.git fetch 下载下来的branch不能edit，必须要手动merge到本地branch之后才可以edit。`   $ git checkout -b local-branch-name remote-name/remote-branch-name 可以把server`上的branch下载下来，并且可以直接edit。 18.` Tracking Branches里面可以不必在pull和push后面加remote参数。git branch -r列出remote tracking branches.  
19\. `git push [remotename] :[remote-branch]删除掉某个remote branch。 20. rebase可以获得和merge一样的效果，但是log记录更好，因为看起来没有分支。` `git rebase --onto newbase upstream branch会把从upstream到branch的改动rebase到newbase上去。git rebase basebranch topicbranch会把topicbranch rebase到basebranch上。 21.` **Do not rebase commits that you have pushed to a public repository.**

**22.git fetch默认会获取repo所有branch的进度，除非指定哪个branch。git push可以将某个branch push到repo中去。**

23、 git reset --hard HEAD~3   会将最新的3次提交全部重置，就像没有提交过一样。

24、git stash — 暂存临时代码

可以将你当前未提交到本地(和服务器)的代码推入到Git的栈中，这时候你的工作区间和上一次提交的内容是完全一样的，所以你可以放心的修 Bug，等到修完Bug，提交到服务器上后，再使用’git stash apply’（stash中还有备份）将以前一半的工作应用回来。或者用"git stash pop"来将改的应用返回来，此时stash中清空 ,相当于调用了git stash clear来清空stlash

25、git config --global user.name="weina"

       git config --global user.email=''weina@weina.com" 配置全局的name和email，以便再提交的时候，能看到是谁提交的

26、clone porject   `用git clone username@ip:29418/projectPath 拷贝项目`

      使用 scp -p -P 29418 yourdomain:hooks/commit-msg .git/hooks/拷贝到你的项目中,可以通过commit后 gitk   查 看log日志有没有相应的changeId`来确认。如果不拷贝这个hooks,在提交到gerrit时很容易发生错误。`

 

27、使用utf8确定commit的字符集修改GIT\_HOME/etc/gitconfig文件增加

      \[gui]

      encoding=utf-8

     \[i18n]

     commitencoding=utf-8

`logoutputencoding=utf-8`