zoukankan      html  css  js  c++  java
  • git使用<一>:常用本地操作

      编写软件,时常免不了修修改改,修改过后的代码不一定比前面好,甚至产生新问题,或者有时无意间修改了某行代码,导致出错,这种情况都是很常见的,如果此时没有版本管理,如果是小软件可能没什么影响,如果代码量很大,就是个很头疼的问题,git的出现正是为了解决这个问题的,对于码农来说,简直是神器,下面简单记录下。

    基本操作:
    1.仓库初始化:
    直接进入文件夹,输入git init
    2.添加文件:
    一个文件(比如x文件):git add x
    多个文件(比如x,y文件):git add x y
    整个文件夹文件:git add *
    3.提交文件:
    一行注释:git commit -m "you comment"
    多行注释:git commit -m ",然后按下回车,输入内容,然后继续输入内容,输入完之后输入"
    4.查看状态:
    git status
    5.查看日志:
    查看版本日志:git log
    查看所有操作日志:git reflog
    6.查看异同:
    仅仅查看:git diff
    制作patch:git diff > diff.patch
    7.版本回滚与切换:
    先执行git log确定要回滚到的版本,然后使用git reset --hard进行回滚;如果回滚后突然又想回到之前的版本,怎么办呢?这个时候只需要再次查看日志,不过需要使用git reflog,然后再次git reset --hard就可以恢复到之前的版本
    操作实例:

    ubuntu@ubuntu:tst$ git init        # 初始化
    Initialized empty Git repository in /samba/TMP/tst/.git/
    ubuntu@ubuntu:tst$ vi tst.txt
    ubuntu@ubuntu:tst$ git status    # 查看状态
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            tst.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    ubuntu@ubuntu:tst$ git add tst.txt    # 添加文件
    ubuntu@ubuntu:tst$ git commit -m "    # 提交文件
    > first commit
    > "
    [master (root-commit) 767ba26] first commit
     1 file changed, 1 insertion(+)
     create mode 100644 tst.txt
    ubuntu@ubuntu:tst$ git log            # 查看日志
    commit 767ba26cc39de73ab2848680058a339341599fe8
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:11:56 2017 +0800
    
        first commit
    ubuntu@ubuntu:tst$ vi tst.txt
    ubuntu@ubuntu:tst$ git diff            # 查看修改
    diff --git a/tst.txt b/tst.txt
    index d00491f..48082f7 100644
    --- a/tst.txt
    +++ b/tst.txt
    @@ -1 +1 @@
    -1
    +12
    ubuntu@ubuntu:tst$ git status        # 再次查看状态
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   tst.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ubuntu@ubuntu:tst$ git add tst.txt    # 再次添加修改后的文件
    ubuntu@ubuntu:tst$ git commit -m "second commit" # 提交修改
    [master be932f8] second commit
     1 file changed, 1 insertion(+), 1 deletion(-)
    ubuntu@ubuntu:tst$ git log            # 查看日志
    commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:16:21 2017 +0800
    
        second commit
    
    commit 767ba26cc39de73ab2848680058a339341599fe8
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:11:56 2017 +0800
    
        first commit
    ubuntu@ubuntu:tst$ git reflog        # 查看所有操作日志
    be932f8 HEAD@{0}: commit: second commit
    767ba26 HEAD@{1}: commit (initial): first commit
    ubuntu@ubuntu:tst$ git reset --hard 767ba26 # 回滚到第一次提交
    HEAD is now at 767ba26 first commit
    ubuntu@ubuntu:tst$ git log                    # 查看日志    
    commit 767ba26cc39de73ab2848680058a339341599fe8
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:11:56 2017 +0800
    
        first commit
    ubuntu@ubuntu:tst$ git reflog        # 查看所有操作日志
    767ba26 HEAD@{0}: reset: moving to 767ba26
    be932f8 HEAD@{1}: commit: second commit
    767ba26 HEAD@{2}: commit (initial): first commit
    ubuntu@ubuntu:tst$ git reset --hard be932f8    # 回滚到第二次提交
    HEAD is now at be932f8 second commit
    ubuntu@ubuntu:tst$ git log
    commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:16:21 2017 +0800
    
        second commit
    
    commit 767ba26cc39de73ab2848680058a339341599fe8
    Author: Your Name <you@example.com>
    Date:   Thu Aug 24 22:11:56 2017 +0800
    
        first commit
    ubuntu@ubuntu:tst$
  • 相关阅读:
    sqlserver 行转列 语文,数学,物理,化学
    除去字符串中不相临的重复的字符 aabcad 得 aabcd
    Mvc项目实例 MvcMusicStore 五
    c# 计算两日期的工作时间间隔(排除非工作日)及计算下一个工作时间点.
    html-----016---HTTP 状态消息
    html-----015---HTML ASCII 参考手册
    html-----014---统一资源定位器
    html-----013----实体字符/HTML URL 编码
    html-----012---颜色的改变
    html-----011--子窗体iframe
  • 原文地址:https://www.cnblogs.com/Ethan-Gao/p/7425567.html
Copyright © 2011-2022 走看看