22.5 單機上使用git(上)

22.5 單機上使用git

分佈式:簡單的說、指的是不須要網絡 能夠本地單機使用!linux

yum install -y git
mkdir /data/gitroot
cd /data/gitroot
git init //初始化倉庫
echo -e  「123\naaa\n456\nbbb」 > 1.txt //建立一個新文件
git add 1.txt//把1.txt添加到倉庫
git commit -m 「add new file 1.txt」 //add完了必需要commit纔算真正把文件提交到git倉庫裏
再次更改1.txt
git status  //查看當前倉庫中的狀態,好比是否有改動的文件
git diff 1.txt  //能夠對比1.txt本次修改了什麼內容,相比較倉庫裏面的版本
版本回退:
多更改幾回1.txt,而後add,commit
git log//查看全部提交記錄
git log --pretty=oneline//一行顯示
git reset --hard f7c8e9//回退版本,其中後面跟的字符串是簡寫
撤銷修改
rm -f 1.txt//不當心刪除了1.txt
git checkout -- 1.txt//恢復1.txt
若是1.txt文件修改,add後但沒有commit,再想回退到上一次提交的狀態,可使用git reset HEAD 1.txt,再執行git checkout -- 1.txt
git reflog //查看全部歷史版本
** 刪除文件:**
echo -e "11111111111\n2222222222" > 2.txt
git rm 2.txt
git commit -m "rm 2.txt"git

