zhangli-s 发表于 2018-9-16 13:26:58

git使用小结

  
一、配置
  
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 提交的作者信息
  

  
配置文件变更如下:
  

  
      name = Jack
  
      email = jack@test.com
  

  

  

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

  

  

  
3、init
  
git init --bare
  

  

  

  
4、分支管理
  
创建分支:
  
# git branch bbb
  

  
查看本地分支:
  
# git branch
  
bbb
  
* develop
  

  
使用分支:
  
# git checkout bbb
  
Switched to branch 'bbb'
  
# git branch
  
* bbb
  
develop
  

  
切换分支:
  
# git checkout develop
  
Switched to branch 'develop'
  

  
删除分支:
  
# git branch -d bbb
  
Deleted branch bbb (was f3a1250).
  

  
# git branch
  
* develop
  

  

  
列出所有分支:
  
# 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 的状态:
  
# git remote -v
  
originhttps://github.com/opera443399/portainer.git (fetch)
  
originhttps://github.com/opera443399/portainer.git (push)
  

  

  
增加一个 upstream 到 remote 中:
  
# git remote add upstream https://github.com/portainer/portainer.git
  
# 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 的代码:
  
# 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
  
*       codefresh-pr996 -> upstream/codefresh-pr996
  
*       demo       -> upstream/demo
  
*       develop    -> upstream/develop
  
*       feat257-compose-support -> upstream/feat257-compose-support
  
*       feat807-i18n -> upstream/feat807-i18n
  
*       gh-pages   -> upstream/gh-pages
  
*       master   -> upstream/master
  
*          1.15.0   -> 1.15.0
  

  

  

  
确认分支后,开始 merge 代码:
  
# git branch
  
* develop
  
# git merge upstream/develop
  
# 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 开发时要回滚
  

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

  

  

  
三、提交变更的常用操作
  
1、当前状态
  
# 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前操作)
  
# git checkout -- build/build_in_container.sh
  
# 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
  
在编辑器中写入修改信息后再提交:
  
# 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
  
add console on 'Task details' view; move tasks to the top
  
4 files changed, 14 insertions(+), 4 deletions(-)
  

  
# git log -1 --oneline
  
c42aaa7 add console on 'Task details' view; move tasks to the top
  
# 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到远端分支
  
# 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
  
*       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]
查看完整版本: git使用小结