2020年 · 点滴记录

Git 常用操作

  • 获取与创建项目命令(git init)
    用 git init 在目录中创建新的 Git 仓库。 你可以在任何时候、任何目录中这么做,完全是本地化的。
    在目录中执行 git init,就可以创建一个 Git 仓库了。比如我们创建 project_scripts 项目:
    Administrator@SC-201912080501 MINGW64 ~/Desktop
    $ mkdir project_scriptsAdministrator@SC-201912080501 MINGW64 ~/Desktop
    $ cd project_scripts

    Administrator@SC-201912080501 MINGW64 ~/Desktop/project_scripts
    $ git init
    Initialized empty Git repository in C:/Users/Administrator/Desktop/project_scripts/.git/

  • git clone拷贝一个 Git 仓库到本地
    使用 git clone 拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改。
    如果你需要与他人合作一个项目,或者想要复制一个项目,看看代码,你就可以克隆那个项目。 执行命令:
    $ git clone http://git.ohyeahwoo.com/myprojects/tools.git
    Cloning into ‘tools’…
    remote: Enumerating objects: 12, done.
    remote: Counting objects: 100% (12/12), done.
    remote: Compressing objects: 100% (10/10), done.
    remote: Total 12 (delta 2), reused 0 (delta 0)
    Unpacking objects: 100% (12/12), 1.57 KiB | 33.00 KiB/s, done.
  • git add命令可将该文件添加到缓存
    git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。
    命令格式如下:git commit -a/git commit -am ‘直接提交描述’Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ touch hello.phpAdministrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    ?? hello.php

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git add hello.php

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    A hello.php

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ touch 111.txt 222.txt

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    A hello.php
    ?? 111.txt
    ?? 222.txt

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git add .

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    A 111.txt
    A 222.txt
    A hello.php

  • git status以查看在你上次提交之后是否有修改(-s 参数,以获得简短的结果输出)
    $ git status
    On branch master
    Your branch is up to date with ‘origin/master’.Changes to be committed:
    (use “git restore –staged <file>…” to unstage)
    new file: 111.txt
    new file: 222.txt
    new file: hello.php
  • git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别
    尚未缓存的改动:git diff
    查看已缓存的改动: git diff –cached
    查看已缓存的与未缓存的所有改动:git diff HEAD
    显示摘要而非整个 diff:git diff –stat
  • git commit 将缓存区内容添加到仓库中
    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    AM 111.txt
    A 222.txt
    A hello.phpAdministrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git commit -m “第一次提交”
    [master 3105ad8] 第一次提交
    3 files changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 111.txt
    create mode 100644 222.txt
    create mode 100644 hello.php

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status
    On branch master
    Your branch is ahead of ‘origin/master’ by 1 commit.
    (use “git push” to publish your local commits)

    Changes not staged for commit:
    (use “git add <file>…” to update what will be committed)
    (use “git restore <file>…” to discard changes in working directory)
    modified: 111.txt

    no changes added to commit (use “git add” and/or “git commit -a”)

  • git reset HEAD 命令用于取消已缓存的内容
    执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存
    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ echo 3333 > 111.txt

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    M 111.txt

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git add .
    warning: LF will be replaced by CRLF in 111.txt.
    The file will have its original line endings in your working directory

    Administrator@SC-201912080501 MINGW64 ~/Desktop/tools (master)
    $ git status -s
    M 111.txt

  • 提交到远程GitHub仓库 : git push -u origin master/git push
  • GitHub远程仓库同步 :git pull

相关操作参考:
https://blog.csdn.net/liuwengai/article/details/52072344
https://www.liaoxuefeng.com/wiki/896043488029600
https://git-scm.com/book/zh/v2
https://www.runoob.com/git/git-tutorial.html