美文网首页
Git常用操作

Git常用操作

作者: hjqjk | 来源:发表于2017-09-15 00:08 被阅读17次

    整理Git的常用命令,方便以后查找。

    配置用户信息

    Git的设置文件为 .gitconfig ,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

    设置用户名称和邮箱地址,--global 表示全局设置,本机上所有的Git仓库通用该配置

    $ git config --global user.name "[name]"
    $ git config --global user.email "[email address]"
    

    查看配置文件

    # 查看[仓库级|全局级|系统级]的 config
    $ git config [--local|--global|--system] -l
    
    # 查看当前生效的配置
    $ git config -l
    

    编辑配置文件

    # 查看仓库级的config
    $ git config --local -e
    $ git config -e 
    
    # 查看全局级的config
    $ git config --global -e
    
    # 查看系统级的config
    $ git config --system -e
    

    添加

    # 添加指定文件到暂存区,可指定多个文件
    $ git add [file1] [dir/file2] ...
    
    # 添加指定文件到暂存区,可指定通配符;如 *.txt,匹配所有txt文件
    $ git add [file-pattern]
    
    # 添加指定目录到暂存区,包括子目录
    $ git add [dir]
    
    # 添加当前目录的所有文件到暂存区
    $ git add .
    

    提交

    # 提交暂存区所有文件到仓库区,一定要记得添加提交信息(message)
    $ git commit -m [message]
    
    # 提交暂存区的指定文件到仓库区
    $ git commit [file1] [file2] ... -m [message]
    
    # 提交工作区自上次commit之后的变化,直接到仓库区 (省略 git add 那一步)
    $ git commit -a 
    
    # 使用一次新的commit,替代上一次提交
    # 如果代码没有任何新变化,则用来改写上一次commit的提交信息
    $ git commit --amend -m [message]
    
    # 重做上一次commit,并包括指定文件的新变化
    $ git commit --amend [file1] [file2] ...
    

    删除文件

    # 删除工作区文件(已被追踪的文件),并将这次删除放入暂存区
    $ git rm [file1] [file2] ...
    
    # 停止追踪指定文件(在暂存区中删除),但该文件会保留在工作区
    $ git rm --cached [file]
    
    # 文件改名(工作区),并将这个改名放入暂存区
    $ git mv [file-oldname] [file-newname]
    

    查看信息

    # 检查当前文件状态
    $ git status
    
    # 显示当前分支的版本历史(按提交时间排序)
    $ git log
    
    # 简洁模式,每个提交只显示一行信息,包括 commit id(版本号)和 提交说明
    $ git log --pretty=oneline
    
    # 显示暂存区和工作区的差异
    $ git diff
    
    # 显示暂存区和上一个commit的差异
    $ git diff --cached [file]
    
    # 显示工作区与当前分支最新commit之间的差异
    $ git diff HEAD
    
    # 显示两次提交之间的差异
    $ git diff [first-commit]..[second-commit]
    
    # 显示某次提交的元数据和内容变化
    $ git show [commit-id]
    
    # 显示当前分支的最近几次操作(操作历史)
    $ git reflog
    

    分支

    # 列出所有的分支,"*" 标识的是当前分支
    $ git branch 
    
    # 列出所有远程分支
    $ git branch -r
    
    # 列出所有本地分支和远程分支
    $ git branch -a
    
    # 新建分支,但仍停留在当前分支
    $ git branch [branch-name]
    
    # 新建分支,并切换到该分支
    $ git checkout -b [branch-name]
    
    # 切换到指定分支,并更新工作区
    $ git checkout [branch-name]
    
    # 合并指定分支到当前分支
    $ git merge [branch-name]
    
    # 删除分支
    $ git branch -d [branch-name]
    
    
    

    标签

    # 列出所有tag
    $ git tag
    
    # 新建一个tag在当前commit
    $ git tag [tag-name]
    
    # 新建一个tag在指定commit
    $ git tag [tag-name] [commit-id]
    
    # 新建一个tag,并指定标签注释说明
    $ git tag -a [tag-name] -m "message"
    
    # 提交指定tag到指定远程仓库( git push 默认不提交标签到远程仓库)
    $ git push [remote-name] [tag-name]
    
    # 提交所有的tag到指定远程仓库
    $ git push [remote-name] --tags
    
    # 查看tag信息
    $ git show [tag-name]
    
    # 删除本地tag
    $ git tag -d [tag-name]
    
    # 删除指定远程仓库的tag
    $ git push [remote-name] :refs/tags/[tag-name]
    

    远程同步

    默认远程仓库名:origin
    默认分支:master

    # 抓取指定远程仓库的所有变动,不做合并,不影响工作区
    $ git fetch [remote]
    
    # 显示所有远程仓库(显示仓库地址)
    $ git remote -v
    
    # 显示指定远程仓库的信息(仓库地址、HEAD指向分支、所有分支等)
    $ git remote show [remote-name]
    
    # 增加一个新的远程仓库,并命名
    $ git remote add [remote-name] [url]
    
    # 删除指定的远程仓库
    $ git remote remove [remote-name]
    
    # 取回指定远程仓库的变化,并与本地分支合并
    $ git pull [remote-name] [branch-name]
    
    # 上传本地指定分支到指定远程仓库
    $ git push [remote-name] [branch-name]
    
    # 推送所有分支到指定远程仓库
    $ git push [remote-name] --all
    
    # 强行推送当前分支到指定远程仓库,即使有冲突
    $ git push [remote-name] --force
    

    撤销修改

    # 恢复暂存区指定文件到工作区( 没"--",就变成切换分支了 )
    $ git checkout -- [file]
    
    # 恢复某个commit的指定文件到暂存区和工作区
    $ git checkout [commit-id] [file]
    
    # 恢复暂存区的所有文件到工作区
    $ git checkout .
    
    # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
    $ git reset [file]
    
    # 重置暂存区与工作区,与上一次commit保持一致
    $ git reset --hard
    
    # 暂时将未提交的变化移到堆栈中(工作区变干净)
    # 从堆栈中恢复最新的存储,--index 重新应用之前被暂存的变更
    $ git stash
    $ git stash pop [--index]
    

    版本回退

    # 新建一个commit,用来撤销指定的commit(commit历史会保留)
    # 后者的所有变化都将被前者抵消,并且应用到当前分支
    $ git revert [commit-id]
    
    # 回滚到commit-id,将该commit-id之后的commit都删除。(commit历史会被删掉)
    # --hard 重设暂存区和工作区
    $ git reset --hard [commit-id]
    

    抄自阮一峰老师的博文:常用 Git 命令清单

    基本没啥自己整理的,该篇博文权当自己备忘用。

    相关文章

      网友评论

          本文标题:Git常用操作

          本文链接:https://www.haomeiwen.com/subject/taijsxtx.html