npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install [name]
安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明 Git 安装成功!
# 设置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
# 查看当前用户名和邮箱
git config user.name
git config user.email
git init
git add demo.txt 保存修改文件到暂存区
git commit -m '提交说明'
git status 查看仓库的状态
git diff 查看修改内容
alt + f8 清除窗口
git log 查看历史记录 git log --pretty=oneline整理的
cat filename 查看文件内容
git log 查看当前版本库及其之前的所有commit
git reflog 查看从本地仓库创建之日起,本地所进行的与项目更改有关的操作!比如说commit,clone等操作。
git reset --hard id 回滚到某个commid版本下
git reset --hard Head^ 回滚到上一个版本
git branch 查看分支,当前分支前面会标一个*号
git branch -a 查看所有分支,包括本地和远程的
git checkout -b branchName 创建并切换到新分支
git checkout branchName 切换分支
git branch -d branchName 删除某本地分支
git branch -D branchName 强制删除某本地分支
git merge branchName 合并某分支到当前分支
git remote -v 查看远程库的信息
git pull origin master 拉取代码到本地
git push origin master 提交修改到远程库
git branch -a (查看有几个分支)
git checkout -b 'aa' 创建并切换分支
git branch(查看当前正在哪个分支)
git push origin aa(将代码提交到该分支上)
git add .
git commit -m '创建分支'
git pull origin name(拉去分支最新信息)
git push origin name(提交分支信息)
git checkout master(切回主分支)
git push origin --delete <branchName>
git push origin --delete originBranch
git branch -m oldName newName
git push origin newName
git fetch origin
git checkout -b chy origin/chy
git fetch origin
git checkout -b chy origin/chy
git fetch origin
git checkout -b chy origin/chy
合并分支时添加注释信息(只适用于non-fast-forward方式, 该方式会生成新的commit。 fast-forward不会生成新的commit )
--no-ff -m" 本次合并添加的注释信息"
--no-ff :非快速合并 non-fast-forward
eg:合并 child 分支到 parent 分支
git fetch origin
git checkout -b child origin/child
本地检查代码,处理冲突
git checkout parent
git merge --no-ff child
git push origin parent
首先,不要 git add,commit。直接隐藏当前的修改
git stash
2.修改完成后,查看隐藏的列表
git stash list
3.如果需要恢复到之前的修改
git stash pop
1.先将自己的代码提交到远程
git add .
git commit -m 'xx'
git pull
git push
2.根据标签,切换到某个 commit 下
git reset --hard <id>
3.修复 bug....
4.提交修复结果
git add .
git commit -m 'xx'
git pull (这里一定要拉最新的代码)
git push
git tag v1.0 默认标签是打在最新提交的commit上的
git tag v0.9 6224937 在指定commit下打标签
git show v0.9 查看标签信息
git push origin tag <tagName> 提交标签到远程
git push origin --delete <tag tagName> 删除远程tag
将名为 old 的 tag 改为 new,可以通过执行以下命令来修改远程 tag 名称:
git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags
回滚到某次提交
git revert "commit id"
git push origin master -f
git add .
git commit --amend
git rebase --continue
本地分支
git branch -m oldName newName
远程分支
// 先重命名本地分支
git branch -m oldName newName
// 删除远程分支
git push --delete origin oldName
// 上传新命名的本地分支
git push origin newName
// 把修改后的本地分支与远程分支关联
git branch --set-upstream-to origin/newName
# 1.克隆A仓库
git clone ssh://git@github.com:xxxx/repo-A.git
# 2.添加B仓库为远程分支,设置别名other
git remote add other ssh://git@github.com:xxxx/repo-B.git
# 3.拉取B仓库最新信息到本地
git fetch other
# 4.以B仓库拉取的master分支作为基准,本地创建一个feature/merge的新分支
git checkout -b feature/merge other/master
# 5.切换回repo-A仓库的master分支
git checkout master
# 6.将feature/merge分支合并到repo-A仓库的master分支
git merge feature/merge --allow-unrelated-histories
在实际场景中,如果线上有 bug,我们一般会从 release 分支切 hotfix 分支进行处理。修复完 bug 后,该分支需要合并回 release,同时 develop 开发分支也需要该 commit。这就需要用到cherry-pick
了,操作如下:
- 切到A分支上
git checkout branchA
- 查看提交记录(也可以直接从gitlab/github的仓库commit历史中获取)
git log
- 切到
branchB
准备cherry-pick
比如这里我们希望合并记录中的commit_id为 e72jfhfji 的提交
git checkout branchB
- 使用cherry-pick合并A分支的指定commit到branchB
合并单个commit
git cherry-pick e72jfhfji
合并多个commit可以使用..
将两个commitid连接起来,表示合并(aaa, bbb]
区间的所有commit,注意区间是左开右闭
git cherry-pick aaa..bbb
如果是期望两边都是闭区间,我们可以使用^..
git cherry-pick aaa^..bbb
- cherry-pick时如果遇到冲突,我们需先解决冲突,然后执行
git add .
,然后继续执行git cherry-pick --continue
提交变更
git push origin branchB
git clone --mirror <旧仓库的git地址>
cd <克隆旧仓库的文件夹>
git remote set-url origin <新仓库的git地址>
git push -f origin
将 feature/search 合并到 master 分支
git checkout master
git pull origin master
git rebase feature/search
合并中遇到冲突,可通过git status
查看冲突文件,并解决。然后执行以下命令
git add . # 将修改后的冲突文件进行保存
git rebase --continue
执行完上述命令后,会进入一个vi
编辑界面,无需操作按esc
后输入wq
即可
执行完上述操作后,如果还有冲突,则继续解决,重复上述的步骤。直到最后一次rebase --continue
提示消息如下:
Successfully rebased and updated refs/heads/feature/search.
rebase 成功后,最后提交代码
git push origin master -f
补充:如果不想合并分支,通过git rebase --abort
退出
合并最近的 4 次提交记录
git rebase -i HEAD~4
or
git log
git rebase -i 第五次提交的commit_id # 注意git rebase -i 后面跟的commit_id不会参与合并,其只是一个标记
输入完以上命令后,会出现一个 vi 编辑页面,如下:
pick cacc52da feat(*): test1
pick f072ef48 feat(*): test2
pick 4e84901a feat(*): test3
pick 8f33126c feat(*): test4
# 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.
#
将需要合并的 commit 对应的pick
修改为s
或者squash
s cacc52da feat(*): test1
s f072ef48 feat(*): test2
s 4e84901a feat(*): test3
pick 8f33126c feat(*): test4
然后按esc
输入wq
保存
最后执行
git add .
git rebase --continue
git checkout commitId -b newBranch
git push origin newBranch
如果远程分支由master
修改为main
,将本地为master
的克隆分支同步成main
分支
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a