前端开发中必用git命令

1.首先要知道以下概念:

在这里插入图片描述


Workspace:工作区
Index / Stage:暂存区
Repository:仓库区(或本地仓库)
Remote:远程仓库


2.必需掌握的git命令

git clone xxx  
> 克隆项目到本地
git pull --rebase
> 取回远程仓库的变化,并与本地分支合并(**推荐使用!!!
每天上班第一件事就是git pull --rebase 拉去master上
最新代码 有冲突解决冲突**)
> 注意⚠️:git pull = git fetch + git rebase
git pull [remote] [branch]
> 取回远程仓库的变化,并与本地分支合并(不推荐使用)
> 注意⚠️:git pull = git fetch + git merge
git push [remote] [branch]
> 上传本地指定分支到远程仓库
git init
> 在当前目录初始化为一个仓库
> 注意⚠️:**当前目录!!!** 不要把代码一小部分初始化成仓库呦~
git add .
> 将修改的文件添加到暂存区以便推送到远程仓库
git commit -m “feat: xxx"
> 提交暂存区到仓库区(我的习惯业务如果增添新功能加个feat: xxx)
git commit -m “fix: xxx"
> 提交暂存区到仓库区(如果线上紧急修复加个fix: xxx)
git branch
> 列出所有本地分支
git branch -r
> 列出所有远程分支
git branch -a
> 列出所有本地分支和远程分支
git checkout -b [branch]
> 新建一个分支,并切换到该分支
git checkout [已有的本地branch-name]
> 新建一个分支,并切换到该分支
git branch -d [branch-name]
> 删除本地分支
git push origin -d [branch-name]
> 删除远程分支
git tag
> 列出所有tag
git show [tag]
> 查看tag信息
git push [remote] [tag]
> 提交指定tag
git push [remote] --tags
> 提交所有tag
git fetch origin [remote]
> 下载远程仓库内容到本地

3.git 高阶操作

rebase解决冲突

使用rebase解决冲突

1.在本地master分支上 git pull --rebase 拉去远程最新代码到本地

2.切换到开发分支,进行 git rebase master 与master分支对比解决冲突

3.合并最近的 N 次提交纪录,执行:git rebase -i HEAD~4(本次开发过程我提交了4次commit,所以只需要把这4次的commit合成一个commit)

4.这时候,会自动进入 vi 编辑模式:(** vi模式基本操作 i 插入 esc退出编辑模式, :wq保存并退出 **)

p cacc52da feat: work done
s f072ef48 feat: indexeddb hack
s 4e84901a feat: add indexedDB floder
s 8f33126c feat: add test2.js

# Rebase 5f2452b2..8f33126c onto 5f2452b2 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

有几个命令需要注意一下:

p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like “squash”, but discard this commit’s log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit

选取 p cacc52da feat: work done 将三次修改的代码合并到第4次(即commit为cacc52da)的commit中 然后合并成最新的commit信息

如果有冲突就使用

git rebase --continue 
git rebase --skip
git rebase --abort

命令进行操作

如果有冲突最后别忘了 git add .
然后 git push origin 你的远程分支名
如果失败了使用 git push --force-with-lease origin feature即可

参考于 解决git rebase操作后推送远端分支不成功的问题

posted @ 2021-05-20 15:29  Web_ztz  阅读(278)  评论(0)    收藏  举报