zoukankan      html  css  js  c++  java
  • Git 初始化项目、创建合并分支、回滚等常用方法总结

    就在刚才查看资料时候, 看见一句话, 写的特别好: 当我的才华撑不起我的梦想的时候, 应该安静下来学习

    配上我最喜欢动漫的一个角色: 红莲

    1. Git 初始化项目

    1). 创建新的知识库

                    echo "# study_git" >> README.md
                    git init
                    git add README.md
                    git commit -m "first commit"
                    git remote add origin https://github.com/renfanzi/study_git.git
                    git push -u origin master

    2). 再次提交

              git add . 或者 git add test.py
                    git commit -m "..."
                    git push origin master

    2. Git 创建分支

    git checkout -b dev 创建dev分支, 并切换到dev分支中去

    等同于:

    git branch dev 创建dev 分支
    git checkout dev 切换分支


    git branch 查看当前分支

    git branch -d dev 删除dev分支

    3. Git 合并分支内容

            git merge dev  命令用于合并指定分支到当前分支, 如果当前在master分支, 那么这条命令就是把dev合并到master
            注意:
                如果文档有冲突, 需要修正冲突以后再上传

    4. Git 回滚

    1). git log 命令可以帮我们查看历史记录, 查看版本号, 如果觉得太眼花缭乱, 可以加上 --pretty=oneline

                    ===========
                    root@corleone:/opt/code/my_code/git_study# git log
                    commit 6c73f83caaefbaa7d19ca583a27cfebcd6c6f071 (HEAD -> master, origin/master)
                    Author: root <root@localhost.localdomain>
                    Date:   Tue Jul 10 15:37:00 2018 +0800
    
                        two commit
    
                    commit ffc485e22ca1748d28d83f817cb5d8abbd7a0a5f
                    Author: root <root@localhost.localdomain>
                    Date:   Tue Jul 10 15:30:12 2018 +0800
    
                        first commit
                    ===========

    2). 两种方式回滚

    方法1:

    git reset --hard HEAD^ 

    (在Git中,用HEAD表示当前版本,也就是最新的提交ffc485e22c...(注意我的提交ID和你的肯定不一样),
    上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。)

    再次查看git log

                        -----------------------
                        root@corleone:/opt/code/my_code/git_study# git log 
                        commit ffc485e22ca1748d28d83f817cb5d8abbd7a0a5f (HEAD -> master)
                        Author: root <root@localhost.localdomain>
                        Date:   Tue Jul 10 15:30:12 2018 +0800
    
                            first commit
                        root@corleone:/opt/code/my_code/git_study# 
                        ----------------------

                    已经回滚到上一级了

    方法2:

    git reset --hard 6c73f83caa 等同于上面

    3). git 回滚到以前被掩盖覆盖的版本中

    方法一:

    只要上面的命令行窗口还没有被关掉,你就可以顺着往上找,找到那个commit 的commit ID,于是就可以指定回到指定版本


    方法二:

    git reflog ---> 作用: 记录你的每一次的命令


    终端返回值:
    ====================
    root@corleone:/opt/code/my_code/git_study# git reflog
    6c73f83 (HEAD -> master, origin/master) HEAD@{0}: reset: moving to 6c73f83caa
    6c73f83 (HEAD -> master, origin/master) HEAD@{1}: reset: moving to 6c73f83caa
    ffc485e HEAD@{2}: reset: moving to HEAD^
    6c73f83 (HEAD -> master, origin/master) HEAD@{3}: commit: two commit
    ffc485e HEAD@{4}: commit (initial): first commit
    ===================

    5. 常用命令

            git diff readme.md  查看文件被改变状态(以提交一次, 继续工作, 许久后忘记哪些地方修改了, 可以用这个命令)
                ======
                root@corleone:/opt/code/my_code/git_study# git diff readme.md
                diff --git a/readme.md b/readme.md
                index e69de29..bca558b 100644
                --- a/readme.md
                +++ b/readme.md
                @@ -0,0 +1,2 @@
                +asdfadsf
                +
                ======
            git reflog ---> 作用: 记录你的每一次的命令
    
            git log 命令可以帮我们查看历史记录, 查看版本号
            
            git reset --hard HEAD^ 
            git reset --hard 6c73f83caa 等同于上面
    
            git clone https://... 克隆项目

    6. .gitignore 的作用

    把要忽略的文件名填进去,Git就会自动忽略这些文件。

     

  • 相关阅读:
    屏蔽Alt+Enter、Ctrl+Alt+Del、Ctrl+Esc等功能键(Windows 98/Me)
    获取本月第一天和最后一天的最简单的写法
    PHP程序员的优化调试技术和技巧
    Windows关机函数ExitWindowsEx使用大全(适用Windows所有操作平台)
    屏蔽Alt+Enter、Ctrl+Alt+Del、Ctrl+Esc等功能键(Windows 98/Me)
    PHP程序员的优化调试技术和技巧
    Ajax,再生还是幻灭好文推荐
    RAID 分类
    Ajax,再生还是幻灭好文推荐
    Windows关机函数ExitWindowsEx使用大全(适用Windows所有操作平台)
  • 原文地址:https://www.cnblogs.com/renfanzi/p/9290301.html
Copyright © 2011-2022 走看看