1.checkout
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
2.reset
reset Reset current HEAD to the special state.
git reset 可以将提交的内容重置。--mixed 是默认操作。 --soft 重置之后不会让本地的代码消失。 --hard 重置之后会让本地代码消失。
git reset --hard HEAD^^ 小角个数代表重置次数。执行reset操作之后,HEAD和远程仓库不一致,需要解决冲突才能push。
以上命令已经无用,以下命令才有用。2021-06-21
git reset --soft HEAD~1
3.fetch
git fetch origin 远程分支xxx:本地分支xxx
git fetch [<options>] [<repository> [<refspec>…]]
git log FETCH_HEAD //查看fetch日志
4.rebase和merge
rebase和merge都是用来合并分支的命令。
rebase只是合并内容,不会合并提交历史。merge会把提交历史都合并。
git checkout featureA
git merge master
git checkout featureA
git rebase master
它会把整个featureA的分支直接移到现在master分支的后面。
git rebase origin/dev
合并远程分支到当前分支。
5.stash
stash将本地的变化缓存起来。stash pop将代码从缓存中弹出来。
可以有效利用该命令,合并远程代码。
refusing to merge unrelated histories
需要执行git pull origin master --allow-unrelated-histories将两个分支强行合并
有时候,git pull的时候提示失败,可以用git fetch之后,再git rebase
6.push
git push origin feature-branch:feature-branch //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)
查看本地分支:
git branch
查看远程分支:
git branch -a
7.更新代码
以下命令的效果,相当于fetch+rebase,-r的意思是rebase
git pull -r origin oa-cloud
没事的时候可以看看:https://www.cnblogs.com/qcloud1001/p/9796750.html
存储密码,避免多次输入
You can use the git-credential-store via
git config credential.helper store
which stores your password unencrypted in the file system
8.关联GitHub仓库
9.开发流程
10.不merge某个文件(有问题)
https://www.jianshu.com/p/09b546b936a7
11.delete branch
// delete branch locally
git branch -d localBranchName
// delete branch remotely
git push origin --delete remoteBranchName
12.拉取远程分支并创建本地分支
方法一
使用如下命令:
git checkout -b 本地分支名x origin/远程分支名x
使用该方式会在本地新建分支x,并自动切换到该本地分支x。
采用此种方法建立的本地分支会和远程分支建立映射关系。
方式二
使用如下命令:
git fetch origin 远程分支名x:本地分支名x
使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。
采用此种方法建立的本地分支不会和远程分支建立映射关系。
参考链接:https://juejin.cn/post/6844903539324485639
https://www.cnblogs.com/qyf404/p/git_push_local_branch_to_remote.html
https://www.cnblogs.com/springbarley/archive/2012/11/03/2752984.html
https://www.huaweicloud.com/articles/e295e93d1a0f08102fe61426ef4997f3.html
https://www.cnblogs.com/qcloud1001/p/9796750.html
https://www.freecodecamp.org/news/how-to-delete-a-git-branch-both-locally-and-remotely/
https://devconnected.com/how-to-undo-last-git-commit/#:~:text=The%20easiest%20way%20to%20undo,removed%20from%20your%20Git%20history.