3)GIT:GIT由linux之父linus于2005年开发,在结构上比起SVN和CVS有很大的提升。可以说GIT是世界上目前最优秀的版本控制系统之一。GIT的每个功能都作为一条独立的命令,导致git庞大的命令集,但这并不妨碍各大程序人员对于GIT的喜爱,原因就在于它一个分布式的版本控制系统。此外:GIT虽然是基于linux操作系统开发的,但目前已经可以跨平台运行在多种操作系统之上,包括linux,MAC OS X,Windows等。
[root@servera workspace]# echo test >> welcome.txt [root@servera workspace]# ls welcome.txt [root@servera workspace]# cat welcome.txt hello test [root@servera workspace]# ls welcome.txt [root@servera workspace]# git diff welcome.txt diff --git a/welcome.txt b/welcome.txt index ce01362..b2b9cc9 100644 --- a/welcome.txt +++ b/welcome.txt @@ -1 +1,2 @@ hello +test
2)git status:用来查看改过的内容
[root@servera workspace]# 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: welcome.txt # no changes added to commit (use "git add" and/or "git commit -a") [root@servera workspace]#
3)git log: 用来查看历史提交的日志
[root@servera workspace]# git log commit a7a778652b1457b838beeca66e4334c35d97e826 Author: carol <lijiayi@uplooking.com> Date: Sun Nov 20 07:01:51 2016 -0500
first
4)git reset 用来做回滚
恢复工作区的文件到上一个提交的版本:
[root@servera workspace]# echo test >> welcome.txt [root@servera workspace]# cat welcome.txt hello test [root@servera workspace]# git add welcome.txt [root@servera workspace]# git commit -m "second" # 以上的操作为再添加一个版本 [master 0d10fd9] second 1 file changed, 1 insertion(+) [root@servera workspace]# cat welcome.txt hello test [root@servera workspace]# git reset --hard HEAD^ # ^代表上一个版本,^^代表上上一个版本,当然往上100个版本写100个^比较容易数不过来,所以可以写成HEAD~100 HEAD is now at a7a7786 first [root@servera workspace]# ls welcome.txt [root@servera workspace]# cat welcome.txt hello
恢复工作区到指定版本:
[root@servera workspace]# echo testok >> welcome.txt [root@servera workspace]# echo testok >> welcome.txt [root@servera workspace]# git add welcome.txt [root@servera workspace]# git commit -m "third" # 这里再添加一个版本 [master 3c3b1d9] third 1 file changed, 2 insertions(+) [root@servera workspace]# git log --graph --oneline # 可以通过该参数查看到每个操作对应的ID * 3c3b1d9 third * a7a7786 first [root@servera workspace]# git reset --hard a7a7786 HEAD is now at a7a7786 first [root@servera workspace]# ls welcome.txt [root@servera workspace]# cat welcome.txt hello
5)git reflog用来查看历史记录,可以结合reset完成恢复数据的操作
[root@servera workspace]# git reflog a7a7786 HEAD@{0}: reset: moving to a7a7786 3c3b1d9 HEAD@{1}: commit: third a7a7786 HEAD@{2}: reset: moving to HEAD^ 0d10fd9 HEAD@{3}: commit: second a7a7786 HEAD@{4}: reset: moving to HEAD^ 4d65b14 HEAD@{5}: commit: second a7a7786 HEAD@{6}: commit (initial): first [root@servera workspace]# cat welcome.txt hello [root@servera workspace]# git reset --hard HEAD@{1} HEAD is now at 3c3b1d9 third [root@servera workspace]# ls welcome.txt [root@servera workspace]# cat welcome.txt hello testok testok
6)git checkout 可以丢弃工作区的修改
[root@servera workspace]# echo carol >> welcome.txt [root@servera workspace]# git add welcome.txt [root@servera workspace]# cat welcome.txt hello testok testok carol #重新提交一个修改后的文件至工作区 [root@servera workspace]# git reset HEAD welcome.txt #如果已经提交给缓存区,则必须要做这步 Unstaged changes after reset: M welcome.txt [root@servera workspace]# git checkout -- welcome.txt [root@servera workspace]# cat welcome.txt hello testok testok
[root@servera ~]# git clone /root/workspace/ /backup Cloning into '/backup'... done. [root@servera ~]# cd /backup/ [root@servera backup]# ls testfile
2)拉取和推送
推送: [root@servera workspace]# git push /backup 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), 272 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable t remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int remote: error: its current branch; however, this is not recommended unless you remote: error: arranged to update its work tree to match what you pushed in som remote: error: other way. remote: error: remote: error: To squelch this message and still keep the default behaviour, se remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To /backup ! [remote rejected] master -> master (branch is currently checked out) error: failed to push some refs to '/backup'
[root@foundation0 ~]# mkdir /test [root@foundation0 ~]# cd /test/ [root@foundation0 test]# ls [root@foundation0 test]# git clone https://github.com/atheling004/test1.git Cloning into 'test1'... remote: Counting objects: 7, done. remote: Compressing objects: 100% (4/4), done. remote: Total 7 (delta 1), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (7/7), done. [root@foundation0 test]# ls test1 [root@foundation0 test]# cd test1/ [root@foundation0 test1]# ls hello testfile zazazazazazaza [root@foundation0 test1]#
4.在本地版本库发生变更后上传
[root@foundation0 test1]# cat 1 testfile [root@foundation0 test1]# git add 1 [root@foundation0 test1]# git commit -m 1 [root@foundation0 test1]# git push https://github.com/atheling004/test1.git 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)
Username for 'https://github.com': atheling004 Password for 'https://atheling004@github.com': Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local objects. To https://github.com/atheling004/test1.git 33a62ba..85c6527 master -> master