LINUX学习之GIT

来源:互联网 发布:软件应届生求职怎么样 编辑:程序博客网 时间:2024/06/13 04:27

创建新仓库

创建新文件夹,打开,然后执行 git init以创建新的 git 仓库。

检出仓库

执行如下命令以创建一个本地仓库的克隆版本:git clone /path/to/repository 如果是远端服务器上的仓库,你的命令会是这个样子:git clone username@host:/path/to/repository

工作流

你的本地仓库由 git 维护的三棵“树”组成。第一个是你的 工作目录,它持有实际文件;第二个是 缓存区(Index),它像个缓存区域,临时保存你的改动;最后是 HEAD,指向你最近一次提交后的结果。

添加与提交

你可以计划改动(把它们添加到缓存区),使用如下命令:git add <filename>git add *这是 git 基本工作流程的第一步;使用如下命令以实际提交改动:git commit -m "代码提交信息"现在,你的改动已经提交到了 HEAD,但是还没到你的远端仓库。

推送改动

你的改动现在已经在本地仓库的 HEAD 中了。执行如下命令以将这些改动提交到远端仓库:git push origin master可以把 master 换成你想要推送的任何分支。 如果你还没有克隆现有仓库,并欲将你的仓库连接到某个远程服务器,你可以使用如下命令添加:git remote add origin <server>如此你就能够将你的改动推送到所添加的服务器上去了。

分支

分支是用来将特性开发绝缘开来的。在你创建仓库的时候,master 是“默认的”。在其他分支上进行开发,完成后再将它们合并到主分支上。创建一个叫做“feature_x”的分支,并切换过去:git checkout -b feature_x切换回主分支:git checkout master再把新建的分支删掉:git branch -d feature_x除非你将分支推送到远端仓库,不然该分支就是 不为他人所见的:git push origin <branch>

更新与合并

要更新你的本地仓库至最新改动,执行:git pull以在你的工作目录中 获取(fetch) 并 合并(merge) 远端的改动。要合并其他分支到你的当前分支(例如 master),执行:git merge <branch>两种情况下,git 都会尝试去自动合并改动。不幸的是,自动合并并非次次都能成功,并可能导致 冲突(conflicts)。 这时候就需要你修改这些文件来人肉合并这些 冲突(conflicts) 了。改完之后,你需要执行如下命令以将它们标记为合并成功:git add <filename>在合并改动之前,也可以使用如下命令查看:git diff <source_branch> <target_branch>

标签

在软件发布时创建标签,是被推荐的。这是个旧有概念,在 SVN 中也有。可以执行如下命令以创建一个叫做 1.0.0 的标签:git tag 1.0.0 1b2e1d63ff1b2e1d63ff 是你想要标记的提交 ID 的前 10 位字符。使用如下命令获取提交 ID:git log你也可以用该提交 ID 的少一些的前几位,只要它是唯一的。

替换本地改动

假如你做错事(自然,这是不可能的),你可以使用如下命令替换掉本地改动:git checkout -- <filename>此命令会使用 HEAD 中的最新内容替换掉你的工作目录中的文件。已添加到缓存区的改动,以及新文件,都不受影响。假如你想要丢弃你所有的本地改动与提交,可以到服务器上获取最新的版本并将你本地主分支指向到它:git fetch origingit reset --hard origin/master

有用的贴士

内建的图形化 git:gitk彩色的 git 输出:git config color.ui true显示历史记录时,只显示一行注释信息:git config format.pretty oneline交互地添加文件至缓存区:git add -i
git fetch:取回远程服务器的更新; git pull:取回远程服务器的更新,而后与本地的指定分支合并;            git pull <远程主机名> <远程分支名><本地分支名> git push:将本地的更新推送到远程主机;            git push <远程主机名> <本地分支名><远程分支名>            git push seaweedfs :master (清空远程分支)
#安装GIT[root@localhost ~]# yum install -y git#创建GIT仓库方法一:克隆[root@localhost ~]# git clone https://github.com/chrislusf/seaweedfs.git#本地克隆[root@bogon ~]# git clone test /tmp/testCloning into '/tmp/test'...warning: You appear to have cloned an empty repository.done.#显示当前仓库的远程版本库[root@bogon ~]# git clone https://github.com/chrislusf/seaweedfs.git[root@bogon seaweedfs]# git remoteorigin[root@bogon seaweedfs]# git remote -vorigin  https://github.com/chrislusf/seaweedfs.git (fetch)origin  https://github.com/chrislusf/seaweedfs.git (push)[root@bogon ~]# git clone -o seaweedfs https://github.com/chrislusf/seaweedfs.git[root@bogon ~]# cd seaweedfs/[root@bogon seaweedfs]# git remoteseaweedfs[root@bogon seaweedfs]# git remote -vseaweedfs   https://github.com/chrislusf/seaweedfs.git (fetch)seaweedfs   https://github.com/chrislusf/seaweedfs.git (push)[root@bogon seaweedfs]# [root@bogon seaweedfs]# git remote show seaweedfs * remote seaweedfs  Fetch URL: https://github.com/chrislusf/seaweedfs.git  Push  URL: https://github.com/chrislusf/seaweedfs.git  HEAD branch: master  Remote branch:    master tracked  Local branch configured for 'git pull':    master merges with remote master  Local ref configured for 'git push':    master pushes to master (up to date)[root@bogon seaweedfs]# 