[root@Dasoncheng ~]# yum install -y git
[root@Dasoncheng ~]# mkdir /data/gitroot
[root@Dasoncheng ~]# cd !$
cd /data/gitroot
[root@Dasoncheng gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/
[root@Dasoncheng gitroot]# echo -e "123\naaa\n456\nbbb" > 1.txt
[root@Dasoncheng gitroot]# git add 1.txt  ##添加1.txt到倉庫(標記);
[root@Dasoncheng gitroot]# git commit -m "add new file 1.txt"  ##提交文件到倉庫(真正將文件提交到git倉庫裏面);
[master (root-commit) 39ae8b7] add new file 1.txt
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 4 insertions(+)
 create mode 100644 1.txt
[root@Dasoncheng gitroot]# echo 'aaa' >>1.txt
[root@Dasoncheng gitroot]# git status  
##查看當前倉庫中的狀態,好比是否有改動的文件;這裏有1.txt修改 提示你add後commit
# 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:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Dasoncheng gitroot]# git diff 1.txt    ##查看文件是否有什麼改動;
diff --git a/1.txt b/1.txt
index b149eee..f9610bd 100644
--- a/1.txt
+++ b/1.txt
@@ -2,3 +2,4 @@
 aaa
 456
 bbb
+aaa
[root@Dasoncheng gitroot]#
##接下來 添加提交到倉庫;
[root@Dasoncheng gitroot]# git add 1.txt
[root@Dasoncheng gitroot]# git commit -m 1.txt
[master db3ea4f] 1.txt
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git status
# On branch master
nothing to commit, working directory clean
##git status倉庫狀態沒有什麼改動 須要提交的文件;  
##版本回退:
[root@Dasoncheng gitroot]# echo abcdef >> 2.txt 
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "hello"  ##-m後面能夠作備註信息;
[master 8d12096] hello
 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git log --pretty=oneline 
##一行一行的顯示全部提交記錄:
8d12096aee2e64bd4fe7da32e6ca3fb1a93ba9d7 hello
e55dd9f7b3dc681d6518c64c68c4ed409ca2f67d add 2.txt second
95137dd0029454d4d545da7977d0ab8381262499 add 2.txt
38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txt
db3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt
39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 add new file 1.txt
[root@Dasoncheng gitroot]# git reset --hard e55dd9f7
##回到以e55dd9f7開頭的記錄;
HEAD is now at e55dd9f add 2.txt second
[root@Dasoncheng gitroot]# git log --pretty=oneline 
e55dd9f7b3dc681d6518c64c68c4ed409ca2f67d add 2.txt second
95137dd0029454d4d545da7977d0ab8381262499 add 2.txt
38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txt
db3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt
39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 add new file 1.txt
[root@Dasoncheng gitroot]# git reflog 
##這裏我又想回到最新版本,一樣也是用到簡寫 但是若是忘記了怎麼破?
##使用git reflog查看全部歷史版本;
e55dd9f HEAD@{0}: reset: moving to e55dd9f7
8d12096 HEAD@{1}: commit: hello
e55dd9f HEAD@{2}: commit: add 2.txt second
95137dd HEAD@{3}: commit: add 2.txt
38cd5c5 HEAD@{4}: commit: 1.txt
db3ea4f HEAD@{5}: commit: 1.txt
39ae8b7 HEAD@{6}: commit (initial): add new file 1.txt
[root@Dasoncheng gitroot]# git reset --hard 8d12096
HEAD is now at 8d12096 hello
[root@Dasoncheng gitroot]# git log --pretty=oneline 
8d12096aee2e64bd4fe7da32e6ca3fb1a93ba9d7 hello
e55dd9f7b3dc681d6518c64c68c4ed409ca2f67d add 2.txt second
95137dd0029454d4d545da7977d0ab8381262499 add 2.txt
38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txt
db3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt
39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 add new file 1.txt
##刪除文件恢復:
[root@Dasoncheng gitroot]# rm -f 2.txt 
[root@Dasoncheng gitroot]# ll
total 4
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
[root@Dasoncheng gitroot]# git checkout  ##查看有哪些能夠恢復的;
D	2.txt
[root@Dasoncheng gitroot]# ll
total 4
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
[root@Dasoncheng gitroot]# git checkout -- 2.txt  ##恢復2.txt文件;
[root@Dasoncheng gitroot]# ll
total 8
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
-rw-r--r-- 1 root root 15 Nov  7 16:24 2.txt
##若是1.txt文件修改,add後但沒有commit,再想回退到上一次提交的狀態,怎麼破?
##可使用git reset HEAD 1.txt 清除標記,再執行git checkout -- 1.txt
[root@Dasoncheng gitroot]# echo bbb >>2.txt
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   2.txt
#
[root@Dasoncheng gitroot]# git reset HEAD 2.txt  ##去掉標記;
Unstaged changes after reset:
M	2.txt
[root@Dasoncheng gitroot]# 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:   2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Dasoncheng gitroot]# git checkout -- 2.txt  ##至關於 同步倉庫到本地;
##刪除文件:
[root@Dasoncheng gitroot]# git rm -f 2.txt  ##強制刪除本地;
rm '2.txt'
[root@Dasoncheng gitroot]# git commit -m "rm 2.txt"  ##刪除提交;
[master 73a9138] rm 2.txt
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 3 deletions(-)
 delete mode 100644 2.txt
[root@Dasoncheng gitroot]# git reflog   ##下面是刪除恢復;
73a9138 HEAD@{0}: commit: rm 2.txt
8d12096 HEAD@{1}: reset: moving to 8d12096
e55dd9f HEAD@{2}: reset: moving to e55dd9f7
8d12096 HEAD@{3}: commit: hello
e55dd9f HEAD@{4}: commit: add 2.txt second
95137dd HEAD@{5}: commit: add 2.txt
38cd5c5 HEAD@{6}: commit: 1.txt
db3ea4f HEAD@{7}: commit: 1.txt
39ae8b7 HEAD@{8}: commit (initial): add new file 1.txt
[root@Dasoncheng gitroot]# git reset --hard 8d12096
HEAD is now at 8d12096 hello

餓,個人/root/.gitconfig這個文件;鬱悶、都搜索不到!(這個文件定義了author等等)github

22.7 創建遠程倉庫

創建遠程倉庫:web

  • 首先到 https://github.com 註冊一個帳號,建立本身的git,點repositories 再點new
  • 名字自定義,好比叫studygit 選擇public 點 create repository
  • 添加key:右上角點本身頭像,選擇settings,左側選擇SSH and GPG keys
  • 左側點New SSH key,把linux機器上的~/.ssh/id_rsa.pub內容粘貼到這裏
  • 把本地倉庫推送到遠程倉庫 git remote add origin git@github.com:aminglinux/studygit.git //這一步是在遠程建立一個新的倉庫studygit,名字儘可能和本地的一致
  • git push -u origin master //而後把本地的studygit倉庫推送到遠程的studygit
  • 下一次再推送,就能夠直接 git push

建立用戶跳過:
新建倉庫(Create a new repository):
mark
提示:
mark網絡

##提示我已經拷貝出來了:
https://github.com/chengzhenge/learning.git
…or create a new repository on the command line
echo "# learning" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/chengzhenge/learning.git
git push -u origin master
…or push an existing repository from the command line
git remote add origin https://github.com/chengzhenge/learning.git
git push -u origin master
…or import code from another repository
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

新建key密鑰:
mark
推送文件到遠程倉庫:dom

[root@Dasoncheng .ssh]# mkdir /data/learning
[root@Dasoncheng .ssh]# cd /data/learning
[root@Dasoncheng learning]# git init
Initialized empty Git repository in /data/learning/.git/
[root@Dasoncheng learning]# echo "# learning" >> README.md
[root@Dasoncheng learning]# git add README.md
[root@Dasoncheng learning]# git commit -m "first commit"
[master (root-commit) 44f7db3] first commit
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[root@Dasoncheng learning]# git remote add origin git@github.com:chengzhenge/learning.git
##在遠程倉庫建立learning倉庫名;注意、區分ssh和web登陸連接;
[root@Dasoncheng learning]# git push -u origin master
##將本地倉庫learning推送到遠程learning倉庫;下次推送 就不須要以上兩步了,直接git push
Username for 'https://github.com': chengzhenge
Password for 'https://chengzhenge@github.com': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/chengzhenge/learning.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

推送成功效果:
markssh

22.8 克隆遠程倉庫

cd /home
 git clone  git@github.com:aminglinux/lanmp.git
 它提示,會在當前目錄下初始化一個倉庫,並建立一個.git的目錄,以下
 Initialized empty Git repository in /home/lanmp/.
 git/完成後,ls能夠看到一個lanmp的目錄
 cd  lanmp 
 vi lanmp.sh 編輯一下文件,而後提交
 git add lanmp.sh
 git commit -m "sdlfasdf" 
 而後再推送到遠程服務端
 git push

獲取git克隆連接:
mark
mark
克隆倉庫並推送push文件:分佈式

[root@Dasoncheng ~]# cd /home/
[root@Dasoncheng home]# git clone git@github.com:chengzhenge/learning.git
Cloning into 'learning'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
[root@Dasoncheng home]# cd learning/
[root@Dasoncheng learning]# ll
total 8
-rw-r--r-- 1 root root  7 Nov  7 19:27 1.txt
-rw-r--r-- 1 root root 11 Nov  7 19:27 README.md
[root@Dasoncheng learning]# echo 'hello' >> 2.txt
[root@Dasoncheng learning]# git add 2.txt
[root@Dasoncheng learning]# git commit -m "add a file"
[master 9ff9124] add a file
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@Dasoncheng learning]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:chengzhenge/learning.git
   85bf4c1..9ff9124  master -> master

mark 拉取文件:
markide

[root@Dasoncheng learning]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:chengzhenge/learning
   9ff9124..ae89ab1  master     -> origin/master
Updating 9ff9124..ae89ab1
Fast-forward
 3.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 3.txt
[root@Dasoncheng learning]# ll
total 16
-rw-r--r-- 1 root root  7 Nov  7 19:27 1.txt
-rw-r--r-- 1 root root  6 Nov  7 19:28 2.txt
-rw-r--r-- 1 root root 12 Nov  7 19:34 3.txt
-rw-r--r-- 1 root root 11 Nov  7 19:27 README.md
相關文章
相關標籤/搜索