git 分支管理记录

测试环境 :虚拟机(VMware Fusion Centos 6.5) 

1.安装git环境

[root@localhost ~]# yum -y install git

2.检测git是否安装成功

[root@localhost ~]# git -v
Unknown option: -v
usage: git [--version] [--help] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

 

 

本地常用操作

 


 

- 创建本地仓库 [git init]

[root@localhost test]# git init 
初始化空的 Git 版本库于 /var/tmp/test/.git/
Initialized empty Git repository in /var/tmp/text/.git/
[root@localhost test]#

- 添加文件到待提交仓库 [git add]

[root@localhost test]# echo "木雨流苏" >> README.md //把 "木雨流苏" 追加到 README.md 文件
[root@localhost test]# git add README.md  // git add . (表示把当前所有文件都提交)  git -f README.md (强制提交 README.md)文件
[root@localhost test]# 

- 查看当前仓库待提交的文件 [git status]

[root@localhost test]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#    README.md
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

- 提交待提交文件到仓库 [git commit]

[root@localhost test]# git commit -m "First Commit"  // 提交代码推荐携带 -m "此处填写提交的备注" [--amend 重新提交]
// 第一次提交的提示 设置了就好了
[master(根提交) 79114a3] First Commit
 Committer: root <root@localhost.localdomain>
您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确
与否。您可以通过下面的命令对其进行明确地设置以免再出现本提示信息:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:

    git commit --amend --reset-author 

 1 file changed, 1 insertion(+)
 create mode 100644 README.md

- 创建分支 [git branch]

[root@localhost test]# git branch one // git branch -d one 删除 one 分支 
[root@localhost test]# git branch // 查看仓库分支列表
* master
  one

- 切换分支 [git checkout]

[root@localhost test]# git checkout one // git chectout -b one 创建并切换到新创建的分支
切换到分支 'one'
// 当切换到不存在的分支后
error: pathspec 'a' did not match any file(s) known to git.

- 合并某个分支到当前分支 [git merge]

[root@localhost test]# git merge one
更新 79114a3..e988dbc
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

- 查看日志记录 [git log]

[root@localhost test]# git log
commit e988dbc451a1a9e546299be9d05fa2c8e8fd3283 // 提交ID
Author: root <root@localhost.localdomain> // 提交 作者
Date:   Sun Apr 16 22:05:20 2017 +0800 // 提交时间

    One Branch Commit // 提交备注

commit 79114a34ce2f512da42af3c23078ce78a6d29d9d
Author: root <root@localhost.localdomain>
Date:   Sun Apr 16 21:38:30 2017 +0800

    First Commit

- 删除文件 [git rm]

[root@localhost test]# git rm 1.txt  // 删除并提交到待提交库中等待commit (此命令与 rm -rf 1.txt ; git add . 等同)
rm '1.txt' // 提交之后的返回
[root@localhost test]# git rm 1.txt  // 此时是上一步删除之后重新创建的 1.txt 
fatal: 路径 '1.txt' 未匹配任何文件 // 这次由于git库中没有此文件记录所以提示错误
[注]
// git rm --cached 1.txt [删除git对此文件的追踪,并不删除该文件] 
// git rm -r --cached . [删除git对当前目录下所有文件的追踪]

 

 

  远程常用命令


 

- 创建并关联远程仓库(此处我已经创建好了一个远程仓库)

echo "# git_test" >> README.md // 创建一个README.md文件 并写入 "#git_test"
git init // 创建本地仓库
git add README.md // 提交README.md 到本地待提交仓库
git commit -m "first commit" // 提交add的文件到本地仓库
git remote add origin git@github.com:zhouwei8725/git_test.git // 添加远程主机(此处以github举例)
git push -u origin master // 提交到远程仓库  

 

- 提交到远程仓库 [git push

git push origin master:master // git push 远程仓库名 本地分支名:远程分支名 
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 264 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)

 

- 拉取远程仓库数据[git pull]

 

- 克隆远程仓库到本地分支[git clone]

-


 

一些其他小情况 ,解决办法

1 . 当出现[warning: LF will be replaced by CRLF in XXXXXXXXXXXXXX.] 错误时候 可以使用一下代码解决错误

git config core.autocrlf false

 

2 . 生成公钥私钥 [ssh-keygen -t rsa -C "your_email@example.com" ]

[root@localhost test]# ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair. 
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.  // 此处是密钥文件位置 有这(id_rsa  id_rsa.pub)两个位置
// 创建密钥的密码 [如果连续敲两次 return/enter/回车 为空密码] Enter passphrase (empty
for no passphrase): //此处输入密码 Enter same passphrase again: // 此处重复密码 Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: cb:eb:9c:5c:5f:d6:bd:40:b9:f5:aa:32:eb:29:2f:cb your_email@example.com The key's randomart image is: +--[ RSA 2048]----+ | | | | | | | . | | S o . | | . . . o.o| | o . oo +| | +o+oo o...| | .E*=+o... | +-----------------+

 3. 定义默认的用户名/邮箱

git config --global user.email "you@example.com" // 设置默认的邮箱
git config --global user.name "Your Name" // 设置默认的用户名

  图形化软件推荐


  

    一款免费好用的 windows and mac 的 git 图形化客户端 地址:https://www.sourcetreeapp.com/

posted @ 2017-04-16 22:09  奔跑的砖头  阅读(1412)  评论(0编辑  收藏  举报