git 常用命令行

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

新建代码库

# 在当前目录新建一个Git代码库
$ git init

# 下载一个项目和它的整个代码历史
$ git clone [url]

# 下载指定SHA分支
$ git clone -b <branch-name> url

config 配置

在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:

  • 仓库级别 local 【优先级最高】
  • 用户级别 global【优先级次之】
  • 系统级别 system【优先级最低】

查看配置项目

# 查看所有的配置信息
$ git config -l 

# 查看仓库配置
$ git config --local -l

# 查看用户配置
$ git config --global -l

# 查看系统配置
$ git config --system -l

添加配置项目

$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"

编辑配置项目

# 编辑配置文
$ git config -e

# 编辑仓库级别配置文件
$ git config --local -e

# 编辑用户级别配置文件
$ git config --global -e

# 编辑系统级别配置文件
$ git config --system -e

增加/删除文件(从工作区到暂存区)

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有文件到暂存区
$ git add .

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

代码提交(从暂存区到本地仓库区)

# 提交暂存区到仓库区
$ git commit -m [message]
 
# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

代码推送(从本地仓库区到远程仓库)

# 将本地分支推送到与之存在追踪关系的远程分支
$ git push origin master

# 删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支
$ git push origin :refs/for/master
$ git push origin –delete master

分支

# 本地所有分支
$ git branch

# 远程所有分支
$ git branch -r

# 本地及远程所有分支
$ git branch -a

# 新建一个分支,但不切换
$ git branch [branch-name]

# 新建一个分支,指向指定commit
$ git branch [branch] [commit]

# 拉取远程分支至本地,同时在本地创建对应的分支
$ git checkout -b [branch] origin/[remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建SHA版本分支,并切换至该分支
$ git checkout [SHA]

# 为新的SHA分支重命名
$ git checkout -b <new-branch-name>

# 分支合并 (将 [branch-name] 合并至 master)
$ git checkout master
$ git merge [branch-name]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

标签 tag

# 列出所有tag
$ git tag
 
# 新建一个tag在当前commit
$ git tag [tag]
 
# 新建一个tag在指定commit
$ git tag [tag] [commit]
 
# 删除本地tag
$ git tag -d [tag]
 
# 删除远程tag
$ git push origin :refs/tags/[tagName]
 
# 查看tag信息
$ git show [tag]
 
# 提交指定tag
$ git push [remote] [tag]
 
# 提交所有tag
$ git push [remote] --tags
 
# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

查看信息

# 显示有变更的文件
$ git status

# 查看历史版本SHA
$ git log

远程同步

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

撤销

# 恢复暂存区的指定文件到工作区
$ git checkout [file]
 
# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]
 
# 恢复暂存区的所有文件到工作区
$ git checkout .
 
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]
 
# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard
 
# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]
 
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]
 
# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]
 
# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]
 
# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

内容来源

posted @ 2022-04-20 10:20  槑孒  阅读(30)  评论(0编辑  收藏  举报