#关联远程库

[root@bogon seaweedfs]# git remote add myserver http://192.168.1.15 /git/seaweedfs.git[root@bogon seaweedfs]# git remotemyserverseaweedfs

#删除

[root@bogon seaweedfs]# git remote rm myserver[root@bogon seaweedfs]# git remoteseaweedfs

#改名

[root@bogon seaweedfs]# git remote rename seaweedfs newname[root@bogon seaweedfs]# git remotenewname[root@bogon seaweedfs]# 

#创建GIT仓库方法二:创建文件夹 并初始化

[root@localhost ~]# mkdir test[root@localhost ~]# cd test[root@localhost test]# git initInitialized empty Git repository in /root/test/.git/[root@localhost test]# ls -a.  ..  .git
#GIT配置段[root@localhost test]# git config -lcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=true#本地配置[root@localhost test]# git config --add user.name xuxue[root@localhost test]# git config -lcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=trueuser.name=xuxue[root@localhost test]# cat .git/config[core]    repositoryformatversion = 0    filemode = true    bare = false    logallrefupdates = true[user]    name = xuxue#全局配置[root@localhost test]# git config --global --add user.email 'xuxue@163.com'[root@localhost test]# cat ~/.gitconfig[user]    email = xuxue@163.com
使用GIT[root@localhost test]# cp /usr/share/doc/git-1.8.3.1/technical/api-quote.txt ./[root@localhost test]# lsapi-quote.txt[root@localhost test]# ls .git/objects/info  pack[root@localhost test]# git add api-quote.txt [root@localhost test]# ls .git/objects/e8  info  pack[root@localhost test]# ls .git/objects/e8/a1bce94e05f06c5b2aa51d2b610fb9da15d0bb#查看文件   -p 美观排版[root@localhost test]# git cat-file -p e8a1bce94e05f06c5b2aa51d2b610fb9da15d0bbquote API=========Talk about <quote.h>, things like* sq_quote and unquote* c_style quote and unquote* quoting for foreign languages(JC)

#计算文件哈希码

[root@localhost test]# git hash-object api-quote.txt e8a1bce94e05f06c5b2aa51d2b610fb9da15d0bb

[root@localhost test]# ls .git/objects/7f/3124cd1893f8c8c0a84a8afac6f849a03ae7c6
.git/objects/7f/3124cd1893f8c8c0a84a8afac6f849a03ae7c6
[root@localhost test]# git cat-file -p 7f3124cd1893f8c8c0a84a8afac6f849a03ae7c6
100644 blob e8a1bce94e05f06c5b2aa51d2b610fb9da15d0bb api-quote.txt
[root@localhost test]# ls .git/objects/ae/d6bdd31d6b94e081f0cf6f4051a6e1a3e50c28
.git/objects/ae/d6bdd31d6b94e081f0cf6f4051a6e1a3e50c28
[root@localhost test]# git cat-file -p aed6bdd31d6b94e081f0cf6f4051a6e1a3e50c28
tree 7f3124cd1893f8c8c0a84a8afac6f849a03ae7c6
author xuxue xuxue@163.com 1499360552 +0800
committer xuxue xuxue@163.com 1499360552 +0800

0.0.1

#创建文件[root@localhost test]# mkdir -pv hi/{01,02/001}#添加目录或文件至索引库中[root@localhost test]# git add .

#显示提交日志信息

[root@localhost test]# git logcommit 8012690de01c1bf798c0bbc046500b765e7192bbAuthor: xuxue <xuxue@163.com>Date:   Fri Jul 7 01:20:38 2017 +0800    0.0.2commit aed6bdd31d6b94e081f0cf6f4051a6e1a3e50c28Author: xuxue <xuxue@163.com>Date:   Fri Jul 7 01:02:32 2017 +0800    0.0.1
#显示索引中的文件信息:权限 对象名 暂存号及原始文件名    git ls-files -s例:[root@localhost test]# git ls-files -s100644 e8a1bce94e05f06c5b2aa51d2b610fb9da15d0bb 0   api-quote.txt

#显示未被追踪的文件 git ls-files -o

例:[root@bogon mytest]# git ls-files -oissue

#git mv OFILE NFILE 改变工作目录中的文件名,及索映射

