分布式:简单的说、指的是不须要网络 能够本地单机使用!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
创建远程仓库:web
建立用户跳过:
新建仓库(Create a new repository):
提示:
网络
##提示我已经拷贝出来了: 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密钥:
推送文件到远程仓库: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.
推送成功效果:
ssh
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克隆连接:
克隆仓库并推送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
拉取文件:
ide
[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