博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
22.5 单机上使用git(上)
阅读量:7040 次
发布时间:2019-06-28

本文共 12626 字,大约阅读时间需要 42 分钟。

hot3.png

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 initInitialized 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 
Your name and email address were configured automatically basedon 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.comAfter 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
..." to update what will be committed)# (use "git checkout --
..." 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.txtindex 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
Your name and email address were configured automatically basedon 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.comAfter 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 masternothing 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 helloe55dd9f7b3dc681d6518c64c68c4ed409ca2f67d add 2.txt second95137dd0029454d4d545da7977d0ab8381262499 add 2.txt38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txtdb3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 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 second95137dd0029454d4d545da7977d0ab8381262499 add 2.txt38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txtdb3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 add new file 1.txt[root@Dasoncheng gitroot]# git reflog ##这里我又想回到最新版本,同样也是用到简写 可是如果忘记了怎么破?##使用git reflog查看所有历史版本;e55dd9f HEAD@{0}: reset: moving to e55dd9f78d12096 HEAD@{1}: commit: helloe55dd9f HEAD@{2}: commit: add 2.txt second95137dd HEAD@{3}: commit: add 2.txt38cd5c5 HEAD@{4}: commit: 1.txtdb3ea4f HEAD@{5}: commit: 1.txt39ae8b7 HEAD@{6}: commit (initial): add new file 1.txt[root@Dasoncheng gitroot]# git reset --hard 8d12096HEAD is now at 8d12096 hello[root@Dasoncheng gitroot]# git log --pretty=oneline 8d12096aee2e64bd4fe7da32e6ca3fb1a93ba9d7 helloe55dd9f7b3dc681d6518c64c68c4ed409ca2f67d add 2.txt second95137dd0029454d4d545da7977d0ab8381262499 add 2.txt38cd5c5e68b46f97f71bf79840b9d1cfe234430c 1.txtdb3ea4f72dd261bba44cddd6e9dad000692b6704 1.txt39ae8b724ca64fb7ae8dddc71fffdccd1674cb10 add new file 1.txt##删除文件恢复:[root@Dasoncheng gitroot]# rm -f 2.txt [root@Dasoncheng gitroot]# lltotal 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]# lltotal 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]# lltotal 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
..." 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
..." to update what will be committed)# (use "git checkout --
..." 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
Your name and email address were configured automatically basedon 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.comAfter 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.txt8d12096 HEAD@{1}: reset: moving to 8d12096e55dd9f HEAD@{2}: reset: moving to e55dd9f78d12096 HEAD@{3}: commit: helloe55dd9f HEAD@{4}: commit: add 2.txt second95137dd HEAD@{5}: commit: add 2.txt38cd5c5 HEAD@{6}: commit: 1.txtdb3ea4f HEAD@{7}: commit: 1.txt39ae8b7 HEAD@{8}: commit (initial): add new file 1.txt[root@Dasoncheng gitroot]# git reset --hard 8d12096HEAD is now at 8d12096 hello

饿,我的/root/.gitconfig这个文件;郁闷、都搜索不到!(这个文件定义了author等等)

22.7 建立远程仓库

建立远程仓库:

  • 首先到 注册一个账号,创建自己的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 :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 lineecho "# learning" >> README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin https://github.com/chengzhenge/learning.gitgit push -u origin master…or push an existing repository from the command linegit remote add origin https://github.com/chengzhenge/learning.gitgit push -u origin master…or import code from another repositoryYou 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 initInitialized 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 
Your name and email address were configured automatically basedon 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.comAfter 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 pushUsername for 'https://github.com': chengzhengePassword 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 -> masterBranch 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.gitCloning 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 0Receiving objects: 100% (6/6), done.[root@Dasoncheng home]# cd learning/[root@Dasoncheng learning]# lltotal 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 
Your name and email address were configured automatically basedon 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.comAfter 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 pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use: git config --global push.default matchingTo squelch this message and adopt the new behavior now, use: git config --global push.default simpleSee '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 pullremote: Counting objects: 3, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.From github.com:chengzhenge/learning   9ff9124..ae89ab1  master     -> origin/masterUpdating 9ff9124..ae89ab1Fast-forward 3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 3.txt[root@Dasoncheng learning]# lltotal 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

你可能感兴趣的文章
小猿圈linux之linux基础命令大全(一)
查看>>
当经历所有大厂的实习面试过后
查看>>
从BEC“代币蒸发”事件看智能合约编写注意事项
查看>>
CentOS 7 Minimal 安装 LXQT
查看>>
机器码 指令 汇编语言 的关系
查看>>
摸索 JS 内深拷贝的最佳实践
查看>>
设计师面试会遇到的问题(part1:HR篇)
查看>>
周记_
查看>>
去掉UIPickerView的弯曲弧度
查看>>
使阿里oss实现前端代码自动上传
查看>>
JavaScript中的作用域和闭包
查看>>
暴力破解WiFi密码
查看>>
Zend Studio使用教程:使用Zend Studio和Zend Server进行根本原因分析 (二)
查看>>
golang的fmt包String(),Error(),Format(),GoString()的接口实现
查看>>
Java技术转(兼顾)产品经理——读《快速转行做产品经理》有感
查看>>
成为优秀Java开发人员的10件事
查看>>
Kali Linux安装教程
查看>>
Android缓存处理
查看>>
JavaScript 数据类型检测终极解决方案
查看>>
年赚百万游戏主播!玩转Python后:几行代码轻松“吃鸡” 附源码
查看>>