git的工具基本使用

摘要

本文简单介绍了 git 工具的基本,简单的使用,基本的一些操作,帮助我来快速构建自己的github 仓库。

1 git 是什么


Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。


2  安装git 

https://github.com/  注册一个账号  ,首先到github 官网上 注册一个账号。
在 centos 上安装 
yum install -y epel-release
yum install -y git
配置用户名,以及邮箱
git config  --global  user.name "abiao"
git config  --global  user.email "[email protected]"
此时 cat .gitconfig   里面已经有一些 东西,就是刚刚配置的 用户名以及email 


查看 配置信息
git config  --list 




3 创建版本仓库并推送文件。

mkdir /home/gitroot
cd /home/gitroot
git init       // 可以把当前目录变成 git 可以管理的仓库。
ls  -al      可以看见多了一个.git 的目录


查看状态当前 库的状态,是否有文件改动等
git  status 




4 版本变更


1) git  add  , git commit  等操作

2)查看所有提交的 git 仓库的记录操作
git log  

一行显示 ,git 仓库的记录
git log  --pretty=oneline


3)  版本 回退
git  reset --hard   ****          *** 代表 git log  第一列的内容,当然可以取前几位数字。


4)显示所有的版本
git reflog   // 显示所有的版本。

5 文件恢复

有时候 你修改了一个文件(1.txt),发现改的不对, 想恢复到上一次的提交状态. 或者不小心 删除了1.txt 


1)正常的一个流程编辑 vi  1.txt  ====>   git add  ===>  git commit  
此时恢复可以用  git  checkout  --  1.txt  



2)还有一种情况: 
(add  但是没有提交, git reset HEAD  文件名)
vim  1.txt   ---> add    ,(没有commit )之后想恢复  ,  
首先:     git reset  HEAD  1.txt
之后再:     git checkout --  1.txt
此时文件就恢复了。


6 文件删除 

先增加一个test  ,提交
cp 1.txt   test
git add test
git commit -m "add new file test" test
现在删除它 (3步)
rm -f test  ,  此时如果此时想要恢复的话,  git checkout  -- test
git rm test
git commit -m "delete file test"  test
这样就彻底删除了。

7 创建 一个远程 仓库 github


在本地重新建一个仓库,和远程仓库名字一样。

在本地先生成一对秘钥对 ssh-keygen       来生成秘钥  一路按enter ,生成秘钥
把公钥 id_rsa.pub 粘贴到 ssh key 框里面。  

点击 new  SSH KEY  把公钥粘贴到下面的框里面 就可以了。 


cd ..
到home 目录下面
git remote add origin https://github.com/changyubiao/studygit.git
发现报错
fatal: Not a git repository (or any of the parent directories): .git
可以手动创建一个和远程一样名字的仓库
mkdir studygit
cd studygit
git init
此时可以把本地的studygit  推送到远程 的studygit
git remote add origin https://github.com/changyubiao/studygit.git
下面来创建一个文件测试
vim aming.txt
git add aming.txt
git commit -m "add aming.txt"
之后要推送到远程,用下面的命令(第一次用的话,之后直接 git push)
git push  -u origin master
出现 以下错误:(图片)


error: The requested URL returned error :403  Forbidden white accessing  https://github.com/*****




解决方案:

vim .git/config
修改
[remote "origin"]
       url = https://github.com/changyubiao/studygit.git
修改为:
   url = https://[email protected]/changyubiao/studygit.git
 
 或者为: 
    url =  [email protected]/changyubiao/studygit.git
     注释:这一种方式同步的时候,不用输入密码。
之后再 push
git push -u origin master
Password:   输入你github 的密码,就可以了。
此时到 网站查看,studygit  发现已经有了aming.txt



此时我修改一下 aming.txt
vim  aming.txt
git add aming.txt
git commit -m "change  aming.txt "
推送到远程
git push
Password :  输入 密码即可。




8 克隆一个远程 仓库

cd  /home
git clone [email protected]:changyubiao/lamp.git


[email protected]:changyubiao/lamp.git  远程仓库的地址
此时可以 ,看到 ls /home   下面有 lamp  项目了,  cd lamp/
可以看到项目的文件, 以及相关的配置。
vim  hello.sh
git add hello.sh
git commit -m "add 111111111"
此时 文件在本地库里面了,之后可以push  到远程去
git push
就可以查看了。



9  分支管理 

1) 分支创建,切换
查看分支
git  branch 

# 创建分支
git branch  abiao


# 切换分支
git  checkout  abiao   // 切换到abiao 分支下面

git branch  查看 ,就会看到有两个分支master 和abiao  ,当前使用的分支 前面会有一个*


# 切换到master 分支
git  checkout  master 



创建分支
 git branch abiao
[root@myCentOS studygit]# git branch
  abiao
