Git that you can kill it

2019-11-14  本文已影响0人  simok
  1. 配置用户和邮箱
git config --global user.name "yourname"
git config --global user.email youremail
  1. 设置一些别名
git config --system alias.st status   #等价 git status
git config --global alias.br branch   #等价 git branch
  1. 开启颜色显示
git config --global color.ui true

  1. 创建仓库

[root@x ~]# git init git_learning
Initialized empty Git repository in /root/git_learning/.git/
[root@x ~]# ls
cert cert.back git_learning
[root@x ~]# cd git_learning/
[root@x git_learning]# ls -a
. .. .git
[root@x git_learning]# ls .git/
branches config description HEAD hooks info objects refs
[root@x git_learning]#

说明
1 执行 git config -e --global 将打开用户家目录的.gitconfig文件。
2 执行 git config -e --system 将打开 /etc/gitconfig文件。
配置文件优先级:
1 .git/config > /home/.gitconfig > /etc/gitconfig。
2 版本库级别配置文件 > 全局配置文件) > 系统级配置文件。
3 相同配置优先级高的会覆盖低的。

echo "Nice to meet you " >> welcome.txt #修改文件内容
git add  welcome.txt #加入暂存区
git commit -m "modify welcome.txt" #提交到版本库

重置 git commit -m "内容"

git commit --amend "new content"
git diff      #比较工作文件与暂存区文件的差异
git diff  --cached        #提交暂存区和版本库(HEAD)对比
git diff  HEAD    #工作区和当前工作分支对比
git diff.png
echo "Nice to meet you " >> welcome.txt
git add  welcome.txt #加入暂存区
git commit -m "add welcome.txt" #提交到版本库
$ git log
commit 80f08800f4447ce33884b9dd186dc3f880ded06e (HEAD -> dev)
Author: ??? <123@example.com>
Date:   Thu Nov 14 15:36:12 2019 +0800

    add welcome.txt

commit a2131ced6945898a0d41a2d4ee84602d79391206
Author: ??? <123@example.com>
Date:   Thu Nov 14 10:36:46 2019 +0800

    +a1

$ git log --pretty=raw
commit 7c5e74d978fbd218697937d7c1fa7ce9424f02da
tree cceaaa16d66dccc9b32a91336c5ff0f0d7abe445
parent 80f08800f4447ce33884b9dd186dc3f880ded06e
author ??? <123@example.com> 1573719201 +0800
committer ??? <123@example.com> 1573719201 +0800

object.png
echo "!!!!"   >> welcome.txt
git add welcome.txt
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   welcome.txt

git checkout -- filename 工作区文件恢复与暂存区一样

$ cat a1
a1
WQD
qqq
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "!!!!" >> a1
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat a1
a1
WQD
qqq
!!!!
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git checkout -- a1
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat a1
a1
WQD
qqq

清除当前工作区中没有加入版本库的文件和目录

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "clean"  >> gitclean.txt    #新建文件

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ ls
a1  a3  database/     jumpserver/  service-log/  test.txt
a2  a4  gitclean.txt  README.md    test/         welcome.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git clean -fd
Removing gitclean.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ ls
a1  a2  a3  a4  database/  jumpserver/  README.md  service-log/  test/  test.txt  welcome.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "git chekcout" >> welcome.txt

$ cat welcome.txt
Nice to meet you
git statusgit status
git chekcout
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   welcome.txt
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "++" >> welcome.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat  welcome.txt
Nice to meet you
git statusgit status
git chekcout
++
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git checkout .

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat welcome.txt
Nice to meet you
git statusgit status
git chekcout
xixianbin@xixianbins-MacBook-Pro git_learning % git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   index.html

xixianbin@xixianbins-MacBook-Pro git_learning % cat index.html 
111
222
333
xixianbin@xixianbins-MacBook-Pro git_learning % git reset HEAD
Unstaged changes after reset:
M   index.html

git.png
版本库.png

.git/refs/heads/master 分支 指向的是一次commit提交产生的ID

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat  .git/refs/heads/dev
2644da659a6345e198c4e38e7de6c862978deb4

提交新文件并查看commit-ID

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ touch.exe new-commit.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git add new-commit.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git commit -m "touch new-commit.txt"
[dev 66d008b] touch new-commit.txt
 2 files changed, 1 insertion(+)
 create mode 100644 new-commit.txt
$ git log --pretty=raw
commit 66d008b74e1904e741f20d2f89cf5fa0fa416f34
tree 3e2adc3d00c7d963b5ff75e33262bd9a7a8f00ff
parent 7c5e74d978fbd218697937d7c1fa7ce9424f02da
author ??? <123@example.com> 1573723354 +0800
committer ??? <123@example.com> 1573723354 +0800

    touch new-commit.txt

