学习目标:

**利用Git命令实现简单的版本控制,设好存档点,保护好自己写的代码不至于丢失,避免重复写代码的尴尬,并跟踪自己的所有修改变化。 **


主干内容:

一、设置git config –global 参数

有了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,一个电脑只需配置一次,当然你也可以对某个仓库指定的不同的用户名和邮箱,需要另外配置,就不多说了。

1
2
git config  –-global user.name "用户名"
git config –-global user.email "用户邮箱"

二、初始化一个仓库 git init

什么是仓库?仓库又名版本库,英文名repository,你可以简单的理解成一个文件夹,这个文件夹里面的所有文件都可以被Git管理起来,每个文件的修改,删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻还可以将文件”还原”。

选中你的工程文件夹 –> 右键 –> Git Bash Here ,进入Git 命令窗口,输入:

1
2
git init 
// pwd //使用该命令可以查看初始化后的仓库绝对路径

这样,就创建好了仓库,并默认在(master)主分支上。

三、把文件添加到仓库中 git add

1
2
git add test1.txt   // 可以单个添加
git add . // 也可以一次添加所有文件

四、提交到本地仓库 git commit

1
git commit -m "提交信息说明"

五、新建分支并切换到新建分支 git checkout -b

可以将主分支保护起来,所有修改都在分支上进行,当调试ok后,再合并分支。

1
2
3
4
git checkout -b 新的分支名  // 新建分支并切换到新建分支

git checkout 分支名 // 切换到指定分支
git branch // 查看分支 * 后面的分支是当前分支

六、合并分支 git merge branch

1
git merge branch 主分支名 分支命  // 合并两个任意分支

七、删除本地分支 git branch -d

合并分支后并不会删除原分支,需要手动删除。

1
git branch -d 分支   // 删除本地分支

八、连接远程仓库 git remote add origin

远程仓库的作用在于共享代码,协同编程,如果只是版本控制也可不用上传。(主要是本人学艺不精,远程推送第一次还正常,第二次推送总是出现各种冲突)

1
git remote add origin git@github.com:用户名/远程仓库名.git   // 使用HHS 加密传输

九、推送主分支 git push -u origin master

1
git push -u origin master   // 推送主分支 第一次需要加 -u 后面就不用了

十、推送任意分支 git push origin

1
git push  origin 分支名  // 推送任意分支

十一、删除任意远程分支 git push origin :

1
git push  origin :分支名  // 删除任意远程分支 (本地分支还在) 注意区别是多了个冒号

十二、拉取远程分支到本地创建的新分支 git checkout -b origin/

1
git checkout -b 本地新建分支名  origin/远程分支名   // 拉取远程分支到本地创建的新分支

十三、查看本地与远程所有分支 git branch -a

1
git branch -a  // 查看本地与远程所有分支 

辅助内容

一、获取常用git命令及说明 git 或 git help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$ git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
<command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
restore Restore working tree files
rm Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status

grow, mark and tweak your common history
branch List, create, or delete branches
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
tag Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

二、显示所有git命令 git help -a

1
git help -a  // 由于内容太多就不展示了

三、查看单个命令的说明 git help ***

1
git help branch  // 查看branch 的手册信息

四、查看文件的区别 git diff

1
2
3
git diff  // 查看当前仓库所有文件与上一次存档的区别,只能区别文本信息

git diff 文件1 文件2 // 查看两个文件的差异,必须在当前目录下

五、查看日志(所有提交信息)

1
git log --oneline  // 显示简单的提交日志

说明:本文是收集参考文档,以方便查看


信息链接:

  1. 利用Git命令进行版本控制之常见命令汇总

=================我是分割线=================

欢迎到公众号来唠嗑: