zoukankan      html  css  js  c++  java
  • 一些有用的git命令清单【转】

    转自:https://www.cnblogs.com/foohack/p/7204372.html

    以下是一些我常用的git命令清单

    如果以下的命令不清晰细节,请看git的文档。

    设置个人信息

    git config --global user.name "John Doe"
    git config --global user.email "john@example.com"

    使用 --global 那么以上的设置会在所有git工程中生效. 如果 git config 没有使用 --global 参数并且运行在当前工程目录下, 那么以上设置只会对当前工程生效。

    让Git忽略文件权限

    cd project/
    git config core.filemode false

    这个选项是让我们忽略文件权限,也就是文件权限对我们不重要的时候,比如在windows系统下就常用

    查看Git的当前配置清单

    git config --list

    初始化一个git仓库本地

    cd existing-project/
    git init

    把远程仓库克隆到本地

    git clone https://github.com/user/repository.git

    这个会在本地新创建一个目录(以远程仓库的名称命名)


    查看某个git子命令的帮助文档

    git help clone

    Update and merge your current branch with a remote 把远程的特定分支更新到本地当前的分支,并与本地分支合并

    cd repository/
    git pull [remote_repo_name] [remote_branch_name]

    如果你不想合并,那么就使用git fetch,不要用pull

    查看远程仓库的URL

    git remote -v

    更改远程仓库的URL

    git remote set-url [remote_repo_name] http//github.com/repo.git

    添加个远程仓库

    git remote add [remote_repo_name] https://github.com/user/repo.git

    查看本地文件(没有添加到暂存区的文件)的变化

    git diff

    Note that this does not track new files.

    查看本地文件(添加到了暂存区但没有commit的文件)的变化

    git diff --cached

    查看本地文件相对于远程仓库特定分支的变化

    git diff [local_repo_name]/[local_branch_name]

    比较两个commit之间的变化

    git diff COMMIT1_ID COMMIT2_ID

    比较两个commit之间的变化(仅仅只会列出变化的文件名)

    git diff --name-only COMMIT1_ID COMMIT2_ID

    查看一个特定commit的文件变化

    git diff-tree --no-commit-id --name-only -r COMMIT_ID

    or

    git show --pretty="format:" --name-only COMMIT_ID

    source: http://stackoverflow.com/a/424142/1391963

    查看push之前的变化(暂存区与本地仓库分支的比较)

    git diff --cached [repo_name]/[branch_name]

    查看一个特定commit的详情(文本变化,提交信息)

    git show COMMIT_ID

    查看当前工作目录的git状态

    git status

    普通的更改文件,提交文件

    git add changed_file.txt
    git add folder-with-changed-files/
    git commit -m "Commiting changes"

    重命名或移动,删除文件,提交修改

    git rm removeme.txt tmp/crap.txt
    git mv file_oldname.txt file_newname.txt
    git commit -m "deleting 2 files, renaming 1"

    修改最后一个提交的信息(如果最后一个提交的信息有必要修改更正的话)

    git commit --amend -m "New commit message"

    把本地的commits 推送到远程仓库的特定分支

    git push [repo_name] [branch_name]

    查看最近提交的历史日志

    git log

    查看最近2个commits的历史日志

    git log -2

    查看最近2个commits的历史,并进行比较(diff)

    git log -p -2

    查看最近提交的历史日志,以单行的形式显示

    git log --pretty=oneline

    恢复到一个特定的commit,并提交

    git revert [commit_id]
    git push origin master
    

    恢复到一个特定的commit的状态

    # reset the index to the desired tree
    git reset 56e05fced
    
    # move the branch pointer back to the previous HEAD
    git reset --soft HEAD@{1}
    
    git commit -m "Revert to 56e05fced"
    
    # Update working copy to reflect the new commit
    git reset --hard [commit_id]

    Source: http://stackoverflow.com/q/1895059/1391963

    撤销上一个提交,并保留本地变化

    git reset --soft HEAD~1
    

    撤销上一个提交,不保留本地变化

    git reset --hard HEAD~1
    

    撤销上一个提交,在索引中保留本地变化

    git reset --mixed HEAD~1
    

    Or git reset HEAD~1
    See also http://stackoverflow.com/q/927358/1391963

    撤销本地没有push的commits

    git reset [repo_name]/[branch_name]
    

    把本地的状态重置为远程的一个特定状态

    git fetch [repo_name]
    git reset --hard [repo_name]/[branch_name]

    查看本地的分支

    git branch

    查看所有的分支

    git branch -a

    创建一个标签

    git tag 7.x-1.3

    推送一个标签

    git push [repo_name] 7.x-1.3

    创建一个分支

    git checkout master
    git branch new-branch-name

    创建一个分支并切换到新分支

    git checkout -b new-branch-name

    从当前分支的上一个提交创建一个分支(或者说,新分支的base,是当前分支的上一个提交)

    git branch branchname 

    or

    git branch branchname HEAD~1       //这里的HEAD~1可以改成特定的commit_id

    or

    git checkout -b branchname 

    Source: http://stackoverflow.com/a/2816728/1391963

    切换到新分支

    git checkout new-branch-name

    合并分支

    git checkout master
    git merge branch-name

    把branch-name中的所有commit合并到master分支

    把branch-name的分支合并到当前分支,并且不合并commits

    git merge branch-name --no-commit --no-ff

    查看当前分支与其他分支的不同

    git diff branch-name

    查看当前分支某个特定文件与其他分支的不同

    git diff branch-name path/to/file

    删除一个分支

    git branch -d new-branch-name

    把本地新分支推送到远程仓库

    git push [repo_name] new-branch-name

    得到远程所有分支

    git fetch origin

    得到git的根目录

    git rev-parse --show-toplevel

    Source: http://stackoverflow.com/q/957928/1391963

    从仓库中删除所有本地已删除的文件

    git rm $(git ls-files --deleted)

    Source: http://stackoverflow.com/a/5147119/1391963

    删除所有没进入暂存区的文件

    git clean -f

    包含目录:

    git clean -f -d

    防止意外发生:

    git clean -n -f -d

    Source: http://stackoverflow.com/q/61212/1391963


    撤消某个git add的文件:

    git reset HEAD file.txt

    用git rebase来合并分支:

    比如我有一个本地的分支已经推送到了远程分支,都叫new-ui。  该分支是为了完成新的UI功能,当new-ui分支测试完毕了,需要合并到主分支master,因为想保留new-ui分支上的所有commit历史信息,不能采用git merge命令,先要把

    整个new-ui的分支变基到master分支上来 ,先切换到new-ui分支,然后调用git rebase master这个命令,结果就是这样整条new-ui提交链条就挂到master头节点的后面了,  变成了 :

      ----+---------+-----------+------------+

           |                                              |

       master                                 new-ui

    然后git会自动合并,冲突的部分会提示,与传统的git merge没有任何区别,然后修改冲突文件,修改完毕后保存,然后git add去除冲突的文件,此时无任何冲突了,可以继续尝试rebase了,git rebase --continue 。注意,

    如果没有冲突,直接完整合并,那么是不用git rebase --continue的。这时候可以用git merge合并两个分支了,先切换到master分支上,然后调用git merge new-ui,结果这时候才会正式把new-ui的代码合并到master分支上,工作流图变成了以下:

                                                     master

                                                       |

    ----+---------+-----------+------------+

                                                       |

                                                     new-ui

    master指针已经指到尾部了,可以删除本地的new-ui分支了,git branch -d new-ui

    最后删除远程new-ui分支:

    先查看远程分支  git branch  -r

    然后根据远程分支的名称删除远程分支: 

    git branch -r -d [remote_repo_name]/new-ui

    git push origin :new-ui



    用git 远程仓库之间的备份:

    比如你在github上有一个多次commit的repo,你需要把这个远程仓库的所有历史commit log等版本信息全部备份到公司内网的repo。

    需要做以下步骤

    git clone [github_repo_url]

    然后cd到clone下来的仓库目录下

    然后运行 git push --mirror [internal_netwrok_repo_url]

    【作者】张昺华
    【大饼教你学系列】https://edu.csdn.net/course/detail/10393
    【新浪微博】 张昺华--sky
    【twitter】 @sky2030_
    【微信公众号】 张昺华
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    com.android.ide.common.process.PrecessException:org.gradle.process....finished with non-zero exit value 1
    android 学习笔记(1)
    C#遍历指定文件夹中的所有文件(转)
    让TextView里面的文字逐个显示的动画效果实现(1)
    This Handler class should be static or leaks might occur(null) 解决办法 (转)
    Android开发 旋转屏幕导致Activity重建解决方法(转)
    SQLite 数据库
    【Android】error opening trace file: No such file or directory (2)
    WPF中的RichTextBox
    android中的 Toast 和 AlertDialog
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/13938284.html
Copyright © 2011-2022 走看看