例:[root@bogon mytest]# git ls-files -s100644 f64bd46668cdb52266e7b72910989d777a257a16 0   fstab[root@bogon mytest]# git mv fstab newfstab[root@bogon mytest]# git ls-files -s100644 f64bd46668cdb52266e7b72910989d777a257a16 0   newfstab

#仅删除索引中的映射 git rm –chched FILE

例:[root@bogon test]# git rm --cached issuerm 'issue'
#删除工作目录中的文件和索引中的映射 git rm FILE 

#显示分支

[root@bogon mytest]# git branch --list* master

#添加分支 *表示当前分支

[root@bogon mytest]# git branch develop[root@bogon mytest]# git branch --list  develop* master

#查看分支及当前的提交

[root@bogon mytest]# git show-branch! [develop] 0.0.1 * [master] 0.0.1--+* [develop] 0.0.1

#切换分支

[root@bogon mytest]# git checkout developSwitched to branch 'develop'[root@bogon mytest]# git branch --list* develop  master

#合并分支

[root@bogon mytest]# git merge developUpdating 09bec56..79ac8c3Fast-forward issue  |  3 +++ shadow | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 issue create mode 100644 shadow#查看仓库状态[root@bogon mytest]# git status#查看分支差异 比较提交,索引及工作目录[root@bogon mytest]# git diff

无冲突合并:
git check master
git status
git merge BRANCH_NAME
git log
有冲突合并分支:
手动解决冲突
解决完成后:
git add FILE
git commit -m ”

#比较索引和提交差异git diff --cached#比较索引和工作目录差异git diff#比较工作目录和提交差异git diff HEAD#比较两个提交git diff commit1_id commit2_id
```#git reset --soft ID 分支最近一次需重新提交[root@bogon data]# git reset --soft a4bbc[root@bogon data]# lsfstab  issue  shadow[root@bogon data]# git status# On branch master# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##   new file:   fstab#[root@bogon data]# git logcommit a4bbca3b9c0850ad42db193e5d1bc47916e4f6b1Author: root <xuxue@163.com>Date:   Fri Jul 7 23:58:59 2017 +0800    v2commit 50049b9b6683673992f0162839add6aa940e08cfAuthor: root <xuxue@163.com>Date:   Fri Jul 7 23:55:22 2017 +0800    v1[root@bogon data]# git commit -m 'v3-1'[master 8cbab61] v3-1 1 file changed, 12 insertions(+) create mode 100644 fstab[root@bogon data]# git logcommit 8cbab615ea341a5f24518b8ac99d1949576f0dd3Author: root <xuxue@163.com>Date:   Sat Jul 8 00:01:53 2017 +0800    v3-1commit a4bbca3b9c0850ad42db193e5d1bc47916e4f6b1Author: root <xuxue@163.com>Date:   Fri Jul 7 23:58:59 2017 +0800    v2commit 50049b9b6683673992f0162839add6aa940e08cfAuthor: root <xuxue@163.com>Date:   Fri Jul 7 23:55:22 2017 +0800    v1[root@bogon data]# git status# On branch masternothing to commit, working directory clean# git reset --mixed ID 分支最近一次需重新添加至暂存区并且重新提交 [root@bogon data]# git reset --mixed a4bbc[root@bogon data]# git status# On branch master# Untracked files:#   (use "git add <file>..." to include in what will be committed)##   fstabnothing added to commit but untracked files present (use "git add" to track)[root@bogon data]# git add fstab[root@bogon data]# git status# On branch master# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##   new file:   fstab#[root@bogon data]# git commit -m 'v3-2'[master 96a110f] v3-2 1 file changed, 12 insertions(+) create mode 100644 fstab[root@bogon data]# git logcommit 96a110f168897e9dd9c0c2d891035fdf46f2351cAuthor: root <xuxue@163.com>Date:   Sat Jul 8 00:03:11 2017 +0800    v3-2commit a4bbca3b9c0850ad42db193e5d1bc47916e4f6b1Author: root <xuxue@163.com>Date:   Fri Jul 7 23:58:59 2017 +0800    v2commit 50049b9b6683673992f0162839add6aa940e08cfAuthor: root <xuxue@163.com>Date:   Fri Jul 7 23:55:22 2017 +0800    v1#git reset --mixed ID 分支最近一次需重新建立文件,添加至暂存区并提交[root@bogon data]# git reset --hard a4bbc HEAD is now at a4bbca3 v2[root@bogon data]# lsissue  shadow[root@bogon data]# 

合并分支并切换

[root@bogon seaweedfs]# git branch --list* master[root@bogon seaweedfs]# git branch -r  newname/HEAD -> newname/master  newname/master[root@bogon seaweedfs]# git checkout -b newbranch newname/masterBranch newbranch set up to track remote branch master from newname.Switched to a new branch 'newbranch'[root@bogon seaweedfs]# git branch --list  master* newbranch[root@bogon seaweedfs]# 
原创粉丝点击