设为首页 收藏本站
查看: 689|回复: 0

[经验分享] git使用小结

[复制链接]

尚未签到

发表于 2018-9-16 13:26:58 | 显示全部楼层 |阅读模式
  
一、配置
  
1、config
  
~]# git config user.name "Jack"
  
~]# git config user.email jack@test.com
  

  
影响内容:.git/config
  

  

  
~]# git config --global user.name "Jack"
  
~]# git config --global user.email jack@test.com
  

  
影响内容:~/.gitconfig
  

  
~]# git config --system user.name "Jack"
  
~]# git config --system user.email jack@test.com
  

  
影响内容:/etc/gitconfig
  

  

  
涉及内容:
  
git 提交的作者信息
  

  
配置文件变更如下:
  
[user]
  
        name = Jack
  
        email = jack@test.com
  

  

  

  
2、clone
  
git clone https://tester01@github.com/tester01/charade.git
  

  

  

  
3、init
  
git init --bare
  

  

  

  
4、分支管理
  
创建分支:
  
[root@dev_08 portainer]# git branch bbb
  

  
查看本地分支:
  
[root@dev_08 portainer]# git branch
  
  bbb
  
* develop
  

  
使用分支:
  
[root@dev_08 portainer]# git checkout bbb
  
Switched to branch 'bbb'
  
[root@dev_08 portainer]# git branch
  
* bbb
  
  develop
  

  
切换分支:
  
[root@dev_08 portainer]# git checkout develop
  
Switched to branch 'develop'
  

  
删除分支:
  
[root@dev_08 portainer]# git branch -d bbb
  
Deleted branch bbb (was f3a1250).
  

  
[root@dev_08 portainer]# git branch
  
* develop
  

  

  
列出所有分支:
  
[root@dev_08 portainer]# git branch -a
  
* develop
  
  remotes/origin/HEAD -> origin/develop
  
  remotes/origin/angular-loading-bar
  
  remotes/origin/codefresh-pr1224
  
  remotes/origin/demo
  
  remotes/origin/develop
  
  remotes/origin/docker17
  
  remotes/origin/feat-add-container-console-on-task-details
  
  remotes/origin/feat1235-setting-disable-binds
  
  remotes/origin/feat257-compose-support
  
  remotes/origin/feat257-stack-deploy
  
  remotes/origin/feat807-i18n
  
  remotes/origin/gh-pages
  
  remotes/origin/master
  
  remotes/upstream/codefresh-pr996
  
  remotes/upstream/demo
  
  remotes/upstream/develop
  
  remotes/upstream/feat257-compose-support
  
  remotes/upstream/feat807-i18n
  
  remotes/upstream/gh-pages
  
  remotes/upstream/master
  

  

  

  
5、remote
  
1)git push时,会报错:
  
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/tester01/charade.git/info/refs
  

  
fatal: HTTP request failed
  
解决办法:
  
git remote set-url origin https://tester01@github.com/tester01/charade.git
  

  

  
2)当我 fork 一个项目后,如果 upstream 有更新,如何合并到自己 fork 的项目中
  
查看 remote 的状态:
  
[root@dev_08 portainer]# git remote -v
  
originhttps://github.com/opera443399/portainer.git (fetch)
  
originhttps://github.com/opera443399/portainer.git (push)
  

  

  
增加一个 upstream 到 remote 中:
  
[root@dev_08 portainer]# git remote add upstream https://github.com/portainer/portainer.git
  
[root@dev_08 portainer]# git remote -v
  
originhttps://github.com/opera443399/portainer.git (fetch)
  
originhttps://github.com/opera443399/portainer.git (push)
  
upstreamhttps://github.com/portainer/portainer.git (fetch)
  
upstreamhttps://github.com/portainer/portainer.git (push)
  

  

  
获取 upstream 的代码:
  
[root@dev_08 portainer]# git fetch upstream
  
remote: Counting objects: 249, done.
  
remote: Compressing objects: 100% (3/3), done.
  
remote: Total 249 (delta 176), reused 179 (delta 176), pack-reused 70
  
Receiving objects: 100% (249/249), 37.10 KiB | 0 bytes/s, done.
  
Resolving deltas: 100% (176/176), completed with 134 local objects.
  
From https://github.com/portainer/portainer
  
* [new branch]      codefresh-pr996 -> upstream/codefresh-pr996
  
* [new branch]      demo       -> upstream/demo
  
* [new branch]      develop    -> upstream/develop
  
* [new branch]      feat257-compose-support -> upstream/feat257-compose-support
  
* [new branch]      feat807-i18n -> upstream/feat807-i18n
  
* [new branch]      gh-pages   -> upstream/gh-pages
  
* [new branch]      master     -> upstream/master
  
* [new tag]         1.15.0     -> 1.15.0
  

  

  

  
确认分支后,开始 merge 代码:
  
[root@dev_08 portainer]# git branch
  
* develop
  
[root@dev_08 portainer]# git merge upstream/develop
  
[root@dev_08 portainer]# git log --oneline -15
  
730925b fix(containers): fix an issue with filters
  
7eaaf9a feat(container-inspect): add the ability to inspect containers
  
925326e feat(volume-details): show a list of containers using the volume
  