dev分支指向改变

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat  .git/refs/heads/dev
66d008b74e1904e741f20d2f89cf5fa0fa416f34

修改文件工作区文件内容

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ ls
a1  a3  database/    new-commit.txt  service-log/  test.txt
a2  a4  jumpserver/  README.md       test/         welcome.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "you are a good man" >> test.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat test.txt
aice to meet you
byebye
qou are a good m2n
newww
22
you are a good man

重置上次commit

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git reset --hard HEAD~1
HEAD is now at 7c5e74d w

new-commit.txt 不存在

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ ls
a1  a2  a3  a4  database/  jumpserver/  README.md  service-log/  test/  test.txt  welcome.txt

工作区的修改文件内容不生效

$ cat test.txt
aice to meet you
byebye
qou are a good m2n
newww
22

查看当前dev指向的commit id

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat .git/refs/heads/dev
2644da659a6345e198c4e38e7de6c862978deb42

git reset --hard HEAD~1 参数--hard 会影响工作区工作的内容(test.txt修改的内容被撤销),HEAD指向dev~1表示上一次提交

重置命令指向之前任意的commit

git reset --hard  9e8a789          #(任意的commit哈希值至少前四位)

挽救错误的重置reflog

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git reflog show dev
7c5e74d (HEAD -> dev) dev@{0}: reset: moving to HEAD~1
66d008b dev@{1}: commit: touch new-commit.txt
7c5e74d (HEAD -> dev) dev@{2}: commit: w
80f0880 dev@{3}: commit: add welcome.txt
a2131ce dev@{4}: commit: +a1
7729f36 (origin/dev) dev@{5}: commit: d
89db835 dev@{6}: pull origin master: Fast-forward
986b574 dev@{7}: pull origin xxb: Fast-forward
0336132 dev@{8}: pull origin dev: Fast-forward
8b67db0 dev@{9}: commit: mod test.txt
6669e21 dev@{10}: pull origin dev: Fast-forward
8d7f6ae dev@{11}: pull origin dev: Fast-forward
88a6943 dev@{12}: commit: aa
f9c532e (tag: old_practice) dev@{13}: branch: Created from HEAD

git reset --hard dev@{1}

恢复文件new-commit.txt

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ ls
a1  a2  a3  a4  database/  jumpserver/  new-commit.txt  README.md  service-log/  test/  test.txt  welcome.txt

git reset 参数说明

git reset --hard HEAD~1
git reset --mixed HEAD~1
git reset --soft  HEAD~1
git reset filename
git reset HEAD
reset 参数.png reset参数2.png
reset参数3.png

用于保存予恢复工作进度,会分别对工作区暂存区的状态进行保存

$ git status list
On branch dev
nothing to commit, working tree clean


bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "new content"  >> a1

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ touch.exe a5

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ echo "a5" >> a5

$ git stash
warning: LF will be replaced by CRLF in a1.
The file will have its original line endings in your working directory
Saved working directory and index state WIP on dev: 66d008b touch new-commit.txt

$ git status
On branch dev
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a5

nothing added to commit but untracked files present (use "git add" to track)

$ git stash list
stash@{0}: WIP on dev: 66d008b touch new-commit.txt
stash@{1}: WIP on master: f9c532e 2019-11-13
stash@{2}: On master: hack-1

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash apply stash@{0}
On branch dev
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:   a1

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a5

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


git stash 完整版

git stash save "message"

git stash drop 删除一个stash

$ git stash list
stash@{0}: WIP on dev: 66d008b touch new-commit.txt
stash@{1}: WIP on master: f9c532e 2019-11-13
stash@{2}: On master: hack-1

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash drop stash@{2}
Dropped stash@{2} (af4b13ac02775e411f977ced05fba8e6f460a390)

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash list
stash@{0}: WIP on dev: 66d008b touch new-commit.txt
stash@{1}: WIP on master: f9c532e 2019-11-13

删除所有stash

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash list
stash@{0}: WIP on dev: 66d008b touch new-commit.txt
stash@{1}: WIP on master: f9c532e 2019-11-13

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash clear

bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ git stash  list

git clone  <repository>     #版本库A  与 B  是对等关系 即工作区版本库都是一样的

git clone --bare     # 生成裸库  即只有版本库对等, 不包含工作区
git pull origin master   =  git fetch +  git merge     #与同步远程分支master
git push origin master   #退出提交到远程分支
bob@DESKTOP-KE36KMF MINGW64 /f/git/facility (dev)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = git@hiq.hxby.com:ooc/faity.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

上一篇 下一篇

猜你喜欢

热点阅读