怀念夏天 发表于 2025-2-24 21:31:57

Git命令



git config

全局设置用户名、密码、邮箱需要用 --global参数
不加--global可以某个git堆栈单独设置用户名、密码、邮箱
# 1.查看git所有的配置信息
git config --list

# 2.查看git用户名、密码、邮箱的配置
git config user.name
git config user.password
git config user.email

# 3.设置git用户名、密码、邮箱的配置
git config user.name "myname"
git config user.password "123456"
git config user.email "1234321@qq.com"

# 3.设置git用户名、密码、邮箱的配置(全局配置)
git config --global user.name 用户名
git config --global user.name littleCat
git config --global user.password 密码
git config --global user.password abc123
git config --global user.password 邮箱
git config --global user.email "1234321@qq.com"

# 4.修改git用户名、密码、邮箱的配置
# 覆盖法
git config user.name "xiaoming"
git config --global user.name "xiaobai"   #修改全局配置
# 替换法
git config --global --replace-all user.name "xiaohong"

# 5.删除git用户名、密码、邮箱的配置
git config --global --unset user.name "xiaoming"
git config --global --unset user.email "1234321@qq.com"
git config --global --unset user.password "abc123" git init

初始化一个git堆栈,是在本地新建了一个git堆栈
git init #在当前目录下初始化一个git仓库

git init <file_directory> #在当前目录下新建file_directory目录,然后在该目录下初始化一个git仓库 git clone

克隆一个git堆栈,是从远程堆栈克隆到本地的git堆栈
git clone <url> #在当前目录下拉取远程仓库的代码
如:git clone git@gitee.com:huoshanmao/test.git,会在当前目录下建一个test的文件夹,里面有代码文件以及git的代码修改提交历史 git add、git rm、git mv

git add <file1> <file2> ... #添加多个指定文件到暂存区

git add <directory_name> ... #添加指定目录到暂存区

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

git add --all #添加当前目录下所有文件到暂存区

git rm <file1> <file2> ... #删除工作区指定文件,并将这次删除多个指定文件放到暂存区

git mv #改名文件,并且将这个改名放入暂存区 git commit

git commit -m <message> #添加暂存区到本地仓库区,并且添加message的提交说明

git commit <file1> <file2> ... -m <message> #添加暂存区的指定文件到本地仓库区

git commit --amend -m <message1> #使用新的commit替代原来的commit,如果文件没变化,则只是修改提交说明

git commit --amend <file1> <file2> ... #使用新的commit替代原来的commit git push

git push #推送到远程仓库,默认是origin源的当前分支

git push <remote_name> <branch_name> #推送到远程仓库的branch_name分支
如:git push -u origin "master",第一次添加时加上-u的参数

git push <remote_name> <commit_id>:<branch_name> #推送到远程仓库的branch_name分支,只是提交某一次commit_id的记录,而不会推送所有的commit记录到远程
如:git push origin 8c180a9:master git push <remote_name> --force #强行推送当前分支到远程仓库,即使有冲突 git push <remote_name> --all #推送所有分支到远程仓库 git pull

git pull #取回远程仓库origin的变化,并于当前分支合并,如果有冲突会标记出来

git pull <remote_name> <branch_name> #取回远程仓库的变化,并与本地分支合并,相当于git fetch和git merge的结合 git fetch

git fetch <remote_name> #下载远程仓库的所有变动(远程新增或删除分支都能显示) git status

git status #查看状态,包括所有变更的文件,工作区、暂存区、仓库区的状态变化 git branch

git branch #列出所有本地分支

git branch -r #列出所有远程分支

git branch -a #列出所有本地分支和远程分支

git branch <branch_name> #新建一个分支,但依然停留在当前分支

git branch --track <branch_name> <remote_name/branch_name> #新建一个分支,与指定的远程分支建立追踪关系,但还处于当前分支
如:git branch --track feat2 origin/feat2

git branch --set-upstream <branch_name> <remote_branch_name> #建立追踪关系,在现有分支与指定的远程分支之间

git branch -d <branch_name> #删除本地分支

git push origin --delete <branch_name> #删除远程分支
git branch -dr <remote_name/branch_name> #删除远程分支 git checkout

git checkout -b <branch_name> #以当前分支为基底,新建一个分支,并切换到该分支,要跟踪远程分支还需要pull命令或者--set-upstream

git checkout <branch_name> #切换到指定分支,并更新工作区

git checkout - #切换到上一个分支 git switch

