2022-05-05 17:17:35 +08:00
|
|
|
|
## 初始化key
|
|
|
|
|
ssh-keygen
|
|
|
|
|
|
2018-07-25 09:55:58 +08:00
|
|
|
|
## 强制回滚到本地的某次历史commit
|
2016-10-12 21:34:21 +08:00
|
|
|
|
git reset --hard <commit-hash>
|
2016-03-01 15:04:25 +08:00
|
|
|
|
## 远程也进行强制同步
|
|
|
|
|
git push -f origin <branch>
|
2018-07-25 09:55:58 +08:00
|
|
|
|
|
2016-06-10 09:36:28 +08:00
|
|
|
|
## 设置用户名
|
|
|
|
|
git config user.name "Aborn Jiang"
|
|
|
|
|
git config --global user.name "Aborn Jiang"
|
2018-04-06 11:39:44 +08:00
|
|
|
|
git config --global user.email loveaborn@foxmail.com
|
2016-06-10 09:36:28 +08:00
|
|
|
|
git config --list
|
2018-07-25 09:55:58 +08:00
|
|
|
|
## 设置用户名(只对当前项目有效),注意切到项目根目录
|
2021-01-29 16:01:24 +08:00
|
|
|
|
git config user.name "aborn"
|
2018-07-12 10:06:09 +08:00
|
|
|
|
git config user.email loveaborn@foxmail.com
|
|
|
|
|
|
2022-04-14 16:26:03 +08:00
|
|
|
|
## 三才相关
|
|
|
|
|
git config user.name "jiangguobao"
|
|
|
|
|
git config user.email jiangguobao@sancaiwulian.com
|
|
|
|
|
|
2018-07-25 09:55:58 +08:00
|
|
|
|
## 初始化一个空项目到本地git仓库
|
2016-06-12 11:09:11 +08:00
|
|
|
|
git init
|
|
|
|
|
git add .
|
|
|
|
|
git commit -m "Initial commit"
|
2016-11-19 23:45:00 +08:00
|
|
|
|
## 删除远程分支
|
|
|
|
|
git push origin :<branch_name> # v1.5+
|
|
|
|
|
git push origin --delete <branch_name> # v1.7+
|
2023-07-10 13:15:38 +08:00
|
|
|
|
git branch --delete dev # 删除本地分支
|
2016-11-19 23:45:00 +08:00
|
|
|
|
|
2016-06-12 11:09:11 +08:00
|
|
|
|
## 与远端关联
|
|
|
|
|
git remote add origin <remote-git-url>
|
2016-11-20 10:43:10 +08:00
|
|
|
|
git config --get remote.origin.url # 获得远端git地址
|
2016-06-12 11:09:11 +08:00
|
|
|
|
# example: git remote add origin git@code.dianpingoa.com:mobile/knb-ua-kit.git
|
|
|
|
|
git push --set-upstream origin master
|
2018-07-25 09:55:58 +08:00
|
|
|
|
# 如果与远端有了关联,则直接git pull就好
|
2016-06-12 11:09:11 +08:00
|
|
|
|
git pull origin master
|
2016-12-15 10:44:29 +08:00
|
|
|
|
# 如果本地已经Commit过,可能会报如下错误:
|
|
|
|
|
# fatal: refusing to merge unrelated histories
|
|
|
|
|
# 请执行
|
|
|
|
|
git pull origin master --allow-unrelated-histories
|
2016-06-12 11:09:11 +08:00
|
|
|
|
|
|
|
|
|
## 撤销add
|
|
|
|
|
git reset <filename>
|
2018-09-21 16:30:57 +08:00
|
|
|
|
## reset single file
|
|
|
|
|
git checkout HEAD -- my-file.txt
|
2022-05-17 21:32:46 +08:00
|
|
|
|
git checkout -- components/TopBar.vue
|
2021-01-29 16:01:24 +08:00
|
|
|
|
git reset HEAD~1 ## 撤销上次commit,保留文件的改变,只将指针调到上一次
|
2016-06-24 10:03:56 +08:00
|
|
|
|
|
2016-07-05 10:35:28 +08:00
|
|
|
|
## github 的pull/request前先同步上流源
|
2016-06-24 10:03:56 +08:00
|
|
|
|
## github fork and sync upstream
|
|
|
|
|
# 如果没有upstram,添加之
|
|
|
|
|
git remote add upstream https://github.com/whoever/whatever.git
|
|
|
|
|
# example
|
|
|
|
|
git remote add upstream https://github.com/melpa/melpa.git #git@github.com:melpa/melpa.git
|
|
|
|
|
# 拉取所有远端,同步之
|
|
|
|
|
git fetch upstream
|
|
|
|
|
# 切到master
|
|
|
|
|
git checkout master
|
|
|
|
|
# rebase
|
|
|
|
|
git rebase upstream/master
|
2016-09-06 19:35:47 +08:00
|
|
|
|
|
2017-03-27 12:29:50 +08:00
|
|
|
|
# 当rebase发生冲突时怎么处理?
|
|
|
|
|
git rebase master
|
|
|
|
|
git add .
|
|
|
|
|
git rebase --continue
|
|
|
|
|
|
2016-09-06 19:35:47 +08:00
|
|
|
|
# pull request ID为PR的ID
|
|
|
|
|
git fetch origin pull/ID/head:BRANCHNAME
|
|
|
|
|
|
|
|
|
|
# git fetch origin pull/157/head:B157
|
|
|
|
|
# git checkout B157
|
2017-04-19 14:17:55 +08:00
|
|
|
|
|
|
|
|
|
# 查看单个文件的修改历史
|
|
|
|
|
git log -p filename
|
2017-04-19 14:23:57 +08:00
|
|
|
|
|
|
|
|
|
# git 切换到tag
|
|
|
|
|
git checkout tag_name
|
|
|
|
|
# 注意: tag 相当于是一个快照,是不能更改它的代码的,如果要在 tag 代码的基础上做修改,你需要一个分支:
|
|
|
|
|
git checkout -b branch_name tag_name
|
2017-06-01 15:07:31 +08:00
|
|
|
|
|
|
|
|
|
# git打tag
|
|
|
|
|
git tag v0.3.4
|
|
|
|
|
# 将tag推送到远端
|
|
|
|
|
git push origin v0.3.4
|
|
|
|
|
# 显示tag列表
|
|
|
|
|
git tag -l
|
2018-07-30 15:36:10 +08:00
|
|
|
|
# 删除tag
|
|
|
|
|
git tag -d tagName
|
|
|
|
|
# 删除远端tag
|
|
|
|
|
git push origin :refs/tags/tagName
|
|
|
|
|
# 或者
|
|
|
|
|
git push --delete origin tagName
|
|
|
|
|
|
2018-01-04 15:18:34 +08:00
|
|
|
|
|
|
|
|
|
## 删除所有commit,仅保留当前文件
|
|
|
|
|
# 1. Checkout
|
|
|
|
|
git checkout --orphan latest_branch
|
|
|
|
|
# 2. Add all the files
|
|
|
|
|
git add -A
|
|
|
|
|
# 3. Commit the changes
|
|
|
|
|
git commit -am "re-init"
|
|
|
|
|
# 4. Delete current master branch
|
|
|
|
|
git branch -D master
|
|
|
|
|
# 5. Rename the current branch to master
|
|
|
|
|
git branch -m master
|
|
|
|
|
# Finally, force update your repository
|
|
|
|
|
git push -f origin master
|
2018-06-19 18:54:44 +08:00
|
|
|
|
|
|
|
|
|
# 查看commitA到commitB之间的diff
|
|
|
|
|
git diff commitA^ commitB
|
|
|
|
|
# 查看某一次的commit的diff,下面两个命令都可以实现
|
|
|
|
|
git diff commit^
|
|
|
|
|
git show commit # 更优
|
2019-10-18 11:31:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 清理不必要的文件,优化本地Rep
|
|
|
|
|
git gc --prune=now
|
|
|
|
|
# 删除所有远端在orgin下所有无用的分支
|
|
|
|
|
git remote prune origin
|
2022-05-28 22:27:58 +08:00
|
|
|
|
|
|
|
|
|
# 本地一个文件不再更新到远程,可以达到本地和远程不一样的效果
|
|
|
|
|
git update-index --skip-worktree path/to/your/file.txt
|
2022-06-27 19:15:54 +08:00
|
|
|
|
|
|
|
|
|
# 反向操作
|
|
|
|
|
git update-index --no-skip-worktree path
|