«

Git_相關指令

Paladin 发布于 阅读:251 oTher


環境配置

- ssh-keygen -t rsa -C "你的邮箱地址" #创建ssh key,邮箱可以随便输入,不会做校验
- git config --global user.name "你的Git提交昵称" #不添加`--global`表示在当前目录下的Git仓库设置,添加表示全局设置。配置Git账号信息,你所有的Git操作记录,都会显示为你设置的昵称
- git config --global user.email "你的Git提交邮箱"

仓库管理

- git init #初始化Git仓库
- git add . #添加所有文件
- git add 文件 # 添加指定文件到
- git commit -m "提交信息" #设置提交信息
- git remote add 远程仓库名(一般直接是origin) 远程仓库地址 #本地仓库设置一个远程仓库地址
- git remote set-url --add 远程仓库名 (一般直接是origin) 远程仓库地址 #该方式在执行git push时会自动同步所有远程仓库.
- git remote add 远程仓库名(不能重复) 远程仓库地址 #该方式在执行git push时,需要指定具体的远程仓库名
- git clone 远程仓库地址 #克隆仓库

记录操作

- git log #查看提交记录
- git status #查看git文件状态
- git log --graph --oneline #图形化显示当前分支的提交日志
- git log --graph --patch #图形化显示当前分支的提交日志及每次提交的变更内容
- git log --graph --oneline --all #图形化显示所有分支的提交日志
- git log --graph --patch --all #图形化显示所有分支的提交日志及每次提交的变更内容
- git show 提交hash值 #显示提交信息
- git show 分支名 #显示提交信息
- git show tag名 #显示提交信息

移除当前修改

- git checkout -- [file1] [file2] #抛弃指定文件的修改
- git restore [file1] [file2] #2.23版本开始,推荐使用如下命令
- git reset --hard #抛弃所有文件的修改
- git restore #抛弃所有文件的修改,2.23版本开始

暂存区操作

- git stash list #查看暂存区列表
- git stash apply #恢复暂存区最近的记录
- git stash apply stash@{n} #恢复暂存区指定记录
- git stash apply --index #暂存区申请使用时,出现冲突,将冲突解决后的信息同步到暂存区
- git stash drop stash@{0} #删除指定暂存区
- git stash pop #恢复并删除最近一次暂存区

文件操作

- git mv 原文件名 新文件名 #重名名文件
- git mv 原文件路径 新文件路径 #移动文件
- git mv 原目录 新目录 #移动目录
#git mv 可以添加参数--dry-run表示预览效果,而不是执行命令。推荐在使用Git来操作文件或者目录,而不是使用shell命令,这样可以保证在Git中的完整记录,也方便追溯、回退。

版本操作

- git tag #查看所有标签
- git tag -l "匹配模式" #指定查找模式
- git show 标签名 #查看标签信息
- git tag 标签名 #创建轻量级标签
- git tag -a 标签名 -m "标签信息" #创建带有记录信息标签
- git tag -d 标签名 #删除标签
- git push origin --tags #推送所有标签
- git push origin 标签名 #推送指定标签

分支操作

- git branch #查看本地分支
- git branch -a #查看所有分支 会显示本地和远程分支
- git branch -r #查看远程分支
- git log [branch-name] #查看某个分支的提交历史
- git branch [new-branch-name] #创建新分支 仅仅是基于当前分支创建新分支,不会切换到新分支。
- git checkout -b [new-branch-name] #创建并切换到新分支
- git checkout [branch-name] #切换到现有分支
- git branch -d [branch-name] #删除本地分支 这将删除本地分支,只有在该分支已经完全合并的情况下才能成功
- git branch -D [branch-name] #强制删除本地分支 强制删除指定的本地分支,不管是否已经合并
- git branch -m [old-branch-name] [new-branch-name] #重命名分支
- git branch --set-upstream-to [remote-branch] #设置跟踪分支、更新远程分支的本地引用
- git branch -vv #查看分支的上游(tracking)分支
- git merge [branch-name] #合并分支
- git push 远程仓库名 --delete [branch-name] #删除远程分支
- git fetch 远程仓库名 and git branch -r | grep -v '\->' | grep [branch-name] | cut -c 3- | xargs -n 1 git branch -d #拉取远程分支并删除本地分支
- git diff [branch1] [branch2] #比较两个分支的差异
- git log [branch-name] #列出某个分支的提交记录
- git reset --hard [commit-hash] #重置当前分支到特定 commit 保留当前更改
- git reset --soft [commit-hash] #重置当前分支到特定 commit 不保留当前更改
- git push [remote-name] [branch-name] #推送本地分支到远程仓库
- git push [remote-name] [branch-name]:[remote-branch-name] #推送本地分支并设置为远程跟踪分支
- git pull [remote-name] [branch-name] #拉取远程分支并自动创建本地分支
- git pull [remote-name] [branch-name] #拉取远程分支并合并到当前分支
- git log --graph --decorate --oneline --all #查看分支合并图

Git