Git 的使用(一)

Git教程

怎样使用 GitHub?

多人管理:http、git以及ssh    浅谈Github多人合作开发    GitHub 实现多人协同提交代码并且权限分组管理、 

Git 版本管理 经验:[2]本地登录SSH认证  

建立多人协作git仓库/git 仓库权限控制(SSH)

简介

掌握基本概念、原理---会常规使用

掌握大部分感念、原理---熟练、高效、多样化使用

常见:GIt(分布式) SVN(集中式)

Git是一款免费、开源的分布式版本控制系统,付费用户可以建私人仓库

github官网:https://github.com/

Git工作原理

工作区(working directory) 版本库/仓库(repository)        

仓库中有:暂存区(index / stage)、 master

 

在初始化git版本库之后会生成一个隐藏的文件 .git (可以理解为repository)

.git中有一个文件index(可以叫stage 或者 叫缓存区) 暂存区只是一个临时保存修改文件的地方。

git还为我们自动生成了一个分支master以及指向该分支的指针head

参考:git工作区、暂存区、版本库之间的关系

Git使用(GitHub官网直接创建项目):

1、登陆github后,点击右上角加号,选择“New repository”

2、填写好你的仓库名称(Repository name)、描述(Description),选择Public

Git使用(GUI):mac下 GItHub / GitHub Desktop

利用客户端软件在本地自定义文件夹GitHub下面新建项目LYHTest

提交后如果弹出,与线上冲突,解决冲突,重新提交 

操作如下:

Git使用(终端):

使用场景一:

已在github上创建了一个项目Demo,链接为https://github.com/liyonghuaxin/Demo.git

在本地新建一个目录 myGitHub

1、进入目录    cd myGitHub    

2、关联远程仓库    git remote add origin https://github.com/liyonghuaxin/Demo.git

  如果报错 fatal: remote origin already exists.输入 git remote rm origin 重新关联

3、克隆远程仓库内容    git clone https://github.com/liyonghuaxin/Demo.git

4、在本地做各种修改

5、将本地修改加到暂存区    git add . 或者 git add <具体文件名>

6、查看工作区是否都提交到暂去区了 git status

7、将暂存区提交到master分支    git commit -m "修改信息"

8、查看暂存区是否都提交到master分支了 git status

9、从远程仓库拉去    git pull origin master

10、将master分支内容提交到远程仓库 git push -u origin master

多人合作时push之前强烈建议先pull一下

使用场景二:

push前没pull,提交到远程仓库(git push -u origin master)发生冲突:

failed to push some refs to 'https://github.com/liyonghuaxin/haha.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决办法:

1、拉取远程仓库内容  git pull origin master

2、到具体文件解决冲突

3、从 git add  这一步开始,重新提交本地修改内容

部分命令说明:

git add . 添加所有文件   或者    git add <文件名>  添加某个文件 (将文件提交到缓存区)

git commit –m “修改信息”    提交本次事务,即将add的文件提交到git仓库(将缓存区提交到分支master下面)

git status    显示提交的状态:已经添加,等待提交事务的文件(绿色字体表示);已经改变但是没有添加(not staged)的文件(红色字体表示);

PS:查看状态: 文件的修改在工作区,显示红色;修改提交到了缓存区,显示绿色;所有内容提交到分支,显示“nothing to commit, working tree clean”。

git diff 比较的是工作区和暂存区的差别
git diff --cached 比较的是暂存区和版本库的差别
git diff HEAD 可以查看工作区和版本库的差别

PS:如果只是在工作区新建一个文件,那么输入这三个命令都没有结果为空白。

git log       可以查看提交历史,

git reflog    查看命令历史,以便确定要回到未来的哪个版本。

git reset --hard commit_id    Git允许我们在版本的历史之间穿梭,使用命令

其它

关于SSH:git终端使用教程

关于分支:Git分支管理和冲突解决

Git工作流指南:Pull Request工作流

Git使用总结

Git使用总结(包含Git Bash和Git GUI的使用)

git 终端使用方法

原文地址:https://www.cnblogs.com/liyonghua/p/8203056.html