dc05ad4 fix(templates): add missing NetworkSettings field (#1287)
  
8ec7b4f chore(codefresh): add a step to download docker binary (#1283)
  
dc48fa6 fix(cli): fix default asset directory value
  
7727fc6 Merge tag '1.15.0' into develop
  
5785ba5 Merge branch 'release/1.15.0'
  
e110986 chore(version): bump version number
  
587e2fa feat(stacks): add support for stack deploy (#1280)
  
8082793 chore(build-system): fix 'gruntify-eslint' usage (#1276)
  
f3a1250 feat(container-creation) - Add container resource management (#1224)
  
79121f9 docs(swagger): add missing Username field in UserAdminInitRequest
  
f678d05 feat(tasks): add a filter for tasks in service-details view
  
c6341ee docs(swagger): update swagger docs
  

  
推送到自己 fork 的项目中
  
git push
  

  

  

  
6、git hooks
  
示例 hooks/post-receive 如下:
  

  
#!/bin/sh
  
git checkout -f
  

  
push 后将打印最近变动的文件
  

  

  
二、回滚
  
参考:
  
https://www.atlassian.com/git/tutorials/undoing-changes
  

  
1、git revert
  
保存了 history 信息,适用于已经 push 到 public repository 后,要回滚
  

  

  
2、git reset
  
清除了 history 信息,很暴力,适用于 local 开发时要回滚
  

  
回退到上一个版本:
  
[root@dev_08 portainer]# git log -2 --oneline
  
0dd239c add console on 'Task details' view; move tasks to the top
  
730925b fix(containers): fix an issue with filters
  
[root@dev_08 portainer]# git reset --hard HEAD^
  
HEAD is now at 730925b fix(containers): fix an issue with filters
  

  

  

  
三、提交变更的常用操作
  
1、当前状态
  
[root@dev_08 portainer]# git status
  
# On branch feat-add-container-console-on-task-details
  
# Changes not staged for commit:
  
#   (use "git add ..." to update what will be committed)
  
#   (use "git checkout -- ..." to discard changes in working directory)
  
#
  
#modified:   app/components/service/includes/tasks.html
  
#modified:   app/components/service/service.html
  
#modified:   app/components/sidebar/sidebarController.js
  
#modified:   app/components/task/task.html
  
#modified:   build/build_in_container.sh
  
#
  
no changes added to commit (use "git add" and/or "git commit -a")
  

  

  
2、丢弃某个文件的修改(commit前操作)
  
[root@dev_08 portainer]# git checkout -- build/build_in_container.sh
  
[root@dev_08 portainer]# git status
  
# On branch feat-add-container-console-on-task-details
  
# Changes not staged for commit:
  
#   (use "git add ..." to update what will be committed)
  
#   (use "git checkout -- ..." to discard changes in working directory)
  
#
  
#modified:   app/components/service/includes/tasks.html
  
#modified:   app/components/service/service.html
  
#modified:   app/components/sidebar/sidebarController.js
  
#modified:   app/components/task/task.html
  
#
  
no changes added to commit (use "git add" and/or "git commit -a")
  

  

  
2、add
  
将当前路径下的所有文件添加到VCS中:
  
git add .
  

  
3、commit
  
在编辑器中写入修改信息后再提交:
  
[root@dev_08 portainer]# git commit -a
  

  
# Please enter the commit message for your changes. Lines starting
  
# with '#' will be ignored, and an empty message aborts the commit.
  
# On branch feat-add-container-console-on-task-details
  
# Changes to be committed:
  
#   (use "git reset HEAD ..." to unstage)
  
#
  
#       modified:   app/components/service/includes/tasks.html
  
#       modified:   app/components/service/service.html
  
#       modified:   app/components/sidebar/sidebarController.js
  
#       modified:   app/components/task/task.html
  
#
  

  
上述,将打开一个编辑器来编辑内容,保存后:
  
".git/COMMIT_EDITMSG" 23L, 1300C written
  
[feat-add-container-console-on-task-details c42aaa7] add console on 'Task details' view; move tasks to the top
  
4 files changed, 14 insertions(+), 4 deletions(-)
  

  
[root@dev_08 portainer]# git log -1 --oneline
  
c42aaa7 add console on 'Task details' view; move tasks to the top
  
[root@dev_08 portainer]# git status
  
# On branch feat-add-container-console-on-task-details
  
nothing to commit, working directory clean
  

  

  
或者,简单的写一行信息并提交:
  
git commit -am
  

  

  
4、push
  
简单的push到远端
  
git push
  

  
push到远端分支
  
[root@dev_08 portainer]# git push origin feat-add-container-console-on-task-details:feat-add-container-console-on-task-details
  
Password for 'https://opera443399@github.com':
  
Counting objects: 23, done.
  
Delta compression using up to 4 threads.
  
Compressing objects: 100% (8/8), done.
  
Writing objects: 100% (12/12), 1.67 KiB | 0 bytes/s, done.
  
Total 12 (delta 8), reused 5 (delta 4)
  
remote: Resolving deltas: 100% (8/8), completed with 8 local objects.
  
To https://opera443399@github.com/opera443399/portainer.git
  
* [new branch]      feat-add-container-console-on-task-details -> feat-add-container-console-on-task-details
  

  

  

  
5、log
  
查看最近5条日志:
  
git log -5
  

  
查看最近5条日志(单行):
  
git log -5 --oneline
  

  

  

  

  

  

  

  
XYWX、参考
  
1、gitlab或github下fork后如何同步源的新更新内容?
  
https://www.zhihu.com/question/28676261



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-584587-1-1.html 上篇帖子: git命令大全(非常齐全) 下篇帖子: git自动化部署,测试环境,线上环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表