Git是目前世界上最先进的分布式版本控制系统(没有之一)。
注:
本例使用的托管平台 码云网址链接
windows 下的管理软件 git
初始化项目
// https://git.oschina.net/huanuo/do.git 创建在码云的项目
$ git clone https://git.oschina.net/huanuo/do.git
Cloning into 'do'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
用户信息配置
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git config --global user.email "argus.deng@nokia.com"
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git config --global user.name "argus.deng"
查看配置信息
$ git config -l
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.required=true
credential.helper=manager
user.emil=argus.deng@nokia,com
user.name=dengerhuan
user.email=argus.deng@nokia.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=https://git.oschina.net/huanuo/doweb.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
基本操作
- git add <文件名> 将工作目录的文件添加到缓存区域。
- git add * 添加所有文件到staging区域。
- git commit 打开一个编辑器编辑说明,然后将staging区域的文件提交到仓库中。
- git commit -m “说明” 将staging区域的文件提交到仓库中,附带简单说明。
- git commit -a 将工作区域的文件直接提交,跳过stage操作
- git status 查看当前的文件状态。
举例
// 创建新的文件 newfile.txt 然后查看当前的额文件状态
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
$ git add newfile.txt // 将newfile.txt 添加到缓存区域
$ git commit -m "addnewfile" // 将缓存区域的文件 提交到仓库
[master 1210b9b] addnewfile
1 file changed, 1 insertion(+)
create mode 100644 newfile.txt
这样就完成了一个提交
查看文件的提交历史
git log 查看历史记录
git log -p 显示详细更改信息,
git log --pretty=oneline 显示简略信息
git log --pretty=oneline --graph 显示项目分支合并图。
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git log
commit ba461623c134d4734ec9804896a582fb2a63b9bd
Author: Deng argus <argus.deng@nokia.com>
Date: Fri Apr 7 17:17:17 2017 +0800
add new file
commit e55ebbd9d4ea4b88134c66367dd0b52131f9b2d2
Author: 夸夸夸夸 <2492698140@qq.com>
Date: Fri Apr 7 16:35:12 2017 +0800
// 查看简略信息
$ git log --pretty=oneline
ba461623c134d4734ec9804896a582fb2a63b9bd add new file //每一条历史记录使用惟一的哈希码标注
e55ebbd9d4ea4b88134c66367dd0b52131f9b2d2 Initial commit
切换历史纪录
git checkout <hash code> 查看历史记录
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do ((ba46162...))
$ git checkout e55e
Previous HEAD position was ba46162... add new file
HEAD is now at e55ebbd... Initial commit
分支管理
git 中的分支就是一个可移动指针,指向了某个历史记录。 默认的分支名是 master 。每次提交后, master 都会指向你刚才的提交记录。
git branch <new branch nama> 创建一个新分支
git checkout -b <new branch name> 创建一个新分支并切换到它
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git branch newbranch
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git checkout newbranch
Switched to branch 'newbranch'
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (newbranch)
$ git checkout -b anobranch
Switched to a new branch 'anobranch'
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$
在新的分支上添加文件
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ touch hellobranch.txt
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ ls
hellobranch.txt newfile.txt README.md
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ vim hellobranch.txt
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ git add hellobranch.txt
warning: LF will be replaced by CRLF in hellobranch.txt.
The file will have its original line endings in your working directory.
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ git commit -m "add hello branch"
[anobranch 14641f3] add hello branch
1 file changed, 1 insertion(+)
create mode 100644 hellobranch.txt
合并分支
git merge hotfix
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)
$ 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)
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git merge anobranch // 合并分支
Updating ba46162..14641f3
Fast-forward
hellobranch.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 hellobranch.txt
删除分支
git branch -d branchname
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git branch -d anobranch
Deleted branch anobranch (was 14641f3).
远程仓库操作
git remote add <shortname> <url> 添加新远程仓库。
git remote show [remote-name] 查看某个远程仓库信息
git remote rename [old-name] [new-name] 重命名远程仓库
git remote rm [remote-name] 删除远程仓库
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote
origin
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote add oam https://git.oschina.net/huanuo/oam.git
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote
oam
origin
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote show oam
* remote oam
Fetch URL: https://git.oschina.net/huanuo/oam.git
Push URL: https://git.oschina.net/huanuo/oam.git
HEAD branch: master
Remote branch:
master new (next fetch will store in remotes/oam)
Local ref configured for 'git push':
master pushes to master (local out of date)
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote rename oam oamnewname
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote
oamnewname
origin
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote rm oamnewname
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git remote
origin
向远程仓库推送数据
$ git push origin master
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 520 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://git.oschina.net/huanuo/do.git
e55ebbd..14641f3 master -> master
从远端获取数据
git fetch
git pull
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git pull
Already up-to-date.
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)
$ git fetch
删除远程分支
git push remotename --delete branchname
举例
fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/test/do (master)
$ git push origin --delete newbranch
To https://git.oschina.net/huanuo/do.git
- [deleted] newbranch