Git使用总结
一,原理
首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的,也是Linux系统开发所用到的分布式版本控制软件。一说到分布式,其实就是每个电脑都有这么个版本库,而摒弃了传统的中心文件系统服务,所以每个人都能保存、还原之前的版本。在航天二院实习的时候,mentor让我改一个软件的Bug,由于国企没有外网,所以不能用github,院里面也没有搭建自己的Git文件服务器,所以每次改Bug之前都要备份一个之前的版本,以日期命名,而且后面还容易忘记那个版本有哪些功能模块,就我们两个人开发一个软件都把版本搞错了好几次,真心头痛。
二,Git常用命令
git的常用命令有:init、add、rm、mv、commit、push、pull、clone、log、checkout、megre、status、branch、diff、config、remote、fetch、reset、tag、show、stash、grep、rebase、gc等。
1,本地操作
Git命令 | 解释 | 撒旦法备注 |
git init | 初始化Git环境 | 先cd到要管理的文件夹中 |
git add *.txt / . | 添加文件到暂存区 | . 是将所有文件都添加到暂存区 |
git status | 查看文件的状态 | |
git commit -m 'First commit' | 提交更新到版本库 | -m 是message的缩写,即每次添加更新的备注 |
git log | 查看提交更新的记录 | |
git rm *.txt | 删除文件 | |
git mv a.txt b.txt | 修改文件名 | 将a.txt的文件名改为b.txt |
2,远程操作
Git命令 | 解释 | 撒旦法备注 |
git config --global user.name "your name" | 设置username,因为github每次commit都会记录他们 | github注册时的用户名 |
git config --global user.email "your_email@youremail.com" | 设置email | github注册时的邮箱地址 |
git remote add origin https://...git | 查看文件的状态与Github建立远程连接 | 现在github网站上建立仓库 |
git push -u origin master | 提交推送到Github的仓库中 | master分支可以是其他分支 |
在远程操作Github之前要进行ssh key的配置,网上有很多教程可以参考。大概分三步:
-
在本地创建ssh key,$ ssh-keygen -t rsa -C "your_email@youremail.com" (github网站上注册的邮箱),回车后会在当前用户.ssh文件夹下生成id_rsa、id_rsa.pub、known_hosts三个文件
-
打开id_rsa.pub,复制里面的key, 回到github网站,进入Account Settings,左边选择SSH Keys,Add SSH Key, Title随便填,粘贴key。
-
验证是否成功,在本地git bash下输入,$ ssh -T git@github.com ,回车就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
参考链接:
git中sshkey有何作用?
http://www.bootcss.com/p/git-guide/
Git命令脑图(图片来自Github)
3,Git分支管理模型Pic
三,Demo
1,初始化一个git仓库,新建一个code.txt文件,提交到git版本库,现在git开始管理code.txt文件了
ckjbug@ckjbug MINGW64 ~/Desktop $ mkdir gituse ckjbug@ckjbug MINGW64 ~/Desktop $ cd gituse ckjbug@ckjbug MINGW64 ~/Desktop/gituse $ git init Initialized empty Git repository in C:/Users/Enz/Desktop/gituse/.git/ ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) code.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git commit -m 'First commit' [master (root-commit) 0f67f2c] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master nothing to commit, working tree clean
2,修改文件,在code.txt添加一行代码,然后用checkout回到修改前(即没有添加代码前的版本,这里之前写入的代码没有了,空文本),这只是checkout的一种用法,更多是后面学习的git的分支转换。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ 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: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout -- code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ notepad code.txt
3,还是上面这个功能,回到文本修改前的状态,但是我们修改文件后已经添加到暂存区了,这时使用git checkout code.txt已经没用了,可以用git reset回到没有添加到暂存区的状态(最初始的状态)
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ clear ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ 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: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git reset Unstaged changes after reset: M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ 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: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout -- code.txt
4,添加、切换、删除分支(添加develop开发者的工作分支)
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop Switched to branch 'develop' M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master Switched to branch 'master' M code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch -d develop Deleted branch develop (was 0f67f2c). ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master
5,新建一个开发者develop分支,然后添加develop.txt文本,添加到暂存库,提交,最后转换到master分支,发现develop.txt文本不见了,这就是版本管理,每个分支控制一个版本,当我们转回到develop分支下时这个devel.txt文件又出现了。如果我们在develop分支下修改了master分支的code.txt,当我们转换为master分支上时,不会出现develop分支下的修改。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse $ git init Initialized empty Git repository in C:/Users/Enz/Desktop/gituse/.git/ ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) code.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git add . ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git commit -m 'First commit' [master (root-commit) 2571887] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git status On branch master nothing to commit, working tree clean ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch * master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git branch develop ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop //之后添加一个develop.txt文件 Switched to branch 'develop' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop Untracked files: (use "git add <file>..." to include in what will be committed) develop.txt nothing added to commit but untracked files present (use "git add" to track) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git add develop.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git commit -m 'add a develop_txt on branch develop' [develop 2fd0d60] add a develop_txt on branch develop 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 develop.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git branch * develop master ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master Switched to branch 'master' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git checkout develop //在develop分支下修改master分支下的code.txt文件 Switched to branch 'develop' ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop 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: code.txt no changes added to commit (use "git add" and/or "git commit -a") ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git add code.txt ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git commit -m '在develop分支下修改了master分支的code.txt文件' [develop 903c8e0] 在develop分支下修改了master分支的code.txt文件 1 file changed, 1 insertion(+) ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git status On branch develop nothing to commit, working tree clean ckjbug@ckjbug MINGW64 ~/Desktop/gituse (develop) $ git checkout master //此时code.txt修改的代码不在了 Switched to branch 'master'
6,融合分支,在master分支上时,将develop分支merge到master分支上。这时develop.txt文件出现了。即融合到了master分支中。
ckjbug@ckjbug MINGW64 ~/Desktop/gituse (master) $ git merge develop Updating 2571887..903c8e0 Fast-forward code.txt | 1 + develop.txt | 0 2 files changed, 1 insertion(+) create mode 100644 develop.txt
7,Merge时冲突的处理,加入两个不同分支同时修改了主分支下code.txt中的第三行代码,两个分支在融合时会冲突。
8,远程推送push、拉取pull项目代码,在实际项目中,往往已经存在了远程的项目,当你参与到项目的开发当中时,
https://blog.csdn.net/weijinqian0/article/details/76217014
相关链接: