git本地仓库基本操作:
工作区:git 所在当前文件夹
暂存区:仓库与工作区之间缓冲区
记录区:理解为仓库,记录提交版本
# git command
git add . # 添加工作区改动到暂存区
git status # 查看暂存区状态
git commit -m 'commit message' # 将暂存区代码提交到记录区
git commit --amend # 直接提交内容到上一次提交信息中,不产生新的提交记录
分支合并:
首先:
git log # 查看历史提交信息
99471@DESKTOP-EMDB8I8 MINGW64 ~/Desktop/SID验收/master分支/mindspore (master)
$ git log
commit 91dfcb765ec7e815c3ea399002cb478e022bef57 (HEAD -> master)
Author: despicablemme <yuhang-guo@foxmail.com>
Date: Mon Jul 5 09:50:49 2021 +0800
fix train_sony.py
commit 50c1f68697f2791b652a654c518a79a261ce9fba (origin/master, origin/HEAD)
Author: despicablemme <yuhang-guo@foxmail.com>
Date: Thu Jun 10 10:42:36 2021 +0800
name readme name cn
commit c5b896b3289d12401c8c91b59bf58bfae1910ff3
Merge: fd9667ee87 cb8d7efc6d
Author: i-robot <huawei_ci_bot@163.com>
Date: Thu Jun 10 10:03:19 2021 +0800
!17980 fix example result of CTCLoss, BNTrainingUpdate and add validation of ResizeBilinear.
Merge pull request !17980 from wangshuide/wsd_master
要合并自己编辑的前两个commit:
git rebase -i commit_id # 将此id之前的commit合并
会弹出选择哪个进行被合并,哪个保留(合入):
pick 50c1f68697 name readme name cn
pick 91dfcb765e fix train_sony.py
# Rebase c5b896b328..91dfcb765e onto c5b896b328 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# 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.
pick选择保留的commit ,将要合并的commit前面的pick修改为squash,保存退出::
pick 50c1f68697 name readme name cn
squash 91dfcb765e fix train_sony.py
之后会弹出对合并后的commit保留一个提交信息,将其他信息注释只保留这次commit的信息即可:
# This is a combination of 2 commits.
# This is the 1st commit message:
# name readme name cn
# This is the commit message #2:
fix train_sony.py # 新的提交信息
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
完成,再次查看git log:
99471@DESKTOP-EMDB8I8 MINGW64 ~/Desktop/SID验收/master分支/mindspore (master)
$ git log
commit 7106c0f24d11fd1ca582614e16dc4881cc67bbc0 (HEAD -> master)
Author: despicablemme <yuhang-guo@foxmail.com>
Date: Thu Jun 10 10:42:36 2021 +0800
fix train_sony.py
commit c5b896b3289d12401c8c91b59bf58bfae1910ff3
Merge: fd9667ee87 cb8d7efc6d
Author: i-robot <huawei_ci_bot@163.com>
Date: Thu Jun 10 10:03:19 2021 +0800
!17980 fix example result of CTCLoss, BNTrainingUpdate and add validation of ResizeBilinear.
Merge pull request !17980 from wangshuide/wsd_master
以次数合并仓库
git rebase -i HEAD~3 # 合并最近的三次提交
注意合并后再push到远程仓库时,因为合并后版本低于远程仓库,需要使用git push -f ... 强制推送
远程
仓库操作:
git clone "远程仓库地址链接" # 从远程仓库克隆完整仓库到本地,对该仓库操作需进入其文件夹
git pull origin master # 从远端仓库拉取代码与本地合并
# origin:仓库地址 master:对应分支
git push origin master # 将本地仓库推入远程仓库
git checkout -b newbranch # 新建分支并跳转至此分支
# 此时工作区会转移至此分支内容
git reset --hard origin/master # 将本地重设为与远端同步
# 撤销本地、暂存区、版本库
# (用远程服务器的origin/master替换本地、暂存区、版本库)
git fetch --all # 需要将这些更新取回本地,这时就要用到git fetch命令
# git强制覆盖本地命令(单条执行):
git fetch --all && git reset --hard origin/master && git pull
将仓库内容同步最新:
git add upstream https:(链接) -t branch(对应分支) # 添加要同步仓库的对应分支
# 不-t对应分支的话fetch会全部拉下来
git fetch upstream # 拉取分支的最新内容
git rebase upstream/branch # 将当前分支更新,同步到目标分支最新
# 记得切换到要操作的分支 git checkout ...
远程仓库项目管理:
维护同一个项目:
fork 基本仓库
在自己代码仓修改
提交PR
合入
同时提交多个PR:
在本地与自己的远程仓库新建分支,要对应名称
从基本仓库拉取代码与基本仓库同步
修改,push到自己的远程仓库对应新建分支
从分支提交PR
未完待续。。。