zoukankan      html  css  js  c++  java
  • Git命令汇总

    关于GIT,推荐阅读 http://www.liaoxuefeng.com/ ,讲的非常透彻清楚。

    http://www.yiibai.com/git/git_pull.html


     查看当前分支版本 git rev-parse HEAD



    创建GIT版本库

    git init

    vim readme.txt

    git commit -m "wrote a readme file"

    echo "# Design-Pattern" >> README.md
      git init
      git add README.md
      git commit -m "first commit"
      git remote add origin https://github.com/xdlaoliu/Design-Pattern.git
      git push -u origin master

    常用分支内命令

    git add <file>

    git add -u 将所有修改过的Tracked files加入Stage

    git add -a 将所有修改过的文件加入Stage

    git status 查看工作空间和Stage信息

    git commit -m "comments"

    git log 查看 commit 历史

    git log --pretty=oneline 格式化查看

    git log --graph --pretty=oneline --abbrev-commit 图形化查看

    git reflog 记录每一次命令


    分支管理命令

    git branch <branch name> 创建分支

    git checkout <branch name> 切换分支

    git checkout -b <branch name> 创建分支并切换到某个分支

    git checkout -d <branch name> 删除分支

    git branch 查看本地分支

    git branch -a 查看所有分支

    git branch -r 查看远程分支

    git merge --no-ff -m "merge with no-ff" dev not fast-forward模式,可以显示merge信息


    分支合并

    git merge <branch name>

    git merge --no-off -m "comments" dev 避免快速合并


    版本回退与撤销操作

    git checkout <filename> :  file stage -> workspace

    git checkout HEAD <filename> : file Repo -> Stage -> workspace

    git checkout HEAD : all files Repo -> Stage -> workspace

    git reset : all files Repo -> Stage

    git reset HEAD : all files Repo -> Stage

    git reset HEAD <filename> : file Repo -> Stage

    git reset --hard HEAD Stage回退到上一版本, HEAD指向上一版本 Repo -> Stage -> Workspace

    git reset --hard origin/master 强制放弃本地修改

    本地新增了一堆文件(并没有git add到暂存区),想放弃修改。 https://blog.csdn.net/ustccw/article/details/79068547

    单个文件/文件夹:

    $ rm filename / rm dir -rf

    所有文件/文件夹:

    $ git clean -xdf

    git fetch -all 下载远程分支内容,不做合并。

    git pull origin master

    git revert

    git reset --hard 3628164 回退到版本号为3628164, HEAD指向3628164

    git stash 把工作现场储存起来

    git stash pop 恢复工作现场

    用git如何把单个文件回退到某一版本

    https://segmentfault.com/q/1010000002464973

    https://www.cnblogs.com/newcbs/p/7814671.html


    与远程git服务器交互

    git remote 显示远程信息

    git remote -v

    git push origin master 推送分支

    git clone url 克隆远程git

    git checkout -b dev origin/dev 创建远程origin dev到本地dev

    git pull 从远程分支下载最新代码  git branch --set-upstream dev origin/dev

    git push origin --delete <branch name> 删除远程分支

    git push origin log_standardize:feature/log_standardize 

    git push -u origin master -f

    远程版本回退 https://blog.csdn.net/wuhounuanyangzhao/article/details/79816171

    多人协作的工作模式通常是这样:

    1. 首先,可以试图用git push origin branch-name推送自己的修改;

    2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

    3. 如果合并有冲突,则解决冲突,并在本地提交;

    4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

    如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name

    这就是多人协作的工作模式,一旦熟悉了,就非常简单。

    小结

    • 查看远程库信息,使用git remote -v

    • 本地新建的分支如果不推送到远程,对其他人就是不可见的;

    • 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;

    • 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;

    • 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name

    • 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。


     标签管理

    命令git tag <name>用于新建一个标签,默认为HEAD,也可以指定一个commit id;

    git tag v0.9

    git tag -a <tagname> -m "blablabla..."可以指定标签信息;

    git tag v0.9 6224937

    git tag -s <tagname> -m "blablabla..."可以用PGP签名标签;

    命令git tag可以查看所有标签。

    git show v0.9

    命令git push origin <tagname>可以推送一个本地标签;

    命令git push origin --tags可以推送全部未推送过的本地标签;

    命令git tag -d <tagname>可以删除一个本地标签;

    命令git push origin :refs/tags/<tagname>可以删除一个远程标签

    git push origin --delete tag <tagname>

    git fetch -p 删除没有和远程分支对应的本地分支


    版本对比

    git diff 8317171c34f 33a04b290f8 --stat scripts


     GitHub

    小结

    • 在GitHub上,可以任意Fork开源仓库;

    • 自己拥有Fork后的仓库的读写权限;

    • 可以推送pull request给官方仓库来贡献代码。


    git 上传Maven项目

    只要提交

    • src

    • pom.xml

    • README.MD

    • .gitignore

    其他的文件都不需要也不应该被提交上来,否则很容易造成冲突。

    提供个java的gitignore 模板:

    ### Java template
    *.class
    
    # Mobile Tools for Java (J2ME)
    .mtj.tmp/
    
    # Package Files #
    *.jar
    *.war
    *.ear
    
    # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
    hs_err_pid*
    ### JetBrains template
    # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
    # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
    
    # User-specific stuff:
    .idea/workspace.xml
    .idea/tasks.xml
    .idea/dictionaries
    .idea/vcs.xml
    .idea/jsLibraryMappings.xml
    
    # Sensitive or high-churn files:
    .idea/dataSources.ids
    .idea/dataSources.xml
    .idea/dataSources.local.xml
    .idea/sqlDataSources.xml
    .idea/dynamic.xml
    .idea/uiDesigner.xml
    
    # Gradle:
    .idea/gradle.xml
    .idea/
    
    # Mongo Explorer plugin:
    .idea/mongoSettings.xml
    
    ## File-based project format:
    *.iws
    
    ## Plugin-specific files:
    
    # IntelliJ
    /out/
    
    # mpeltonen/sbt-idea plugin
    .idea_modules/
    
    # JIRA plugin
    atlassian-ide-plugin.xml
    
    # Crashlytics plugin (for Android Studio and IntelliJ)
    com_crashlytics_export_strings.xml
    crashlytics.properties
    crashlytics-build.properties
    fabric.properties
    ### Windows template
    # Windows image file caches
    Thumbs.db
    ehthumbs.db
    
    # Folder config file
    Desktop.ini
    
    # Recycle Bin used on file shares
    $RECYCLE.BIN/
    
    # Windows Installer files
    *.cab
    *.msi
    *.msm
    *.msp
    
    # Windows shortcuts
    *.lnk
    ### Maven template
    target/
    pom.xml.tag
    pom.xml.releaseBackup
    pom.xml.versionsBackup
    pom.xml.next
    release.properties
    dependency-reduced-pom.xml
    buildNumber.properties
    .mvn/timing.properties
    # ignore eclipse files
    .project
    .classpath
    .settings
    .metadata

     Github上fork项目后与原项目保持同步

    http://blog.csdn.net/niuszeng/article/details/51305061

    https://blog.csdn.net/ustccw/article/details/79068547

  • 相关阅读:
    distcc加速内核编译
    ssh不检查server变化
    bbb u-boot SPI 启动
    Debian NAT共享上网
    Debian Epson L455 打印机
    Learn CMake's Scripting Language in 15 Minutes (ZZ)
    网络启动并安装Debian
    GNU LD 脚本学习笔记
    JLink defective
    获取真实mac地址
  • 原文地址:https://www.cnblogs.com/xdlaoliu/p/6964598.html
Copyright © 2011-2022 走看看