Git 学习笔记

建完仓库后Git上会给一下简略的Command

A simple command-line tutorial: 

Git global settings:

git config --global user.name "XXX"
git config --global user.email "XXX@XX.com"

Create git repository:
mkdir MyHomeSISBDD
cd MyHomeSISBDD
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/tzmok/MyHomeSISBDD.git
git push -u origin master

Existing repository?
cd existing_git_repo
git remote add origin https://gitee.com/tzmok/MyHomeSISBDD.git
git push -u origin master

文件状态

在 GIt 中,你的文件可能会处于三种状态之一:

  • 已修改(modified) - 已修改表示修改了文件,但还没保存到数据库中。

  • 已暂存(staged) - 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。

  • 已提交(committed) - 已提交表示数据已经安全的保存在本地数据库中。


工作区域

与文件状态对应的,不同状态的文件在 Git 中处于不同的工作区域。

  • 工作区(working) - 当你 git clone 一个项目到本地,相当于在本地克隆了项目的一个副本。工作区是对项目的某个版本独立提取出来的内容。这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。

  • 暂存区(staging) - 暂存区是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。有时候也被称作 `‘索引 index’',不过一般说法还是叫暂存区。

  • 本地仓库(local) - 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 本地仓库。

  • 远程仓库(remote) - 以上几个工作区都是在本地。为了让别人可以看到你的修改,你需要将你的更新推送到远程仓库。同理,如果你想同步别人的修改,你需要从远程仓库拉取更新。



分支操作:
1.查看本地分支 * 表示当前分支
$git branch

  2.查看远程分支

$ git branch -r

 3.查看所有分支

 

git branch -a

 git branch -vv 查看本地分支及追踪的分支

 

 

4.切换远程分支

5.切换本地分支

 # 基于远程分支创建新的可追溯的分支
$ git branch --track <new-branch> <remote-branch>


5.合并分支
合并前要先切回要并入的分支

Cherry-pick is an advanced feature
- Should git cherry-pick not replace merge




查看分支合并情况 git log --graph -pretty=oneline --abbre-commit

git log --graph --decorate --oneline



Rebase
- Squash commits
- Linear changes from another branch
Cherry-pick
- Move specific commits

run cherry-pick eeb37e2 from ticktet2 branch

6.撤消上一次commit的内容(该操作会彻底回退到某个版本,本地的源码也会变为上一个版本的内容)
git reset --hard <commit-id>


7.git commit -m 注释换行
8.查看到刚刚的log信息

commit --amend 

9.删除分支

删除一个远程分支:
(master)$ git push origin --delete my-branch 或者(master)$ git push origin :my-branch
 

 10.Rename a Branch

git branch -m <oldbranch> <newbranch>
git branch -m quickfix longfix

 

git branch -d 会在删除前检查merge状态(其与上游分支或者与head)。

git branch -D 是git branch --delete --force的简写,它会直接删除。

共同点
都是删除本地分支的方法(与删除远程分支命令相独立,要想本地和远程都删除,必须得运行两个命令)。

延伸

    1. 删除远程分支以及追踪分支:git push --origin -delete branch
    2. git clone 都干了啥?
      1、自动将服务器默认命名为origin
      2、创建远程分支origin/branch(指向master分支的指针)
      3、创建名为master的本地分支
分支常用流程:

 




查看Log:

git log

git log --oneline

git long --pretty=oneline

git log --pretty=format:"%ae committed on %cd"

git log --raw -1->
The last commit using the raw diff format




# 显示某个文件的所有修改
$ git log -p <file>

查看信息

显示工作路径下已修改的文件:

$ git status

显示与上次提交版本文件的不同:

$ git diff

显示提交历史:

# 从最新提交开始,显示所有的提交记录(显示hash, 作者信息,提交的标题和时间)
$ git log

# 显示某个用户的所有提交
$ git log --author="username"

# 显示某个文件的所有修改
$ git log -p <file>


Git Push
git push --dry-run ->pushing the feature/test branch to a remote origin
git show



reset命令的三个参数对比

reset moves the current branch,and optionally copies data from the Repository to the other areas.

–soft
仅仅在本地库移动HEAD指针
–mixed
在本地库移动HEAD指针
重置暂存区
–hard
在本地库移动HEAD指针
重置暂存区
重置工作区

撤销修改

撤销本地修改:

# 移除缓存区的所有文件(i.e. 撤销上次git add)
$ git reset HEAD

# 将HEAD重置到上一次提交的版本,并将之后的修改标记为未添加到缓存区的修改
$ git reset <commit>

# 将HEAD重置到上一次提交的版本,并保留未提交的本地修改
$ git reset --keep <commit>

# 放弃工作目录下的所有修改
$ git reset --hard HEAD

# 将HEAD重置到指定的版本,并抛弃该版本之后的所有修改
$ git reset --hard <commit-hash>

# 用远端分支强制覆盖本地分支
$ git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature

# 放弃某个文件的所有本地修改
$ git checkout HEAD <file>
比较文件差异:
查看尚未暂存的文件更新了哪些部分,不加参数直接输入
git diff
此命令比较的是工作目录(Working tree)和暂存区域快照(index)之间的差异
也就是修改之后还没有暂存起来的变化内容

diff里面a表示前面那个变量,b表示第二个变量

 
HEAD     commit版本
Index     staged版本
 
a、查看尚未暂存的文件更新了哪些部分,不加参数直接输入
    git diff
此命令比较的是工作目录(Working tree)和暂存区域快照(index)之间的差异
也就是修改之后还没有暂存起来的变化内容。
 
b、查看已经暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异
    git diff --cached
    git diff --staged
显示的是下一次commit时会提交到HEAD的内容(不带-a情况下)
 
 
c、显示工作版本(Working tree)和HEAD的差别
    git diff HEAD
 
 
d、直接将两个分支上最新的提交做diff
    git diff topic master 或 git diff topic..master
 
e、输出自topic和master分别开发以来,master分支上的changed。
    git diff topic...master
Changes that occurred on the master branch since when the topic
 branch was started off it
f、查看简单的diff结果,可以加上--stat参数
    git diff --stat
 
g、查看当前目录和另外一个分支的差别
    git diff test
显示当前目录和另一个叫'test'分支的差别
    git diff HEAD -- ./lib
显示当前目录下的lib目录和上次提交之间的差别(更准确的说是在当前分支下)
 
h、比较上次提交commit和上上次提交
    git diff HEAD^ HEAD
i、比较两个历史版本之间的差异
    git diff SHA1 SHA2
git diff 【文件名】:将工作区
将工作区中的文件与缓存区进行比较 git diff [文件名]
将工作区中的文件和本地库的历史记录比较
git diff [历史索引] [文件名] 不指定文件名可比较当前工作区中所有的文件
git diff HEAD


Git 基本原理






通过各个版本的父子关系




拉取:


更新与推送

what is ""pull"?

a fetch follow by merge

更新:

# 下载远程端版本,但不合并到HEAD中
$ git fetch <remote>

# 将远程端版本合并到本地版本中
$ git pull origin master

# 以rebase方式将远端分支与本地合并
$ git pull --rebase <remote> <branch>

推送:

# 将本地版本推送到远程端
$ git push remote <remote> <branch>

# 删除远程端分支
$ git push <remote> :<branch> (since Git v1.5.0)
$ git push <remote> --delete <branch> (since Git v1.7.0)

# 发布标签
$ git push --tags


QA:
After writing the command git checkout {branch-name} numerous times, you decide you want to abbreviate the name of the command.
You run the command git config --global alias.co checkout and expect an alias to be set. What happens if you then type git co {branch-name}?
AS:
The checkout command happens as expected; the alias was set successfully.别名的使用
git config --global alias.a add
git config --global alias.c commit
git config --global alias.o checkout

QA:What does git grep accomplish?
AS:
It searches for specific patterns in your repository.

 Why is the reflog considered a safety net?

It maintains references to the old state of objects even if they are not connected to a branch.

Git  configuration:
1.Git Configuration Level

2.Three configuation level:
– Repository (local)
– User account (global)
– Git installation (system)
# Local (repository)
git config --local user.name “David Tucker"
# Global (user directory)
git config --global user.name “David Tucker”
# System (git installation)
git config --system user.name “David Tucker”

# Show all config values and the file where they are defined
git config --list —-show-origin
# Show the current user.name config value (sub any key)
git config user.name

Removing Git Config Values
Leveraging the command line to remove current config values
# Remove a specific setting for a specific level of config
git config --global --unset user.name
# Edit a specific level of config directly
git config --global --edit
# Remove a section of config for a specific

Git Hooks Process
A pre-defined script that is executed in response to a specific action that has occurred within your Git repository.


Which type of Git hooks can be used to strictly enforce team policies?
server-side hooks
Which ty can be used to evaluate all refs currently being push to a remote?
pre-receive

Finding Bugs with Git
Git Bisect=>A tool included with Git that enables you to specify a start and end commit and either manually or
automatically determine the point of failure within that range of commits.
Evaluating Commits in Git Bisect
Designating commits as either good or bad
start the git bisect:git bisect start
eedn the git bisect :git binsect reset

# Indicate that the commit is good
git bisect good
# Indicate that the commit is bad
git bisect bad



git diff


  1. (单选题)关于git push推送,下列说法错误的是 (D)
  2. git push <远程主机名> <本地分支名>:<远程分支名>:将本地分支推送到远程分支
  3. git push --all origin:将本地的所有分支都推送到远程主机
  4. git push:如果当前分支只有一个追踪分支,那么主机名也可以省略
  5. git push --force origin:强制推送,没有任何风险
  1. (单选题)以下不是git的操作有 (C)
  2. add
  3. push
  4. mkdir
  5. fetch

 


  1. (单选题)如果把项目中文件 hello.java 的内容破坏了,如何使其还原至原始版本?(B)
  2. git reset – hello.java
  3. git checkout HEAD – hello.java
  4. git revert hello.java
  5. git update hello.java

 


  1. (单选题)下列关于git的描述不恰当的一项是 (C)
  2. 可以采用公钥认证进行安全管理
  3. 可以利用快照签名回溯历史版本
  4. 必须搭建Server才能提交修改
  5. 属于分布式版本控制工具

 


  1. (单选题)关于删除分支XX,下列说法正确的是 (A)
  2. 执行 git push origin :XX 来删除远程版本库的 XX 分支。
  3. 执行 git branch -D XX 删除分支,总是能成功。
  4. 远程版本库删除的分支,在执行 git fetch 时本地分支自动删除。
  5. 本地删除的分支,执行 git push 时,远程分支亦自动删除。

 


  1. (单选题)关于git的临时工作区操作,下列说法错误的是:(A)
  2. git stash:创建保存临时工作区,只可执行一次,即只能创建一个临时工作区
  3. git stash list:查看保存的工作区列表
  4. git stash pop:恢复最新的进度到工作区。git默认会把工作区和暂存区的改动都恢复到工作区。
  5. git stash clear:删除所有存储的进度。

 

  1. (单选题)创建名为test的本地分支(B)
  2. git checkout test
  3. git branch test
  4. git branch -d test
  5. git remote test

 


 


  1. (单选题)关于git clone 下面说法错误的是:(D)
  2. 克隆时所有分支均被克隆,但只有HEAD指向的分支被检出。
  3. 可以通过 git clone --single-branch 命令实现只克隆一个分支。
  4. 克隆出的工作区中执行 git log、git status、git checkout、git commit等操作不会去访问远程版本库。
  5. 克隆时只有远程版本库HEAD指向的分支被克隆。

 


  1. (单选题)仅将工作区中修改的文件添加到暂存区(新增文件不添加),以备提交,用什么命令标记最快?(D)
  2. git add -A
  3. git add -p
  4. git add -i
  5. git add -u

 


  1. (单选题)对于下列git命令,说法错误的是(D)
  2. git init:初始化仓库
  3. git status:查看分支状态
  4. git add [file]:将文件提交到暂存区
  5. git log:文件同步到本地仓库


posted @ 2020-08-21 16:29  tzmok  阅读(261)  评论(0编辑  收藏  举报