这篇文章上次修改于 1631 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
最小配置
# 添加配置
git config [--local | --global | --system] user.name 'Your name'
git config [--local | --global | --system] user.email 'Your email'
# 查看配置
git config --list [--local | --global | --system]
# 区别
local:区域为本仓库
global: 当前用户的所有仓库
system: 本系统的所有用户
初始化项目
# 已有项目代码的情况
cd git_learning
git init
# 新建项目文件夹
git init YOUR_PROJECT
cd YOUR_PROJECT
设置项目本地用户,非git全局用户
git config --add --local user.name <YOUR_GIT_NAME>
git config --add --local user.email <YOUR_GIT_NAME>
建立本地分支
# 本地建立分支
git branch YOUR_LOCAL_BRANCH
# 基于远端创建本地分支同时切换到新建的分支
git checkout -b feature/add_git_commands origin/feature/add_git_commands
# 创建分支并切换到指定分支
## 第一种方式
$ git switch -c dev
## 第二种方式
$ git checkout -b dev
Switched to a new branch ‘dev'
# 查看所有分支包括远端分支:
git branch -av
# 切换分支:
git switch YOUR_BARACH
# 新建本地分支与远程分支关联:
git branch --set-upstream-to=origin/远程分支名 本地新建分支名,
例子:git branch --set-upstream-to=origin/lau-dev test
# 提交本地分支到远程仓库:
git push origin 本地分支名
切换分支:git checkout <name>或者git switch <name>
创建+切换分支:git checkout -b <name>或者git switch -c <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
场景
两个人开发同一个分支
修改同一个文件不同位置
这种场景下 Git 会自动帮助我们合并代码,我们不用担心合并冲突文问题。
# 每天上班时同步下代码
git pull
# 查看同步代码后的分支
git branch -av
# push 代码时遇到冲突建议先 fetch
git fetch
修改同一个文件相同位置
这种场景下 Git 会提示冲突,冲突的用户可以先和远端合并或者强制 pull 下代码,在人工审核冲突的地方,待解决冲突后再次 push 即可
# 每天上班时同步下代码
git pull
# 查看同步代码后的分支
git branch -av
# 当不同用户编辑相同文件的同一个位置后再 push 到远端时,后push 的一方会提示错误
git push
# 此时可以先尝试和远端合并,然后再次同步代码
git merge origin/master
git pull
# 此时打开提示冲突的文件,远端会提示冲突的地方,人工编辑冲突地方 fix 冲突即可,更改后需要再次查看状态,并根据提示操作
git status
修改文件名和文件内容后的操作
A 用户先修改 index.html 为 index.htm并没有修改文件内容,且提交至远端
B 用户没有更新到最新版代码,B 编辑了旧文件名的文件,编辑后并提交报错
~/PycharmProjects/git_learn master vim index.html
~/PycharmProjects/git_learn master ● git add .
~/PycharmProjects/git_learn master ✚ git commit -am "Add html tag"
[master 06ea515] Add html tag
1 file changed, 2 insertions(+)
~/PycharmProjects/git_learn master git push
To github.com:ucstone/git_learn.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'github.com:ucstone/git_learn.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
B 可以尝试进行拉取代码,git 能够帮助自动合并 代码解决问题。
git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 251 bytes | 251.00 KiB/s, done.
From github.com:ucstone/git_learn
59f0b80..88872a8 master -> origin/master
Merge made by the 'recursive' strategy.
index.html => index.htm | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.html => index.htm (100%)
将同一文件修改为不同的文件名如何处理
A 的操作如下
git mv index.htm index2.htm
git add .
git commit -am "Mv index.htm to index2.htm"
git push
B 的操作如下
git mv index.htm index1.htm
git add .
git commit -am "Mv index.htm to index1.htm"
git push
报如下错误
To github.com:ucstone/git_learn.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'github.com:ucstone/git_learn.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
✘ ~/PycharmProjects/git_learn master git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 253 bytes | 253.00 KiB/s, done.
From github.com:ucstone/git_learn
88872a8..057d115 master -> origin/master
CONFLICT (rename/rename): Rename "index.htm"->"index1.htm" in branch "HEAD" rename "index.htm"->"index2.htm" in "057d11558707d18858e1e48e9412c9b9920d3d70"
# 对策是删除 index2.htm 保留 index1.html
git rm index2.htm
git add .
git commit -am "Keep index1.htm, rm index2.htm"
[master 27a69dd] Keep index1.htm, rm index2.htm
# 这样就解决了冲突
git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 1016 bytes | 508.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:ucstone/git_learn.git
057d115..27a69dd master -> master
没有评论