Git入门

来源:互联网 发布:阿里云时间服务器 编辑:程序博客网 时间:2024/06/05 04:39

Git是目前世界上最先进的分布式版本控制系统(没有之一)。

注:

本例使用的托管平台 码云网址链接
windows 下的管理软件 git

初始化项目

// https://git.oschina.net/huanuo/do.git  创建在码云的项目$ git clone https://git.oschina.net/huanuo/do.gitCloning 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 -lcore.symlinks=falsecore.autocrlf=truecore.fscache=truecolor.diff=autocolor.status=autocolor.branch=autocolor.interactive=truehelp.format=htmlhttp.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crtdiff.astextplain.textconv=astextplainrebase.autosquash=truefilter.lfs.clean=git-lfs clean -- %ffilter.lfs.smudge=git-lfs smudge -- %ffilter.lfs.required=truecredential.helper=manageruser.emil=argus.deng@nokia,comuser.name=dengerhuanuser.email=argus.deng@nokia.comcore.repositoryformatversion=0core.filemode=falsecore.bare=falsecore.logallrefupdates=truecore.symlinks=falsecore.ignorecase=trueremote.origin.url=https://git.oschina.net/huanuo/doweb.gitremote.origin.fetch=+refs/heads/*:refs/remotes/origin/*branch.master.remote=originbranch.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 statusOn branch masterYour 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 logcommit ba461623c134d4734ec9804896a582fb2a63b9bdAuthor: Deng argus <argus.deng@nokia.com>Date:   Fri Apr 7 17:17:17 2017 +0800    add new filecommit e55ebbd9d4ea4b88134c66367dd0b52131f9b2d2Author: 夸夸夸夸 <2492698140@qq.com>Date:   Fri Apr 7 16:35:12 2017 +0800// 查看简略信息$ git log --pretty=onelineba461623c134d4734ec9804896a582fb2a63b9bd add new file  //每一条历史记录使用惟一的哈希码标注e55ebbd9d4ea4b88134c66367dd0b52131f9b2d2 Initial commit

切换历史纪录

git checkout <hash code> 查看历史记录

举例

fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do ((ba46162...))$ git checkout e55ePrevious HEAD position was ba46162... add new fileHEAD 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 newbranchfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git checkout newbranchSwitched to branch 'newbranch'fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (newbranch)$ git checkout -b anobranchSwitched to a new branch 'anobranch'fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)$

在新的分支上添加文件

fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)$ touch hellobranch.txtfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)$ lshellobranch.txt  newfile.txt  README.mdfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)$ vim hellobranch.txtfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (anobranch)$ git add hellobranch.txtwarning: 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..14641f3Fast-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 anobranchDeleted 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 remoteoriginfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git remote add oam https://git.oschina.net/huanuo/oam.gitfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git remoteoamoriginfan@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 oamnewnamefan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git remoteoamnewnameoriginfan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git remote rm oamnewnamefan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git remoteorigin

向远程仓库推送数据

$ git push origin master

举例

fan@DESKTOP-1OGO4V1 MINGW64 ~/Desktop/gti/do (master)$ git push origin masterCounting 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 pullAlready 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 newbranchTo https://git.oschina.net/huanuo/do.git - [deleted]         newbranch
0 0
原创粉丝点击