别名
git alias定义
git config --global alias.st status
git config --global alias.br branch
git config --global alias.rlog reflog
git config --global alias.log "log --oneline --graph --decorate --all"
git config --global alias.co checkout
git config --global alias.gt tag
git config --global alias.lsr ls-remote --tags origin
git config --global alias.ft fetch --tags
git日常使用小tips
git log日期,永久修改
git config --global log.date iso8601
默认
修改后
安全配置
git config --global user.name "zhangsan"
git config --global user.email "zhangsan@a.com"
git commit --amend --author="zhangsan <zhangsan@a.com>"
使用别名
git st
git br
git rlog
分支
查看分支
# 本地所有分支
git br -a
# 远程所有分支
git br -v
拉取分支
拉取远程分支到本地,并创建本地分支
git co -b refactor_service_local origin/refactor_service
Branch 'refactor_service_local' set up to track remote branch 'refactor_service' from 'origin'.
Switched to a new branch 'refactor_service_local'
git pull
Already up to date.
git br -a
master
new-branch
newbr
refactor_service
* refactor_service_local
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/refactor_service
「通常首次从main切换为其他分支才需要、且进需要1次设置」
# 设置本地分支与远程仓库中对应分支的关联,
# 这样 Git 才能知道在执行如 `git pull` 或 `git push` 时与哪个远程分支进行交互
git branch --set-upstream-to=origin/fix_aibot_tip
branch 'remotes/origin/fix_aibot_tip' set up to track 'origin/fix_aibot_tip'.
git pull
## 查看本地分支和源端分支的跟踪信息
git branch -vv
Tag
Tag
#查看本地所有标签
git tag
#查看远程仓库所有标签
git ls-remote --tags origin
`存在于远程仓库中的标签或分支,而你的本地仓库尚未拉取这个更新,
需要先使用 `git fetch` 命令来更新本地引用列表。
例如,要拉取远程标签,可以使用 `git fetch --tags`。`
git fetch --tags
#切换到对应tag
git checkout v1.0.10
#删除本地tag
git tag -d v1.1.11
#删除远程tag
git push origin :refs/tags/v1.1.11
Tag提交
提交、打tag、推送提交
git commit -a -m "feat 引入opentracing、jaeger"
git tag -a v1.0.4 -m "feat 引入opentracing、jaeger"
# 推送指定的tag
git push origin --tags
# 推送所有
git push origin --all
git push origin master
查看文件修改 / 变动情况
git st -s
放弃变更
**放弃工作目录中的未提交变更**
git restore go.mod go.sum
**放弃暂存区中的变更**
git reset go.mod go.sum
Unstaged changes after reset:
M go.mod
M go.sum
标准的go私有package提交Git操作顺序如下
-
在你的本地仓库中做出一些更改。
-
使用
git add命令将这些更改添加到暂存区。 -
使用
git commit命令将这些更改提交到你的本地仓库。 -
使用
git tag命令在当前提交上打一个新的标签。 -
使用
git push命令将这些更改和新的标签推送到远程仓库。
这样,当你使用Go拉取代码的时候,它会拉取到最新的标签和对应的代码
Q&A
解决Please move or remove them before you switch branches
git co
remotes/origin/refactor_service
Please move or remove them before you switch branches.
Aborting
# 解决 Please move or remove them before you switch branches.
git clean -dfx
Removing .idea/
Removing .vscode/
Removing acmp-service
Removing assets/
Removing log/
Removing nohup.out
Removing service/billdbquery/
Removing service/settings/
Removing vendor/
# 若无效
git reset --hard
HEAD is now at 984f08b refactor:
解决git提交时You have divergent branches and need to specify how to reconcile them.
> git pull --tags origin master
From xxx.com:infra/uiam
* branch master -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
当运行 git pull 并看到类似的提示时,这意味着你的分支和远程分支之间存在分歧,Git 需要知道你希望如何合并这些变化。Git 提供了三种不同的策略来解决这种分歧:合并(merge),变基(rebase),或者仅当可以快速合并时才合并(fast-forward only)。
合并(Merge)
这是 Git 的默认策略,将远程的改动与本地的改动合并到一起,可能会创建一个新的合并提交。
可以通过以下命令设置为默认行为:
```
<BASH>
git config pull.rebase false
```
或者每次运行时指定:
```
<BASH>
git pull origin master --no-rebase
```
修改git默认编辑器,从nano到vim
git config --global core.editor "vim"
拉取并合并(merge)
git pull origin master --no-rebase