美文网首页
git常用命令

git常用命令

作者: 杨依_1 | 来源:发表于2020-12-17 14:43 被阅读0次

基本命令

初始化本地仓库

git init

clone仓库:

git clone codeAddress

配置

// 查看git配置本地或全局配置
git config --[local/global] --list

// 配置用户名和邮箱
git config --[lcoal/global] user.name 'yourName'
git config --[lcoal/global] user.email 'yourEmail'

// 重置配置
git config --[lcoal/global] --unset user.name

提交

git add .
git commit -m"commit description"

撤销提交

// 撤销add操作或

git restore --staged .  //撤销所有add的文件
git restore --staged filepath1 filepath2 [filePath3 ...] // 撤销add的指定文件

// 撤销本地最后一次commit操作【未提交到远程分支】
git reset HEAD^

// 撤销远程仓库某次提交【会保留本次撤销的操作记录】
git revert commitId
git add .
git commit -m"revert commitid"

删除文件

git rm filePath

pull代码

// git fetch + git merge FETCH_HEAD :会生成一次merge的提交记录。并保留合并之前的代码分支
//                    C - D                      C - D
// 合并前:           /             合并后:      /      \
//              A - B - E                 A - B - E - F
git pull
或
// git fetch + git rebase FECH_HEAD: 合并提交到主分支。不会生成额外的提交记录,也不保留合并前的提交分支。保持提交记录的线性展示:
//              C - D
// 合并前:      /             合并后: A - B -C - D - E
//             A - B - E                     
git pull --rebase

push代码

git push

冲突解决

// git pull --rebase 产生的冲突。
1. 在代码里解决冲突
2. git add .
3. git rebase --continue

// git merge 产生的冲突
1. 在代码里解决冲突
2. git add .
3. git merge --continue

查看提交记录

// 查看commit记录,显示commit的信息
git log

// 查看操作记录【可用于回退操作】
git reflog

分支操作

//在当前分支的基础上新建分支
git branch branchName
或
git checkout -b branchName
//删除分支
git branch -d branchName
// 查看本地分支
git branch -l
// 查看远程分支
git branch -r
// 查看所有分支
git branch -a
// 新建分支并关联远程分支
git branch --set-upstream localBranchName origin/remoteBranchName
// 关联本地分支到远程分支【在要关联的本地分支上操作】
git branch --set-upstream-to=origin/remoteBranchName
// 解除分支关联
git branch --unset-upstream
// 查看分支关联信息
git branch -vv

切换分支

// 切换到指定分支
git checkout branchName
// 切换到某个提交时的分支
git checkout commitID

代码合并

// 合并某个分支代码到当前分支
git merge branchName
// 合并其它分支的指定提交到当前分支
git cherry-pick commitID

查看远程分支地址

git remote origin show

submodule相关命令

// 给当前工程添加子模块
git submodule add toAddModuleRepository [--recurse-submodules]
// 子模块代码更新后同步到当前项目
git submodule update  [--recurse-submodules]

相关文章

网友评论

      本文标题:git常用命令

      本文链接:https://www.haomeiwen.com/subject/uicngktx.html