* master
查看有两个分支,中有* 代表当前正在使用的分支。当然也可以切换分支
git checkout abiao
[root@myCentOS studygit]# git branch
* abiao
  master
此时已经切换到abiao分支。
下面 创建一个文件
vim abiao.txt
git add abiao.txt
git commit -m "add abiao file."
切换分支
git checkout  master
[root@myCentOS studygit]# ls
aming.txt
发现只有 aming.txt  而没有 abiao.txt
切换分支到abiao,
git checkout abiao
[root@myCentOS studygit]# ls
abiao.txt
发现里面是有abiao.txt 这就是分支的作用。



2) 分支的合并与删除
新建分支:aming  abiao
git  branch  aming
git branch  abiao


切换分支
切换到 aming  分支
git  checkout  aming   


合并分支要注意:
 比如把 aming  分支合并到 maser 下,   应该在 maser 分支 进行
查看当前分支:  git  branch          有* 号代表当前所在分支。
 git merge aming     //  这样就把 aming  分支下面的内容合并到maser 下面了。


删除分支: 
 git  branch  -d  aming 
 git  branch -D  aming    (强制删除,如果还没有合并分支,就删除删除。)


下面来看看 :分支使用原则(下面的截图)



10 现场保留 

当你 正在进行 项目中的某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他的分支上进行一些工作,问题是 ,你现在不想提交进行一半的代码,否则以后 你无法回到这个工作点,  这个时候就需要用 现场保留。 用  git  stash  命令


当我们编辑一个文档的时候, 正在进行一半的时候,突然要去另外的一个分支,修复bug ,此时可以 
git add  hello.c
git stash
之后去修复bug ,完成后,
我们可以 在回复现场,
先查询一下    git  stash  list 
git stash list
stash@{0}: WIP on abiao: d8e9cc1 delete hello.cpp


恢复现场:
               
 git  stash  apply  stash@{0}
这样就恢复了。
ls   ;  vim  hello.c  继续编辑。  



11  标签管理

标签管理类似 快照功能,可以先给版本打一个标签,记录 某个时刻库的状态。 也可以随时恢复到改状态。 


git  checkout   master 
git  tag  v1.0 
查看 所有的tag 
git  tag 


git log  --pretty=oneline  --abbrev-commit




git log  --pretty=oneline  --abbrev-commit
f874b98 tiao jiao 1.txt
b4aac73 second commit add  1.txt
a5d6075 delete 1.txt
c589fb0 Merge branch 'abiao'
860330c add new hello.cpp
4de51fa Merge branch 'abiao'
4ffeead insert 1111 on the top
72610e4 master modify 1.txt
a1a44bf Merge branch 'abiao'
5f2e454 add new file 1.txt
bea3713 Merge branch 'aming'
2942b01 add new file aming.txt
69617a9 add abiao.txt
f8711e7 renew  ...
191c256 change master.txt
a4f6777 add  abiao.txt
d7e1e7a Merge branch 'aming'
4835e38 add aming.txt


给历史版本 打标记。
git  tag  v0.8  c589fb0
当然也可以添加标签的同时,对标签进行描述
git tag -a v0.3 -m "初始版本,基本实现功能."   503fb33
然后 把本地标签 推送 到远程
git push origin v0.3



删除标签
git  tag -d  v0.5


推送标签 到远程主机:git push origin  v1.0
[root@myCentOS studygit]# git push origin  v1.0
Password:
Total 0 (delta 0), reused 0 (delta 0)
To https://[email protected]/changyubiao/studygit.git
* [new tag]         v1.0 -> v1.0


当然也可以推送所有的标签  git push --tag origin
[root@myCentOS studygit]# git push --tag origin
Password:
Total 0 (delta 0), reused 0 (delta 0)
To https://[email protected]/changyubiao/studygit.git
* [new tag]         v0.5 -> v0.5
* [new tag]         v0.8 -> v0.8




当然也可以删除远程标签
先删除本地的标签,在删除远程标签。
git tag -d v0.5
git push origin :refs/tags/v0.5


[root@myCentOS studygit]# git push origin :refs/tags/v0.5
Password:XXXXXXXXX
To https://[email protected]/changyubiao/studygit.git
- [deleted]         v0.5
从网页上查看一下。


已经删除了。。


总结: tag  用来 发布版本的,tag  是针对 commit 来打标签的,所以 可以针对历史的commit 来打tag

12 遇到的一些问题

git remote add origin  https://[email protected]/changyubiao/test.git


1)遇到错误:
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/changyubiao/lanmp.git/info/refs
解决方案:
vim .git/config









2)出现错误的主要原因是github中的README.md文件不在本地代码目录中

解决方案:
 git pull --rebase origin master


此时 执行上面代码后可以看到本地代码库中多了README.md文件


猜你喜欢

转载自blog.csdn.net/u010339879/article/details/78478227