1、本地提交代码
# git 初始化
$ git init
# 查看状态
$ git status
# 添加暂存区(二选一)
$ git add filename
$ git add *
# 从暂存区移除(二选一)
$ git rm --cached filename
$ git rm --cached *
# 提交本地库
$ git commit -m "message"
# 查看log
$ git reflog # 简易版
$ git log # 详细版
# 本地回到之前的版本,f2e0dca为id,git reflog 获得
$ git reset --hard f2e0dca
2、远程库相关操作
# 克隆远程仓
$ git clone https://gitee.com/lin-zhiqing/GitLearning.git
# 更新远程代码到本地
$ git pull
# push到远程仓库
$ git push
# 查看远程仓
$ git remote -v
# 添加远程仓
$ git remote add remoteName remoteURL
3、分支操作
# 查看所有分支(* 代表当前分支)
$ git branch -v
* main e8baf68 ccc
# 从当前分支拉取新分支
$ git branch branchName
# 切换分支
$ git checkout branchName
# 将指定分支合并到当前分支
$ git merge branchName
# push本地分支到远程仓库
$ git push remoteRepoName localBranchName
# pull远程分支到本地
$ git pull remoteRepoName localBranchName
合并代码冲突
# 合并时代码冲突
$ git merge master
Auto-merging ccc.txt
CONFLICT (content): Merge conflict in ccc.txt # ccc.txt 文件冲突
Automatic merge failed; fix conflicts and then commit the result.
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING) # MERGING
$ git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: ccc.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING)
$ cat ccc.txt
<<<<<<< HEAD
iiiiiiiiiiiii # HEAD 与===之间是本分支代码
jhjhj
=======
iiiiiiiiiiii # === 与 >>>> 之间是来源分支代码
tttttt
>>>>>>> master # 来源分支为master
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING)
$ vi ccc.txt # 修改冲突文件
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main)
$ cat ccc.txt
iiiiiiiiiiiii
tttttt
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING)
$ git add ccc.txt
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING)
$ git commit -m "gg" ccc.txt # 不能加文件名
fatal: cannot do a partial commit during a merge.
Administrator@DESKTOP-7CL5JI5 MINGW64 /d/1file/GitLearning/GitLearning (main|MERGING)
$ git commit -m "gg"
[main b03a7d1] gg