最近在研究puppet的企业架构部署,补充下自己的git方面的知识。
位置:
cd /home/nginx git init
建立单纯的git仓库:
cd /var/nging.git git clone --bare /home/nginx /var/nginx.git
我们搭建git学习环境:
cd /home/nginx git add * git commit -a -m "init" git remote add origin /var/nginx.git git push origin master:master
另一个副本目录:
git clone /var/nginx.git /opt/mynginx
至此,git环境应搭建完毕。
git仓库为/var/nginx.git
git的副本目录为:/home/nginx /opt/mynginx
git语法学习:
1:往git仓库中添加文件
在其中一个副本目录中操作:
cd /home/nginx touch 1.txt git add 1.txt git commit -a -m 'add 1.txt' git push origin 另一个副本目录: cd /home/mynginx git pull origin master ―-->把远程的仓库中的下载到本地,类似的svn up 注意: git fetch origin ---->只下载变更信息,相当于pull的过程的前一部分。
增删改,和添加文件操作类似。
2: 创建分支
cd /home/nginx git branch ----->查看分支 git branch test ----->新建test分支 git checkout test ---->切换到test分支
在test分支中,进行一次创建文件的操作:
[root@192 nginx]# git checkout test Switched to branch 'test' [root@192 nginx]# touch 10.txt [root@192 nginx]# git add 10.txt [root@192 nginx]# git commit -a -m 'add 10.txt' [test e517336] add 10.txt Committer: root <[email protected]> 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 [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <[email protected]>' 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 10.txt [root@192 nginx]# ls 10.txt 10.txt [root@192 nginx]# git checkout master Switched to branch 'master' [root@192 nginx]# ls 10.txt ls: cannot access 10.txt: No such file or directory [root@192 nginx]# git merge test master Trying simple merge with test Already up-to-date with master Merge made by octopus. 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 10.txt [root@192 nginx]# ls 10.txt 10.txt [root@192 nginx]# git push origin Counting objects: 4, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 363 bytes, done. Total 3 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To /var/ngint.git/ 8c40602..c66f7c2 master -> master 这时就能在另一个副本,下载最新文件了: [root@192 nginx]# cd /opt/mynginx/ [root@192 mynginx]# git pull origin remote: Counting objects: 4, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From /var/ngint 8c40602..c66f7c2 master -> origin/master Updating 8c40602..c66f7c2 Fast-forward 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 10.txt [root@192 mynginx]#
“branch分支传递”:
[root@192 nginx]# git branch test2 [root@192 nginx]# git branch * master test test2 [root@192 nginx]# git push origin test2:test2 Counting objects: 30, done. Compressing objects: 100% (25/25), done. Writing objects: 100% (28/28), 2.87 KiB, done. Total 28 (delta 14), reused 0 (delta 0) Unpacking objects: 100% (28/28), done. To /var/ngint.git/ * [new branch] test2 -> test2 [root@192 mynginx]# git fetch origin remote: Counting objects: 30, done. remote: Compressing objects: 100% (25/25), done. remote: Total 28 (delta 14), reused 0 (delta 0) Unpacking objects: 100% (28/28), done. From /var/ngint * [new branch] test2 -> origin/test2 [root@192 nginx]# cd /opt/mynginx [root@192 mynginx]# git checkout -b test2 origin/test2 Branch test2 set up to track remote branch test2 from origin. Switched to a new branch 'test2' [root@192 mynginx]# git branch master test * test2
回退:
1:修改完后没有add操作的回退
[root@192 nginx]# echo "haha" >> 3.txt [root@192 nginx]# cat 3.txt this is just a test! haha [root@192 nginx]# git checkout 3.txt [root@192 nginx]# cat 3.txt this is just a test! [root@192 nginx]#
2:已经add操作的回退
[root@192 nginx]# echo "haha" >> 3.txt [root@192 nginx]# cat 3.txt this is just a test! haha [root@192 nginx]# git add 3.txt [root@192 nginx]# cat 3.txt this is just a test! haha [root@192 nginx]# git reset --hard HEAD HEAD is now at 7d31256 add 20.txt [root@192 nginx]# cat 3.txt this is just a test! [root@192 nginx]#
3:已经commit操作的回退
[root@192 nginx]# echo "gaga" >> 3.txt [root@192 nginx]# cat 3.txt this is just a test! this is just a test! hahhhh gaga [root@192 nginx]# git add 3.txt ;git commit -a -m 'modify 3.txt' [master be0ec23] modify 3.txt Committer: root <[email protected]> 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 [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <[email protected]>' 1 files changed, 1 insertions(+), 0 deletions(-) [root@192 nginx]# git log | head -20 commit be0ec23cb49cc386533ff3512a21690cb89190cc Author: root <[email protected]> Date: Fri Apr 10 16:33:32 2015 +0800 modify 3.txt [root@192 nginx]# git revert be0ec23cb49cc386533ff3512a21690cb89190cc [root@192 nginx]# cat 3.txt this is just a test! this is just a test! hahhhh
git stash:
如果你不想提交现有的更改,而是暂时存储到别的地方,可以将修改暂时“隐藏储存”起来。而不会影响之后的提交操作。
[root@192 nginx]# git stash Saved working directory and index state WIP on master: 061f1b7 Revert "Revert "Revert "add 20.txt""" HEAD is now at 061f1b7 Revert "Revert "Revert "add 20.txt""" [root@192 nginx]# git stash list stash@{0}: WIP on master: 061f1b7 Revert "Revert "Revert "add 20.txt""" [root@192 nginx]# git status # On branch master nothing to commit (working directory clean) [root@192 nginx]# touch 5.txt 6.txt [root@192 nginx]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # 5.txt # 6.txt nothing added to commit but untracked files present (use "git add" to track) [root@192 nginx]# git stash apply # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: 30.txt # new file: 4.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # 5.txt # 6.txt [root@192 nginx]# git log --abbrev-commit --pretty=oneline 061f1b7 Revert "Revert "Revert "add 20.txt""" 59e0054 Revert "modify 3.txt" [root@192 nginx]# git show 061f1b7
参考资料:
http://my.oschina.net/u/877348/blog/152660
http://blog.csdn.net/hustpzb/article/details/7287967