git 指令总结
完整命令文档
1.账号管理:
git config --global user.name Your Name
git config --global user.email youremail@yourdomain.com
git config --global user.password pwd
2. 查看本地分支
git branch -a
remotes/origin/HEAD -> origin/master
remotes/origin/develop
remotes/origin/dv2
remotes/origin/master
remotes/origin/test
3. git 拉取更新远程分支列表
git remote update origin --prune
4.复制分支
从master复制一个dev分支:
git chekout mater
git checkout -b dev
5.合并分支
1.合并分支
把master合并到 dev
git chekout dev
git merge master
git push
2.rebase合并
为了避免合并时解决冲突以rebase进行合并
git rebase --m release
6.删除分支
git branch -d add_user_setting //删除本地分支add_user_setting
git push origin :hotfix_authority_backup // 删除远程分支hotfix_authority_backup
7.空
8.拉代码冲突
git pull -r
First, rewinding head to replay your work on top of it...
Applying: cancel app_time comment
Using index info to reconstruct a base tree...
M src/omc/ui/locale/index.js
M src/omc/ui/views/Upgrades/upgradePackage/constants.js
M src/omc/ui/views/Upgrades/upgradePackage/index.jsx
Falling back to patching base and 3-way merge...
Auto-merging src/omc/ui/views/Upgrades/upgradePackage/index.jsx
Auto-merging src/omc/ui/views/Upgrades/upgradePackage/constants.js
Auto-merging src/omc/ui/locale/index.js
CONFLICT (content): Merge conflict in src/omc/ui/locale/index.js
error: Failed to merge in the changes.
Patch failed at 0001 cancel app_time comment
Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
解决方案:
1.修改冲突,
2.git add git rebase --continue
2.git pull git push
9.代码回滚
8.1本地代码回滚
git reset --hard {version}
8.2远程代码回滚
因为本地代码回滚后版本比远程仓库版本低,需要强制push
git reset --hard {version}
git push -f
10.标签管理
tag不是基于某个分支的,二是基于某个commit的
- 查看本地tag: git tag
- 获取远程tag:git fetch --tag
- 从某个tag切出一个分支:
git branch
git checkout newbranch
git tag v1.1.7
git push origin v1.1.7 - 删除tag
本地tag:git tag -d <tag名>
远程tag:git push origin :refs/tags/<tag名>
标签文档
https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE
11.ignore已提交文件
1.拉取最新代码
git pull
2.清理git缓存
git rm -r --cached .
3.修改.ignore, 添加文件并提交到远程
git add .
git commit -m"ignore file"
git push
12.移动某个commit
git cherry-pick [commit_num]
例子
现在要把分支2上面的Ecommit加到分支1后面
git checkout 分支1
git cherry-pick E
14.合并多个commit
如果有多个commit需要合并使用git rebase -i
1.查看历史commit
git log
commit 1wewe23w
commit 2wew2312
commit 31212s23
commit 23212s23
2.rebase你要合并那个commit
如果你要把最后三个合并成一个
git rebase -i 23212s23
执行上面的命令后会出现以下界面
pick 31212s23
pick 2wew2312
pick 1wewe23w
# Rebase a5a6164..f77f333 onto a5a6164 (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.
#
# Note that empty commits are commented out
合并主要使用squash和fixup
squash 把这个commit压入前一个commit并保留commit消息
fixup 把这个commit压入前一个commit不保留commit消息
如果后面的commit消息没有意义直接使用fixup(f)下面的改为
pick 31212s23
f 2wew2312
f 1wewe23w
wq保存以下,再Git log就变成了
commit 31212s23
commit 23212s23
如果使用squash,会把前几个commit的msg都保留下来append到31212s23的 commit消息后,如果需要做部分修改再执行
git commit --amend
则会把最新一个commit的消息展示出来,修改后wq保存以下即可
15. git ssh配置
cd ~/.ssh
ssh-keygen -t rsa -C "user"
cat id_rsa.pub
将id_rsa.pub 的内容全部copy到git的web端个人的ssh keys里