GIT基本操作命令大全

来源:互联网 发布:mac系统怎么下载word 编辑:程序博客网 时间:2024/05/21 18:42

1、更新文件

git pull


2、提交文件

git add *       ——提交所有修改的文件

git add [file1] [file2]


3、查看状态

git status

[html]view plaincopy
  1. $ git status  
  2. # On branch master  
  3. # Changes not staged for commit:  
  4. #   (use "git add <file>..." to update what will be committed)  
  5. #   (use "git checkout -- <file>..." to discard changes in working directory)  
  6. #  
  7. #    modified:   readme.txt  
  8. #  
  9. no changes added to commit (use "git add" and/or "git commit -a")  

4、提交文件

git commit -m "代码提交信息"

5、查看日志

git log

通过第4步提交是提交到head上,还没有都远程仓库上。

6、提交到远程仓库

git push origin master


提交到master是有权限控制的,所以要使用git push origin HEAD:refs/for/master(把master可以换成任何你想提交的分支)将代码提交上去



7、将分支中的某次提交到master上

切换到master分支,git cherry-pick e6327f8d01276bcd22b38d53b1c7a907802a2c04(commit Id)


如果你现在的位置是在分支上,那么你需要切换master:git checkout master。切换前将你本次提交的id记下来,

然后使用命令,将分支上的这一次提交覆盖到master上:git cherry-pick e6327f8d01276bcd22b38d53b1c7a907802a2c04(commit Id)

然后使用git push origin HEAD:refs/for/master(把master可以换成任何你想提交的分支)


8、删除某次git add

一种是 git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除;

一种是 git rm --f "文件路径",不仅将该文件从缓存中删除,还会将物理文件删除(不会回收到垃圾桶)。


9、撤销某次提交

git reset HEAD (撤销这个commit)


10、回到哪个版本

git reset --hard commitid


11、放弃本地修改,直接覆盖之

git reset --hardgit pull


12、stash

通常遇到这个问题,你可以直接commit你的修改;但我这次不想这样。
看看git stash是如何做的。

1、git stash
2、git pull
3、git stash pop

接下来diff一下此文件看看自动合并的情况,并作出相应修改。
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

13、撤回add

git reset file