美文网首页
Git使用笔记

Git使用笔记

作者: 青楼画技 | 来源:发表于2018-11-28 17:43 被阅读0次

Git是什么?

Git是目前世界上最先进的分布式版本控制系统,并且开源免费。
Git的工作原理参见https://git-scm.com

Git命令

1. git init 将文件夹纳入版本控制

在文件夹内会生成一个.git的隐藏文件

  $ mkdir gitRepository //创建文件夹
  $ cd gitRepository
  $ git init
  Initialized empty Git repository
2. git add 将文件加入版本控制
  $ vim index.html
  $ git add index.html  // 只添加具体的文件
  $ vim readme.md
  $ vim LICENSE
  $ git add .  // 将所有文件全部加入,添加大量文件时非常好用
3. git status 查看文件跟踪状态
  • 新添加的未提交的文件会被标记为new file:
  • 未提交到版本控制的文件会被标记为Untracked files:下
  • 已经修改过的文件会被标记为modified:
  $ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   LICENSE
    new file:   index.html
    new file:   readme.md

4. git commit -m <message> 提交文件到仓库

可以多次添加文件,然后一次性提交

$ git commit -m 'initializer project'
[master (root-commit) 29ed60c] initializer project
 3 files changed, 8 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 index.html
 create mode 100644 readme.md

$ git status
On branch master
nothing to commit, working tree clean
5. 修改查询与回退

修改的文件却没有提交时,可以用git diff命令查看修改
使用git chekout可以退回到以前提交的版本

$ vim index.html
   ...
$ git diff index.html
diff --git a/index.html b/index.html
index 45956f9..ac8eaeb 100644
--- a/index.html
+++ b/index.html
@@ -3,5 +3,6 @@
       </head>
       <body>
               <h1>this is h1</h1>
+               <h2>this is h2</h2>
       </body>
</html>
 $ git checkout -- index.html
 $ vim index.html
6. git tag -a <tag> -m<message>

git tag 查看标签
git tag '<tagname> <commit id>'
默认标签是打在最新提交的commit上的
方法是找到历史提交的commit id,然后打上就可以了:

$ git log --pretty=oneline --abbrev-commit
e939f6c (HEAD -> master) modified index.html
29ed60c initializer project
$ git tag 0.0.1 29ed60c
$ git log --pretty=oneline --abbrev-commit
e939f6c (HEAD -> master, tag: 0.01) modified index.html
29ed60c (tag: 0.0.1) initializer project
7. 版本回退

git reset --hard commit_id
假如要放弃对index.html的修改,或者回到以前的某个版本

  $ git reset --hard 29ed60c
  $ vim index.html

8. 添加远程仓库并推送本地版本到远程仓库

git remote add <远程仓库名> <仓库地址>
git push -u <远程仓库名> <分支名>

  $ git remote add origin git@github.com:burtworld/gitRepository.git
  $ git remote -v
origin  git@github.com:burtworld/gitRepository.git (fetch)
origin  git@github.com:burtworld/gitRepository.git (push)
  $ git push -u origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 363 bytes | 363.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/burtworld/gitRepository/pull/new/master
remote:
To github.com:burtworld/gitRepository.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

注意:

  • 添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库,一般不会去改它。
  • 当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告:说你没有ssh,生成一个rsa的公钥并添加到账号的列表中即可。
9.克隆远程仓库

git clone <远程仓库地址>

10.创建分支

master也是一个分支,只不过默认就使用这个分支。
git checkout -b <分支名> 创建分支并切换过去
git branch -v 查看所有分支

$ git checkout -b dev
Switched to a new branch 'dev'
$ git branch -v
* dev    29ed60c initializer project
  master 29ed60c initializer project
11.分支合并

git merge <分支名>

  $ vim index2.html
  $ git status
On branch dev
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    index2.html

nothing added to commit but untracked files present (use "git add" to track)
$ git add index2.html
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   index2.html
$ git commit -m 'add index2.html'
[dev 881ac8f] add index2.html
 1 file changed, 6 insertions(+)
 create mode 100644 index2.html
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git merge dev
Updating 29ed60c..881ac8f
Fast-forward
 index2.html | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 index2.html
$ ls
LICENSE     index.html  index2.html readme.md
$ git branch -d dev
Deleted branch dev (was 881ac8f).
12.冲突解决

即合并后将冲突文件修改后再重新提交即可
使用git status可以查看是哪个文件发生了冲突

$ git checkout -b dev2 // 创建分支
Switched to a new branch 'dev2'
$ vim index2.html  // 修改index2.html
$ git add .
$ git commit -m 'modified index2.html'
[dev2 5727b22] modified index2.html
 1 file changed, 1 insertion(+)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
$ vim index2.html
...// 修改index2.html
$ git add .
$ git commit -m 'modified index2.html on master'
[master aae6c14] modified index2.html on master
 1 file changed, 1 insertion(+)

$ git merge dev2 // 合并分支
Auto-merging index2.html
<html>
CONFLICT (content): Merge conflict in index2.html
Automatic merge failed; fix conflicts and then commit the result.

$ git status // 查看冲突文件
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   index2.html

no changes added to commit (use "git add" and/or "git commit -a")
$ vim index2.html //解决冲突
$ git add .
$ git commit -m 'conflict fixed'

$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

相关文章

  • Git book

    目录 git community book git入门 git笔记 git 用法 git安装使用手册 git命令大...

  • Git与Git flow使用笔记

    使用Git的一些真实的使用笔记,这不是理论,而是真实的使用过程中的一些笔记 基础Git与流程 git init 初...

  • Git

    Git使用笔记 下载(Window版):https://git-for-windows.github.io/ 安装...

  • Git常用命令

    本文作为平日使用git的笔记 查看文件状态: $ git status //查看文件状态 存储文件: $ git ...

  • Git入门及常用方法

    Git学习笔记 关于 Git 的背景知识 Git 是什么Git——分布式版本控制系统,Linus使用C编写 Git...

  • **git** 使用笔记

    git 使用笔记 git原理: 文件(blob)对象,树(tree)对象,提交(commit)对象 tree对象 ...

  • 2019-10-25

    git 使用笔记 git原理: 文件(blob)对象,树(tree)对象,提交(commit)对象 tree对象 ...

  • Git教程笔记

    笔记20170207:Git教程 初始化一个Git仓库,使用git init命令。 添加文件到Git仓库,分两步:...

  • 2019-07-12

    Git使用笔记 0、先将工作分支的内容提交 git add . git commit -m "说明文字" 1、切换...

  • Git学习笔记(廖雪峰)

    Git学习笔记(廖雪峰) 创建版本库 小结 初始化一个Git仓库,使用git init命令。 添加文件到Git仓库...

网友评论

      本文标题:Git使用笔记

      本文链接:https://www.haomeiwen.com/subject/yjkaqqtx.html