git switch -c <branch_name> #新建一个分支,并切换到该分支

git switch <branch_name> #切换到指定分支,并更新工作区

git switch - #取消刚刚新建一个分支 git merge

git merge <branch_name> #合并指定分支branch_name到当前分支 git cherry-pick

git cherry-pick <commit_id> #选择一个commit,合并进当前分支 git remote

git remote -v #查看远程关联的仓库

git remote add <remote_name> <url> #添加远程名为remote_name的源,并且对应url地址

git remote set-url -- <remote_name> <new_url> #设置远程名为remote_name的源的url

git remote get-url -- <remote_name> #查看远程名为remote_name的源的url
如:git remote get-url --all origin,可以获得远程仓库origin源的URL地址

git remote show <remote_name> #显示某个远程仓库的信息 git log

git log #查看每一次提交的版本及commit_id git reflog

git reflog #查看所有分支的所有操作记录,包括commit、reset操作,也可以看到被删除的commit记录 git diff

git diff#比较工作区和暂存区的差异

git diff <file_name>#比较指定文件工作区和暂存区的差异

git diff --cached#比较暂存区和上一次提交(commit)的差异

git diff --cached

git diff --staged #比较指定文件暂存区和上一次提交(commit)的差异 git reset

git reset #回退到上一次的commit,并且将差异保存在工作区

git reset --hard <commit_id> #回退到某一次提交的版本,该commit之后的提交内容,工作区和暂存区的内容都被抹掉

git reset --soft <commit_id> #回退到某一次提交的版本,该commit之后的提交内容,保留工作目录,并把重置 HEAD 所带来的新的差异放进暂存区

git reset 或 git reset --mixed <commit_id> #不带参数,或带参数–mixed(默认参数),与git reset --soft 不同,它将会把差异放到工作区

git reset --hard origin/master #回退到远程的master分支的最新版本 git revert

#git revert 命令意思是撤销某次提交,它会产生一个新的提交,虽然代码回退了,但是版本依然是向前的

git revert HEAD #撤销最近一次提交

git revert HEAD~1 #撤销上上次的提交,注意:数字从0开始

git revert 0ffaacc #撤销0ffaacc这次提交 git rebase 

git rebase master feat1 #将feat1拉取分支的基底改成master分支最新提交记录为基底,并且合并feat1分支后面提交的记录到新基底上
#相当于 git checkout feat1 和 git rebase master 组合使用 git restore

git restore <file> #丢弃指定文件file在工作区的修改

git restore --staged <file>... #丢弃暂存区的文件修改,不暂存到暂存区,也就是恢复到工作区 git tag

git tag #列出所有tag

git tag <tag_name> #新建一个tag在当前commit

git tag <tag_name> <commit_id> #新建一个tag在指定commit

git tag -d <tag_name> #删除本地tag

git push origin:refs/tags/<tag_name> #删除远程tag

git show <tag_name> #查看tag信息

git push <remote_name> <tag_name> #提交指定tag

git push <remote_name> --tags #提交所有tag

git checkout -b <branch_name> <tag_name> #新建一个分支,指向某个tag git stash

git stash 会缓存:跟踪的但未添加到暂存区的文件(unstaged changes)、已经添加到暂存区的文件(staged changes)
不会缓存:新建的未跟踪的文件(untracked files)、被忽略的文件(ignored files)
git stash list   #列出当前所有的储存文件记录

git stash#保存当前的工作区与暂存区的状态,把当前的修改的保存到git栈,等以后需要的时候再恢复,git stash 这个命令可以多次使用,每次使用都会新加一个stash@{num} num是编号

git stash save '注释'#作⽤等同于git stash,区别是可以加⼀些注释, 执⾏存储时,添加注释,⽅便查找
如:git stash save 'test'

git stash pop#默认恢复git栈中最新的一个stash@{num},建议在git栈中只有一条的时候使用,以免混乱,该命令将堆栈中最新保存的内容删除

git stash apply#将堆栈中的内容恢复到当前分支下。这个命令不同于 git stash pop。该命令不会将内容从对堆栈中删除,也就是该命令能够将堆栈的内容多次运用到工作目录,适合用与多个分支的场景
如:git stash apply stash@{2}

git stash drop#从堆栈中移除指定的stash
如:git stash drop stash@{3}

git stash clear#移除全部的stash

git stash show#查看堆栈中最新保存的stash和当前⽬录的差异

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Git命令