1

Git用法总结

 3 years ago
source link: https://maxyoung.fun//blog/Git%E7%94%A8%E6%B3%95%E6%80%BB%E7%BB%93.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Git用法总结

  • 创建版本库。在需要版本控制的文件夹下输入此命令,才能进行版本控制

    git init

  • 查看版本状态

    git status


  • git branch branch_name

  • 创建并切换到一个分支

    git checkout -b branch_name

  • git merge branch_name

  • git branch -d branch_name

  • 删除远程分支

    git push origin —delete <branch_name>

  • 批量删除分支

    https://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git

    删除 3.2 3.2.1 3.2.2三个分支

    $ git branch -D 3.2 3.2.1 3.2.2
    

    更geek的方法是

    $ git branch -D `git branch | grep -E '^3\.2\..*'`
    
  • 如何用远程另外的分支覆盖本地分支

    本地分支名为yangle,用远程master分支覆盖此分支,保证yangle分支和远程master分支一样。在本地yangle分支下:

      $ git fetch origin
      $ git reset --hard origin/master
      # origin/master实际上是指fetch下来已经在本地的分支
    
  • 重命名本地分支

    git branch -m new_branch_name


  • 查看修改记录日志

    git log

  • 查看修改日志一条一行显示

    git log --pretty=oneline

  • 以导出当前分支最近50条log为例

    git log -50 > log50_latest_commits.txt

  • 查看文件修改信息

    git diff [文件名]


  • 版本回退到上一步

    git reset --hard HEAD^

  • 版本回退到前5步

    git reset --hard HEAD~5

  • 根据commit id来回退

    git reset --hard *******(id)


  • 如何从另外一个分支复制文件到当前分支

    git checkout branch_name file_path

  • 如何将另外一个分支的文件夹全部复制到当前分支

    git checkout <other-branch> -- <path-name>


标签tag

  • 创建tag

    git tag tagname

  • 删除本地tag

    git tag -d tagname

  • 推送tag

    git push --tag

  • 删除远程tag

    git push origin :refs/tags/tagname

submodule

在某个仓库里有submodule, 如果想要这个仓库运行起来, 需要执行:

$ git submodule update --init

如何让GIT不跟踪某些文件或文件夹

.gitignore文件, 将不需跟踪的文件和文件夹放入到这个文件里
规则参照https://www.atlassian.com/git/tutorials/saving-changes/gitignore

如果文件以及提交到了仓库, 那么还需要将文件从暂存区里移出, 采用:
git rm --cached readme.txt
参照https://blog.csdn.net/leedaning/article/details/44976319

如果是某种类型的文件, 非常多, 我们可以讲虽有文件都移出暂存区, 然后重新add
参照http://www.codeblocq.com/2016/01/Untrack-files-already-added-to-git-repository-based-on-gitignore/:

$ git rm -r --cached .
$ git add .
$ git commit -m ".gitignore fix"

  1. $ git remote -v
    # View current remotes
    origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    origin  https://github.com/OWNER/REPOSITORY.git (push)
    destination  https://github.com/FORKER/REPOSITORY.git (fetch)
    destination  https://github.com/FORKER/REPOSITORY.git (push)
       
    $ git remote rm destination
    # Remove remote
    $ git remote -v
    # Verify it's gone
    origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    origin  https://github.com/OWNER/REPOSITORY.git (push)
    
  2. $ git remote add origin <remote_repo_url>
    
  3. 修改远程仓库链接

     $ git remote set-url origin ***
    

合并commit

How can I merge two commits into one?

Get back to where you started with

$ git rebase --abort

Say your history is

$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a

That is, a was the first commit, then b, and finally c. After committing c we decide to squash b and c together:

Running git rebase --interactive HEAD~2 gives you an editor with

pick b76d157 b
pick a931ac7 c
Rebase df23917..a931ac7 onto df23917
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Changing b’s pick to squash will result in the error you saw, but if instead you squash c into b (newer into the older) by changing the text to

pick b76d157 b
pick a931ac7 c

and save-quitting your editor, you’ll get another editor whose contents are

# This is a combination of 2 commits.
# The first commit's message is:
b
# This is the 2nd commit message:
c

When you save and quit, the contents of the edited file become commit message of the new combined commit:

$ git log --pretty=oneline
18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c
df239176e1a2ffac927d8b496ea00d5488481db5 a

git rebase的用法

5.1 代码合并:Merge、Rebase 的选择

工作流参考资料

采用git-flow,原理参照:

http://www.ruanyifeng.com/blog/2012/07/git.html

https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/git-flow

http://nvie.com/posts/a-successful-git-branching-model/

https://github.com/tiimgreen/github-cheat-sheet

https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow

共分为5个分支,2个稳定分支,3个临时分支

  • master和develop分支会作为保护分支,必需通过code review才能合并

    分支 源分支 功能 master master 稳定分支,生产上线 develop master 所有开发的分支都从这个分支创建

  • 分支 源分支 功能 feature/* develop 新功能开发 release/* develop develop上创建的分支,用于发布前测试 hotfix/* master 生产环境有bug,临时紧急修复的分支

  1. 开发
    分支:feature-*
    操作人员:开发
    步骤:
    • 开发一个新功能时,从develop分支上切一个分支,命令规范为feature/*
       # 新建分支
       $ git checkout -b feature/new-feature develop  
       # 更新分支保证是最新代码  
       $ git rebase origin/develop
      
    • 联调和开发测试均在此分支上
      发布到测试服务器上供前端联调时, docker build打dev的tag
      发布后, 后端的地址是: clm-fe.dev.didatrip.com
    • 在本地构建镜像
      $ docker build . -t registry.cn-beijing.aliyuncs.com/rock2018/clm:dev
      $ docker push registry.cn-beijing.aliyuncs.com/rock2018/clm
      
    • 在测试服务器上上线
      $ docker pull registry.cn-beijing.aliyuncs.com/rock2018/clm:dev && docker-compose -f /var/www/devops/docker-compose-test.yml up -d
      
    • 测试通过,提交merge request,通过code review之后合并到develop上
      如果此功能不在这个发版周期内, 先不要合并到master
  2. 分支:release/*
    操作人员:分支管理人员
    一个周期内(比如一周),有多个feature开发,开发完成之后都合并到develop分支上,然后从develop分支上checkout一个test分支,命名规范为release/日期,例如:test-20171216
    如后续有feature分支合并到develop上随这个版本周期发版, 那么rebase develop, 继续在release分支上测试 测试人员在此分支上做整体测试,若测试有问题,开发人员在release分支上修改,直到测试通过
    提交测试之前后端开发人员需在测试环境自测一遍
    测试前需要给测试人员发送邮件,抄送[email protected],说明测试要点

  3. 分支:master
    master和develop分支分别合并release分支,生产发布master分支

    • git flow init

      git flow init

    • Creating a feature branch

      git flow feature start feature_branch

    • Finishing a feature branch

      git flow feature finish feature_branch

https://github.com/geeeeeeeeek/git-recipes/wiki

http://www.ruanyifeng.com/blog/2015/08/git-use-process.html

http://www.ruanyifeng.com/blog/2012/07/git.html

https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/git-flow

https://www.ibm.com/developerworks/cn/java/j-lo-git-mange/index.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK