Git常用命令

Git常用命令

1.仓库
shell 复制代码
#当前目录新建一个代码库
git init
#新建一个目录,将其初始化为代码库
git init [exercise]
#拉取一个远程项目和整个代码历史
git clone [url]
2.配置
shell 复制代码
#显示git的配置
git config --list
#编写git的配置文件
git config -e [--global]
#设置提交时的用户信息
git config [--global] user.name "xwz"
git config [--global] user.email "88888888@email"
3.增加删除文件
shell 复制代码
#添加指定文件到暂存区
git add [file1] [file2]
#添加指定目录及子目录到暂存区
git add [dir]
#添加当前目录所有文件到暂存区
git add .

#删除工作区文件,并且将这次删除放入暂存区
git rm [file1]
#停止追踪文件,但会保留工作区
git rm --cache [file1]
#该名文件,并将这个文件放入暂存区
git mv [origin_name] [new_name]
4.代码提交
shell 复制代码
#提交暂存区到本地仓库
git commit -m "这是第一次提交";
#提交暂存区的 指定文件 到本地仓库
git commit [file1] -m "这是一次指定文件提交" 
#提交自上一次commit的全部变化到本地仓库
git commit -a 

#提交时显示不同diff
git commit -v
#本次的commit代替上一次提交,如果代码没有改变,更改提交信息
git commit --amend -m "这是一个测试堵盖上一次提交的信息" 
#重做上一次commit,包括指定文件的变化
git commit --amend [file1]
5.分支
shell 复制代码
#列出本地分支
git branch
#列出远程分支
git branch -r
#列出所有分支(本地分支和远程分支)
git branch -a
#创建一个分支
git branch [branch_name]
#新建一个分支,并切换到该分支
git checkout -b [branch_name]
#新建一个分支,并指向commit
git branch [branch_name] commit
#新建一个分支,与指定远程分支建立追踪关系
git branch --track [bracnch_name] [remote_branch] 

#切换分支
git checkout [branch_name]
#切换到上一个分支
git checkout -
#现有分支和指定分支建立追踪关系
git branch --set-upstream [branch_name] [remote_branch]

#删除分支
git branch -d [branch_name]
#删除远程分支
git push origin --delete [branch_name]
git branch -dr [branch_name]
#合并指定分支到当前分支
git merge [branch_name]
6.标签
shell 复制代码
#列出所有标签
git tag 
#创建一个标签再当前commit
git 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_name] [tag] 
7.查看信息
shell 复制代码
#显示有变更的文件
git status
#显示当前分支的版本历史
git log 
#显示commit的历史,以及每次的发生变化的文件
git log --stat

#显示暂存区和工作区的差别
git diff
#显示暂存区和上一个commit的差别
git diff -cache [file]
8.远程同步
shell 复制代码
#显示所有远程仓库
git remote -v
#显示指定远程仓库信息
git remote show [remote]
#下载指定仓库
git fetch [remote]

#新增一个远程仓库
git remote add [name] [url]
#拉取远程仓库,并与本地分支合并
git pull [remote] [branch]

#代码推送到远程仓库
git push -u orgin master
#上传 本地指定分支 到 远程仓库
git push [remote] [branch]
#强行推送当前分支到远程仓库
git push [remote] --force
#推送本地所有分支到远程仓库
git push [remote] --all
9.撤销
shell 复制代码
#恢复 暂存区 某个文件到 工作区
git checkout [file]
#恢复 暂存区 所有文件到 工作区
git checkout .
#恢复某个 commit 的指定文件到 暂存区 和 工作区
git checkout [commit] [file]

#重置缓存区 与上次commit一样 但工作区不变
git reset [file]
#重置 工作区和 缓存区
git reset --hard

#重置当前分支 缓存区 为某次 commit,但工作区不变
git reset [commit]
#重置缓存区和工作区为某次commit
git reset --hard [commit]

#暂存与恢复
git stash
git stash pop
相关推荐
永洪科技6 小时前
永洪科技荣获商业智能品牌影响力奖,全力打造”AI+决策”引擎
大数据·人工智能·科技·数据分析·数据可视化·bi
weixin_307779136 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
大只鹅7 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
星源~7 小时前
tree 命令集成到 Git Bash:可视化目录结构的指南
git·单片机·物联网·嵌入式·项目开发
zhaqonianzhu8 小时前
git gerrit安装钩子
git·gerrit
这是个栗子8 小时前
【问题解决】VSCode终端中看不到Git-Bash
ide·git·vscode
悲伤小伞8 小时前
linux_git的使用
linux·c语言·c++·git
上海锝秉工控9 小时前
防爆拉线位移传感器:工业安全的“隐形守护者”
大数据·人工智能·安全
cv高级工程师YKY9 小时前
SRE - - PV、UV、VV、IP详解及区别
大数据·服务器·uv
天机️灵韵10 小时前
云效DevOps vs Gitee vs 自建GitLab的技术选型
git·开源项目