一、基本操作
创建Github账号,新建仓库 创建新本地仓库使用下列命令:
echo "# tse" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:xwrich/test.git
git push -u origin main
推送暂存仓库使用下列命令:
git remote add origin git@github.com:xwrich/tse.git
git branch -M main
git push -u origin main
二、分支版本控制操作
开发企业项目中在使用Git或者其它类似版本控制软件对项目版本进行管理时,多人合作的项目在开发时通常会在主干master上进行操作,而是重新开辟新的分支,在新的分支上进行开发、调试等操作,当项目调试通过时才会将分支项目的代码合并到主干上,这是实战中比较好的一种策略,特别是多人协同开发一个项目的情况下尤为重要。Git对于分支操作提供了以下基本命令:
命令 | 描述 |
---|
git branch | 查看所有分支,并且*号标记当前所在分支 | git branch name | 创建分支 | git checkout name 或git switch name | 切换分支 | git checkout -b name 或git switch -c name | 创建并切换到新建分支 | git branch -d name | 删除指定分支 | git merge name | 合并某分支到当前分支 | git push origin --delete [branch_name] | 删除远程分支 | git branch -D name | 强制删除分支 | git branch -m oldBranchName newBranchName | 修改分支名称 |
三、分支Push与Pull操作
相关命令
命令 | 描述 |
---|
git branch | 查看本地所有分支 | git branch -r | 查看远程所有分支 | git branch -a | 查看本地与远程的所有分支 | git push origin name | 推送本地分支到远程 | git push orgin :branchName | 删除远程分支(本地分支仍保留) | git checkout -b loclBranch origin/remoteBranch | 拉取远程指定分支并在本地创建分支 |
场景 公司有一个项目在远程仓库,主分支为main,一个分支为futures。你刚到公司,工作的电脑已经有了这个项目且为main分支,但是你并没有futures这个分支,那你需要通过远程仓库把这个futures的分支拉取到本地放到futures的分支中。(公司的项目都需要在分支上进行开发,经过开发、调试之后会合并到主分支,那么你需要将远程仓库中的futures分支拉取到本地仓库进行开发)。
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git fetch
From github.com:xwrich/test
* [new branch] futures -> origin/futures
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git checkout -b futures origin/futures
Switched to a new branch 'futures'
Branch 'futures' set up to track remote branch 'futures' from 'origin'.
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git branch
dev
* futures
main
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git branch -a
dev
* futures
main
remotes/origin/futures
remotes/origin/main
git fetch :将远程仓库的更新拉取到本地 gir fetch branchName :取回特定分支的更新,指定分支名 如拉取origin 主机的master 分支:git fetch origin master
四、本地分支操作冲突
开发中对不同分支下同一文件进行修改后执行合并时就会出现文件修改冲突情况,在此以本地分支操作冲突的解决办法
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git ls-files
hello.txt
temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git ls-files
hello.txt
temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git switch futures
Switched to branch 'futures'
Your branch is up to date with 'origin/futures'.
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git ls-files
hello.txt
temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git ls-files
hello.txt
temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git status
On branch futures
Your branch is up to date with 'origin/futures'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: temp.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git add temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git commit -m "futures分支修改"
[futures ad9f895] futures分支修改
1 file changed, 2 insertions(+)
Dock@Dock MINGW64 ~/Desktop/Spring (futures)
$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: temp.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git add temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git commit -m "main分支修改"
[main 2e0a70f] main分支修改
1 file changed, 2 insertions(+)
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git merge futures
Auto-merging temp.txt
CONFLICT (content): Merge conflict in temp.txt
Automatic merge failed; fix conflicts and then commit the result.
Dock@Dock MINGW64 ~/Desktop/Spring (main|MERGING)
$ cat temp.txt
分支操作
<<<<<<< HEAD
main分支修改操作
=======
分支内容测试
>>>>>>> futures
Dock@Dock MINGW64 ~/Desktop/Spring (main|MERGING)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(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: temp.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dock@Dock MINGW64 ~/Desktop/Spring (main|MERGING)
$ git add temp.txt
Dock@Dock MINGW64 ~/Desktop/Spring (main|MERGING)
$ git commit -m "main与futures分支的冲突解决"
[main 5656e40] main与futures分支的冲突解决
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ cat temp.txt
分支操作
main分支修改操作 && 分支内容测试
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git log --graph --pretty=online
fatal: invalid --pretty format: online
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git log --graph --pretty=oneline
* 5656e40c393330b37362c671d26efd668b0738d4 (HEAD -> main) main与futures分支的
冲突解决
|\
| * ad9f89582aeca40973e1c95358dceeff1dea4075 (futures) futures分支修改
* | 2e0a70f59d46027c20a5b93358e10ad9a3b28543 main分支修改
|/
* 528b0aee51733174144a41a2a95fcafdbace782e (origin/main, origin/futures) git分支提交操作
* a01a0e0e5609f36e419384bcc966ad500da7eac9 项目初始化
当合并分支时,提示合并存在冲突
Dock@Dock MINGW64 ~/Desktop/Spring (main)
$ git merge futures
Auto-merging temp.txt
CONFLICT (content): Merge conflict in temp.txt
Automatic merge failed; fix conflicts and then commit the result.
那么,打开本地项目的temp.txt文件,处理冲突后,重新添加到本地仓库
五、标签管理
标签操作基本命令git tag
命令 | 描述 |
---|
git tag tagName | 新建标签,默认为HEAD | git tag -a tagName -m 'xxx' | 添加标签并指定标签描述信息 | git tag | 查看本地所有标签 | git tag -d tagName | 删除本地标签 | git push origin tagName | 推送本地标签到远程仓库 | git push origin --tags | 推送全部未推送的本地标签到远程 | git push origin :refs/tags/tagsName | 删除一个远程标签 |
|