zoukankan      html  css  js  c++  java
  • Git 经常使用的命令

    1、智能程序猿想偷懒。So我们正处于home文件夹加入一个全局配置:.git/config。并定义快捷键:


    [alias]  
        co = checkout  
        ci = commit  
        st = status  
        pl = pull  
        ps = push  
        dt = difftool  
        l = log --stat  
        cp = cherry-pick  
        ca = commit -a  
        b = branch


    2、Now。開始解释经常使用命令。

    查看、加入、提交、删除、找回,重置改动文件

    git help <command> # 显示command的help

    git show # 显示某次提交的内容 git show $id

    git co -- <file> # 抛弃工作区改动

    git co . # 抛弃工作区改动

    git add <file> # 将工作文件改动提交到本地暂存区

    git add . # 将全部改动过的工作文件提交暂存区

    git rm <file> # 从版本号库中删除文件

    git rm <file> --cached # 从版本号库中删除文件,但不删除文件

    git reset <file> # 从暂存区恢复到工作文件

    git reset -- . # 从暂存区恢复到工作文件

    git reset --hard # 恢复近期一次提交过的状态。即放弃上次提交后的全部本次改动

    git ci <file> git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做:tgit ci -am "some comments"

    git ci --amend # 改动最后一次提交记录

    git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建次提交对象

    git revert HEAD # 恢复最后一次提交的状态

    查看文件diff

    git diff <file> # 比較当前文件和暂存区文件差异 git diff

    git diff <$id1> <$id2> # 比較两次提交之间的差异

    git diff <branch1>..<branch2> # 在两个分支之间比較

    git diff --staged # 比較暂存区和版本号库差异

    git diff --cached # 比較暂存区和版本号库差异

    git diff --stat # 只比較统计信息

    查看提交记录

    git log git log <file> # 查看该文件每次提交记录

    git log -p <file> # 查看每次具体改动内容的diff

    git log -p -2 # 查看近期两次具体改动内容的diff

    git log --stat #查看提交统计信息

    tig

    Mac上能够使用tig取代diff和log,brew install tig

    Git 本地分支管理

    查看、切换、创建和删除分支

    git br -r # 查看远程分支

    git br <new_branch> # 创建新的分支

    git br -v # 查看各个分支最后提交信息

    git br --merged # 查看已经被合并到当前分支的分支

    git br --no-merged # 查看尚未被合并到当前分支的分支

    git co <branch> # 切换到某个分支

    git co -b <new_branch> # 创建新的分支,而且切换过去

    git co -b <new_branch> <branch> # 基于branch创建新的new_branch

    git co $id # 把某次历史提交记录checkout出来,但无分支信息,切换到其它分支会自己主动删除

    git co $id -b <new_branch> # 把某次历史提交记录checkout出来。创建成一个分支

    git br -d <branch> # 删除某个分支

    git br -D <branch> # 强制删除某个分支 (未被合并的分支被删除的时候须要强制)

     分支合并和rebase

    git merge <branch> # 将branch分支合并到当前分支

    git merge origin/master --no-ff # 不要Fast-Foward合并,这样能够生成merge提交

    git rebase master <branch> # 将master rebase到branch,相当于: git co <branch> && git rebase master && git co master && git merge <branch>

     Git补丁管理(方便在多台机器上开发同步时用)

    git diff > ../sync.patch # 生成补丁

    git apply ../sync.patch # 打补丁

    git apply --check ../sync.patch #測试补丁是否能成功

     Git暂存管理

    git stash # 暂存

    git stash list # 列全部stash

    git stash apply # 恢复暂存的内容

    git stash drop # 删除暂存区

    Git远程分支管理

    git pull # 抓取远程仓库全部分支更新并合并到本地

    git pull --no-ff # 抓取远程仓库全部分支更新并合并到本地,不要快进合并

    git fetch origin # 抓取远程仓库更新

    git merge origin/master # 将远程主分支合并到本地当前分支

    git co --track origin/branch # 跟踪某个远程分支创建对应的本地分支

    git co -b <local_branch> origin/<remote_branch> # 基于远程分支创建本地分支。功能同上

    git push # push全部分支

    git push origin master # 将本地主分支推到远程主分支

    git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建。用于初始化远程仓库)

    git push origin <local_branch> # 创建远程分支, origin是远程仓库名

    git push origin <local_branch>:<remote_branch> # 创建远程分支

    git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支

    Git远程仓库管理

    git remote -v # 查看远程server地址和仓库名称

    git remote show origin # 查看远程server仓库状态

    git remote add origin git@ github:robbin/robbin_site.git # 加入远程仓库地址

    git remote set-url origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址(用于改动远程仓库地址) git remote rm <repository> # 删除远程仓库

    创建远程仓库

    git clone --bare robbin_site robbin_site.git # 用带版本号的项目创建纯版本号仓库

    scp -r my_project.git git@ git.csdn.net:~ # 将纯仓库上传到server上

    mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在server创建纯仓库

    git remote add origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址

    git push -u origin master # client首次提交

    git push -u origin develop # 首次将本地develop分支提交到远程develop分支,而且track

    git remote set-head origin master # 设置远程仓库的HEAD指向master分支

    也能够命令设置跟踪远程库和本地库

    git branch --set-upstream master origin/master

    git branch --set-upstream develop origin/develop

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    游标cursor
    SQL: EXISTS
    LeetCode Reverse Integer
    LeetCode Same Tree
    LeetCode Maximum Depth of Binary Tree
    LeetCode 3Sum Closest
    LeetCode Linked List Cycle
    LeetCode Best Time to Buy and Sell Stock II
    LeetCode Balanced Binary Tree
    LeetCode Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4810025.html
Copyright © 2011-2022 走看看