前端工程师必备的Git命令和使用场景
前端工程师必备的Git命令和使用场景
我想把本地的一个项目文件夹(非空文件夹)推送到我的git仓库
$ cd local-project path # 进入本地项目文件夹 $ git clone <git-url> tmp # 克隆新建的git仓库到本地项目文件夹的 tmp目录 $ mv tmp/.git . # 将.git 文件移动到本地项目文件夹根目录 $ git reset --hard HEAD # 是将工作区、暂存取和HEAD保持一致 复制代码
Tips:
git reset HEAD
与git reset --hard HEAD
的区别git reset HEAD 是将咱暂存区和HEAD的提交保持一致
git reset --hard HEAD 是将工作区、暂存区和HEAD保持一致
我想克隆一个Git项目
克隆一个项目和他的整个代码史
$ git clone <url> 复制代码
克隆一个项目的某一个分支
$ git clone -b <branchname> <url> 复制代码
Git仓库改名了, 我本地拉取的代码该如何处理?
修改本地仓库名称与代码仓库名称一致
mv local-git-name new-git-name
将本地仓库和远程仓库对接
$ cd new-git-name $ git remote set-url origin <git-url> # 设置远程仓库地址 $ git remote -v # 查看结果 确认~ 复制代码
每次提交代码都要输入用户名密码,怎么省略掉~
修改 ssh协议
$ git remote rm origin $ git remote add origin git@github.com:project/project.git $ git push -u origin master 复制代码
接手项目我想基于master分支创建一个我自己的Feature分支, 我该怎么做?
$ git branch # 查看当前分支 $ git checkout master # 如果不是master分支,使用此命令切换, 如果是master分支则忽略该命令 $ git pull origin master # 拉取远程最新的 master分支代码 $ git push origin HEAD:feature/your-feature-name # 基于master分支(当前代码)创建远程feature分支 $ git checkout -b feature/your-feature-name origin/feature/your-feature-name # 创建本地feature分支并与远程分支同步~ 复制代码
我想提交我的代码
workspace: 工作区
Index /Stage: 暂存区
Repository: 仓库区 (本地仓库)
Remote: 远程仓库
基本命令
$ git add . # 将修改的文件提交到暂存区 $ git commit -m"提交内容描述" # 提交暂存区到仓库区 $ git push # 将代码提交到远程仓库 复制代码
git add 详解 暂存区
# 添加当前目录的所有文件到暂存区 $ git add . # 添加指定文件到暂存区 $ git add [file1] [file2] ... # 添加指定目录到暂存区 $ git add [dir] # 添加前确认变化 $ git add -p # 删除工作区文件并将这次删除放入暂存区 $ git rm [file] / [dir] # 仅在暂存区中该删除但是本地保留 $ git rm --cahed [file] / [dir] # 修改文件名并提交到暂存区 $ git mv [file-original-name] [file-renamed] 复制代码
git commit 详解 仓库区
# 提交暂存区到仓库区 $git commit -m "commit description" # 提交暂存区指定文件到仓库区 $ git commit [file1] [file2] ... -m "commit description" # 提交工作区自上次commit之后的所有变化直接到仓库区,相当于省略了 git add . $ git commit -a $ git commit -am"commit description" # 等同于 git add. && git commit -m"msg" # 提交时查看所有diff信息 $ git commit -v # 如果我的提交信息(提交的内容描述 message)写错了, 我该怎么处理? $ git commit --amend -m"re commmit description" # 改写上一次commit的提交信息 # 重做上一次commit,并包括指定文件的新变化 git commit --amend [file1] [file2] ... 复制代码
关于分支操作的常用命令
我想查看我所有的本地分支
git branch
我想查看所有的远程分支
git branch -r
我想既看本地分支又看远程分支
git branch -a
新建分支但不切换
git branch [branch-name]
新建分支并切换到该分支
git checkout -b [branch-name]
新建一个分支,并与指定远程分支建立追踪管理
git branch --track [branch] [remote-branch]
切换到指定分支
git checkout [branch-name]
切换到上一个分支
git checkout -
在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
我想合并指定分支到当前分支
git merge [branch]
选择一个commit合并到当前分支
存在该场景,甲乙两人分别开发一个模块,甲开发完一个功能但是测试未完成还不能合并到master,此时乙需要使用甲commit的代码
git cherry-pick [commit]
删除分支
git branch -d [branch-name]
删除远程分支
git push origin --delete [branch-name]
git branch -dr [remote/branch]
作者:墩墩大魔王丶
链接:https://juejin.cn/post/7012867754315218981