22.5 单机上使用git(上)

22.5 单机上使用git

分布式:简单的说、指的是不需要网络 可以本地单机使用!

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"

[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等等)

22.7 建立远程仓库

建立远程仓库:

  • 首先到 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
推送文件到远程仓库:

[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.

推送成功效果:
mark

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拉取文件:
mark

[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

转载于:https://my.oschina.net/u/3651233/blog/1555920

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值