[root@linux-node2 ~]# yum install -y git
[root@linux-node2 ~]# git config --global user.name "andy"
[root@linux-node2 ~]# git config --global user.email "andy@163.com"
[root@linux-node2 ~]# git config --global color.ui true
[root@linux-node2 ~]# git config --list
user.name=andy
user.email=andy@163.com
color.ui=true
[root@linux-node2 ~]# mkdir old
[root@linux-node2 ~]# cd old/
[root@linux-node2 old]# ll -a
total 8
drwxr-xr-x 2 root root 4096 Aug 12 22:11 .
dr-xr-x---. 5 root root 4096 Aug 12 22:11 ..
[root@linux-node2 old]# git init
Initialized empty Git repository in /root/old/.git/
[root@linux-node2 old]# ll -a
total 12
drwxr-xr-x 3 root root 4096 Aug 12 22:12 .
dr-xr-x---. 5 root root 4096 Aug 12 22:11 ..
drwxr-xr-x 7 root root 4096 Aug 12 22:12 .git
[root@linux-node2 old]# vim readme.txt
[root@linux-node2 old]# cat readme.txt
1 hehe
[root@linux-node2 old]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@linux-node2 old]# git add readme.txt ##添加到暂存区
[root@linux-node2 old]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
[root@linux-node2 old]# git commit -m "the first commit" ##提交
[master (root-commit) b2c0d97] the first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
[root@linux-node2 old]# git status
# On branch master
nothing to commit (working directory clean)
[root@linux-node2 old]# vim deploy.sh
[root@linux-node2 old]# cat deploy.sh
!#/bin/bash
echo hehe
[root@linux-node2 old]# git add deploy.sh ##添加到暂存区
[root@linux-node2 old]# git commit -m "2th commit" ##提交
[master c196373] 2th commit
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 deploy.sh
[root@linux-node2 old]# git log ##查看log日志
commit c196373ebda4986b9978d52838a6901c713b4996
Author: andy <andy@163.com>
Date: Sun Aug 12 22:25:15 2018 +0800
2th commit
commit b2c0d9757a9274508bd3cf6a2e0ed319cd431ff1
Author: andy <andy@163.com>
Date: Sun Aug 12 22:20:48 2018 +0800
the first commit
[root@linux-node2 old]# vim readme.txt
[root@linux-node2 old]# cat readme.txt
1 hehe
2 hehe
[root@linux-node2 old]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@linux-node2 old]# git diff readme.txt ##比较文件的不同
diff --git a/readme.txt b/readme.txt
index 408e625..833bc94 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
1 hehe
+2 hehe
[root@linux-node2 old]# git add readme.txt ##添加到暂存区
[root@linux-node2 old]# git commit -m "add 2hehe" ##提交到工作区
[master af27798] add 2hehe
1 files changed, 1 insertions(+), 0 deletions(-)
[root@linux-node2 old]# git log
commit af277982cb73b7425670618fd7305511657e2692
Author: andy <andy@163.com>
Date: Sun Aug 12 22:34:39 2018 +0800
add 2hehe
commit c196373ebda4986b9978d52838a6901c713b4996
Author: andy <andy@163.com>
Date: Sun Aug 12 22:25:15 2018 +0800
2th commit
commit b2c0d9757a9274508bd3cf6a2e0ed319cd431ff1
Author: andy <andy@163.com>
Date: Sun Aug 12 22:20:48 2018 +0800
the first commit
[root@linux-node2 old]# git reset --hard HEAD^ ##回退到当前版本的上一个版本
HEAD is now at c196373 2th commit
[root@linux-node2 old]# cat readme.txt
1 hehe
[root@linux-node2 old]# git log
commit c196373ebda4986b9978d52838a6901c713b4996
Author: andy <andy@163.com>
Date: Sun Aug 12 22:25:15 2018 +0800
2th commit
commit b2c0d9757a9274508bd3cf6a2e0ed319cd431ff1
Author: andy <andy@163.com>
Date: Sun Aug 12 22:20:48 2018 +0800
the first commit
[root@linux-node2 old]# git reflog ##查看历史的提交记录
c196373 HEAD@{0}: HEAD^: updating HEAD
af27798 HEAD@{1}: commit: add 2hehe
c196373 HEAD@{2}: commit: 2th commit
b2c0d97 HEAD@{3}: commit (initial): the first commit
[root@linux-node2 old]# git reset --hard b2c0d97 ##根据commitid回退到第一次提交的状态
HEAD is now at b2c0d97 the first commit
[root@linux-node2 old]# ll
total 4
-rw-r--r-- 1 root root 7 Aug 12 22:36 readme.txt
[root@linux-node2 old]# cat readme.txt
1 hehe
[root@linux-node2 old]# vim readme.txt
[root@linux-node2 old]# cat readme.txt
1 hehe
2 echo hehe
[root@linux-node2 old]# git add readme.txt
[root@linux-node2 old]# git commit -m "3th commit"
[master f83decf] 3th commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@linux-node2 old]# vim readme.txt
[root@linux-node2 old]# cat readme.txt
1 hehe
2 echo hehe
3 echo hehe hehe
[root@linux-node2 old]# git checkout -- readme.txt ##从版本库中重新拉一份
[root@linux-node2 old]# cat readme.txt
1 hehe
2 echo hehe
建立远程仓库
[root@linux-node2 old]# git remote add origin https://github.com/yueyuancun/demo.git
[root@linux-node2 old]# cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/yueyuancun/demo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[root@linux-node2 old]# git pull origin master
warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/yueyuancun/demo
* branch master -> FETCH_HEAD
Merge made by recursive.
.gitignore | 104 +++++++++++++++++++++++++++++++
LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 305 insertions(+), 0 deletions(-)
create mode 100644 .gitignore
create mode 100644 LICENSE
[root@linux-node2 old]# ls -a
. .. .git .gitignore LICENSE readme.txt
[root@linux-node2 old]# touch 12.txt
[root@linux-node2 old]# git add 12.txt
[root@linux-node2 old]# git commit -m "1th"
[master d3690b0] 1th
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 12.txt
[root@linux-node2 old]# ll
total 16
-rw-r--r-- 1 root root 0 Aug 19 19:00 12.txt
-rw-r--r-- 1 root root 11357 Aug 19 18:34 LICENSE
-rw-r--r-- 1 root root 43 Aug 19 18:58 readme.txt
[root@linux-node2 old]# git push -u origin master
Password:
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 349 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://yueyuancun@github.com/yueyuancun/demo.git
c381b1f..d3690b0 master -> master
Branch master set up to track remote branch master from origin.
克隆
[root@linux-node2 ~]# cd /tmp/
[root@linux-node2 tmp]# ll
total 0
[root@linux-node2 tmp]# git clone https://github.com/yueyuancun/demo.git
Initialized empty Git repository in /tmp/demo/.git/
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 16 (delta 2), reused 11 (delta 0), pack-reused 0
Unpacking objects: 100% (16/16), done.
[root@linux-node2 tmp]# tree
.
└── demo
├── 12.txt
├── LICENSE
└── readme.txt
1 directory, 3 files
分支的管理
[root@linux-node2 old]# git branch
* master
[root@linux-node2 old]# git checkout -b dev (git brabch dev , git checkout dev)
Switched to a new branch 'dev'
[root@linux-node2 old]# git branch
* dev
master
[root@linux-node2 old]# vim test.txt
hehe
[root@linux-node2 old]# ll
total 20
-rw-r--r-- 1 root root 0 Aug 19 19:00 12.txt
-rw-r--r-- 1 root root 11357 Aug 19 18:34 LICENSE
-rw-r--r-- 1 root root 43 Aug 19 18:58 readme.txt
-rw-r--r-- 1 root root 5 Aug 19 22:07 test.txt
[root@linux-node2 old]# git add test.txt
[root@linux-node2 old]# git commit -m "add test.txt"
[dev 4d6397a] add test.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
[root@linux-node2 old]# git checkout master
Switched to branch 'master'
[root@linux-node2 old]# ll
total 16
-rw-r--r-- 1 root root 0 Aug 19 19:00 12.txt
-rw-r--r-- 1 root root 11357 Aug 19 18:34 LICENSE
-rw-r--r-- 1 root root 43 Aug 19 18:58 readme.txt
[root@linux-node2 old]# git merge dev (合并dev分支到master)
Updating d3690b0..4d6397a
Fast-forward
test.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
[root@linux-node2 old]# ll
total 20
-rw-r--r-- 1 root root 0 Aug 19 19:00 12.txt
-rw-r--r-- 1 root root 11357 Aug 19 18:34 LICENSE
-rw-r--r-- 1 root root 43 Aug 19 18:58 readme.txt
-rw-r--r-- 1 root root 5 Aug 19 22:13 test.txt
[root@linux-node2 old]# git branch
dev
* master
[root@linux-node2 old]# git branch -d dev (删除dev分支)
[root@linux-node2 old]# git checkout -b test
Switched to a new branch 'test'
[root@linux-node2 old]# vim 12.txt
123321hehe123123456
[root@linux-node2 old]# git add 12.txt
[root@linux-node2 old]# git cimmit -m "add 12.txt"
[root@linux-node2 old]# git checkout master
[root@linux-node2 old]# vim 12.txt
123456hehe123123
[root@linux-node2 old]# git add 12.txt
[root@linux-node2 old]# git commit -m "test change"
[test 8af0c6b] test change
1 files changed, 1 insertions(+), 1 deletions(-)
[root@linux-node2 old]# git merge test
Auto-merging 12.txt
CONFLICT (content): Merge conflict in 12.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@linux-node2 old]# cat 12.txt
<<<<<<< HEAD
123456hehe123123
=======
123321hehe123123456
>>>>>>> test
[root@linux-node2 old]# vim 12.txt
123321hehe123123456
[root@linux-node2 old]# git add 12.txt
[root@linux-node2 old]# git commit -m "master change"
[master c881b16] master change
[root@linux-node2 old]# git merge test
Already up-to-date.
[root@linux-node2 old]# cat 12.txt
123321hehe123123456
[root@linux-node2 old]# git push origin test
Password:
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (15/15), 1.10 KiB, done.
Total 15 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 1 local object.
To https://yueyuancun@github.com/yueyuancun/demo.git
* [new branch] test -> test