Git常用命令

发布于:2024-04-17 ⋅ 阅读:(26) ⋅ 点赞:(0)

Git常用命令

1.仓库
#当前目录新建一个代码库
git init
#新建一个目录,将其初始化为代码库
git init [exercise]
#拉取一个远程项目和整个代码历史
git clone [url]




2.配置
#显示git的配置
git config --list
#编写git的配置文件
git config -e [--global]
#设置提交时的用户信息
git config [--global] user.name "xwz"
git config [--global] user.email "88888888@email"




3.增加删除文件
#添加指定文件到暂存区
git add [file1] [file2]
#添加指定目录及子目录到暂存区
git add [dir]
#添加当前目录所有文件到暂存区
git add .

#删除工作区文件,并且将这次删除放入暂存区
git rm [file1]
#停止追踪文件,但会保留工作区
git rm --cache [file1]
#该名文件,并将这个文件放入暂存区
git mv [origin_name] [new_name]



4.代码提交
#提交暂存区到本地仓库
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.分支
#列出本地分支
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.标签
#列出所有标签
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.查看信息
#显示有变更的文件
git status
#显示当前分支的版本历史
git log 
#显示commit的历史,以及每次的发生变化的文件
git log --stat

#显示暂存区和工作区的差别
git diff
#显示暂存区和上一个commit的差别
git diff -cache [file]




8.远程同步
#显示所有远程仓库
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.撤销
#恢复 暂存区 某个文件到 工作区
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