使用 git 托管代码

在线练习 :css

 

https://learngitbranching.js.org/html

 

一、 下载安装好 git 客户端git

二、 找一个家代码托管平台shell

我用 coding.net,注册个帐号,建一个空项目缓存

而后打开安装好的 git bash 客户端,使用 git clone 命令克隆下远程仓库的项目bash

 

而后cd 进入 本地仓库地址,把 你本身的 代码粘贴进去,手动命令都可markdown

接着,在本地仓库里 git add 那些新文件,而后 git commit -m "此次修改信息" 提交到本地仓库网络

 

最后,使用 git push 便可推送到远程仓库啦!curl

 

 有空再看看 source Treeide

修改远程仓库 git 地址

git remote set-url origin http://xxx////.git

```

解决一些问题

git 版本太低

sudo yum update nss curl  # nss为名称解析和认证服务 curl为网络请求库

``` 

 

配置 .gitignore 文件 忽略提交文件及文件夹

target/
*.iml
.idea/
readme.txt
startML.py
src/main/test/
.project
.settings

 

强制覆盖本地代码

git fetch --all

git reset --hard origin/master

git pull

git经常使用命令--持续更新

 

git经常使用命令:

  • git init //初始化本地git环境
  • git clone XXX//克隆一份代码到本地仓库
  • git pull //把远程库的代码更新到工做台
  • git pull --rebase origin master //强制把远程库的代码跟新到当前分支上面
  • git fetch //把远程库的代码更新到本地库
  • git add . //把本地的修改加到stage中
  • git commit -m 'comments here' //把stage中的修改提交到本地库
  • git push //把本地库的修改提交到远程库中
  • git branch -r/-a //查看远程分支/所有分支
  • git checkout master/branch //切换到某个分支
  • git checkout -b test //新建test分支
  • git checkout -d test //删除test分支
  • git merge master //假设当前在test分支上面,把master分支上的修改同步到test分支上
  • git merge tool //调用merge工具
  • git stash //把未完成的修改缓存到栈容器中
  • git stash list //查看全部的缓存
  • git stash pop //恢复本地分支到缓存状态
  • git blame someFile //查看某个文件的每一行的修改记录()谁在何时修改的)
  • git status //查看当前分支有哪些修改
  • git log //查看当前分支上面的日志信息
  • git diff //查看当前没有add的内容
  • git diff --cache //查看已经add可是没有commit的内容
  • git diff HEAD //上面两个内容的合并
  • git reset --hard HEAD //撤销本地修改
  • echo $HOME //查看git config的HOME路径
  • export $HOME=/c/gitconfig //配置git config的HOME路径

 

 

团队协做git操做流程:

  • 克隆一个全新的项目,完成新功能而且提交:
  1. git clone XXX //克隆代码库
  2. git checkout -b test //新建分支
  3. modify some files //完成修改
  4. git add . //把修改加入stage中
  5. git commit -m '' //提交修改到test分支
  6. review代码
  7. git checkout master //切换到master分支
  8. git pull //更新代码
  9. git checkout test //切换到test分支
  10. git meger master //把master分支的代码merge到test分支
  11. git push origin 分支名//把test分支的代码push到远程库
  • 目前正在test分支上面开发某个功能,可是没有完成。忽然一个紧急的bug须要处理
  1. git add .
  2. git stash
  3. git checkout bugFixBranch
  4. git pull --rebase origin master
  5. fix the bug
  6. git add .
  7. git commit -m ''
  8. git push
  9. git checkout test
  10. git stash pop
  11. continue new feature's development
  • git工做流

 

 

 

git clone url -b <branch>

 

git log --pretty=oneline

git reflog

git checkout -- readme.txt 

git reset --hard HEAD^

git reset --hard HEAD~100

git reset --hard f8c9beb

 

# 比较工做区与暂存区间 某个文件的区别

git diff HEAD -- readme.txt

 

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令git push -u origin master第一次推送master分支的全部内容;

 

 

git tag -a v2.0 -m "version 2.0 released" d7a70c9 

 

git log --author="Frank Li"

 

看谁提交最多

git shortlog -sn

 git shortlog -sn --since='10 weeks' --until='2 weeks'

 

小小的举动, 大大的温暖

git config --global alias.praise blame

 

git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"

这将显示咱们工做的最后10个(--count=10)分支,按照最后一次在使用的时间排序。它只显示咱们本地的分支(refs/heads/),同时显示的格式更友好 --format。

 

 

 

 

$ git shortlog -sn

80 Harry Roberts

34 Samantha Peters

3 Tom Smith

这个 shortlog 命令是 git log 的总结摘要,-n 将根据每一个做者的提交数而不是做者字母顺序对输出进行排序。

上面显示了项目生命周期的全部commit,可是若是您想查看在指定的时间内有多少人完成,可使用 --since和--untilflags:

$ git shortlog -sn --since='10 weeks' --until='2 weeks'

59 Harry Roberts

24 Samantha Peters

我把它设置别名 $ git stats.

表扬

Git有一个很是有用的 blame 功能,容许咱们查看某段特定的代码段是由哪些开发人员负责更改的:

# 找出这个button的CSS文件第5-10行是谁改的:

$ git blame -L5,10 _components.buttons.scss

blame这样的措辞不太好,这好像在说咱们要找的这个开发人员作错了事。但事实上可能不老是这样——他们可能作了一些特别聪明或使人印象深入的事情,咱们也想知道是谁干的。

从SVN那儿找到灵感,我把blame的别名设置成praise:

$ git config --global alias.praise blame

那么以前的命令就变成:

# 找出谁补全了提示信息,要给他们买咖啡:

$ git praise -L18,23 _includes/head.html

一个小小的改动,可是很温暖。

隐藏空白噪声

当您用diff或show查看一个有大量空白的对象时,咱们会看到不少视觉噪音,这可能使你很难看到更重要的内容。

去除这种噪声很容易,能够经过-w,它能够跟git diff和git show一块儿使用。例如,使用以前:

a {

color: $color-links;

-&:hover {

- color: $color-links-hover;

-}

+ &:hover {

+ color: $color-links-hover;

+ text-decoration: underline;

+ }

}

使用以后:

a {

color: $color-links;

&:hover {

color: $color-links-hover;

+ text-decoration: underline;

}

}

如今很容易看出,这里惟一有意义的变化是添加了 text-decoration: underline;,而其他的diff 内容是有点误导性的。

显示更改的单词而不是整行

当编辑文字时,与代码相反,查看更改的单词而不是整个发生改动的行一般更有用; 这在写markdown时特别有用。

幸运的是,咱们能够用 --word-diff 标志来显示发生更改的单词:

$ git diff --word-diff

下面是执行没有--word-diff标志的 diff 结果,比较乱,很难看到改了哪里:

-My friend Tom recently gave an excellent talk

+My good friend Tom gave an excellent talk

...可是加上 --word-diff 以后从新执行 diff,咱们获得了更加有用的信息:

My {+good+} friend Tom [-recently-] gave an excellent talk

查看你最近工做的分支

在不少项目中,开发者要在许多不一样的分支之间频繁切换的事情并不鲜见,这样的状况下要保持头脑清醒不太容易。咱们可让Git 帮忙解决这个问题:

$ git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"

这将显示咱们工做的最后10个(--count=10)分支,按照最后一次在使用的时间排序。它只显示咱们本地的分支(refs/heads/),同时显示的格式更友好 --format。

这一大段有点长,因此我设置了别名 $ git recent。

看看你们都在干什么

有时候,特别是对于team leader来讲,对全部分支上每一个人的工做状况有个通常了解是颇有用的。再一次,Git 很容易作到:

$ git log --all --oneline --no-merges

这将简略显示每一个人在全部分支上工做的日志(--no-merges)。

也能够经过如下方式来限制返回的提交数--since:

$ git log --all --since='2 weeks' --oneline --no-merges

对此我有个别名 $ git overview

本身以前的工做回顾

你回到一个旧的项目工做,或者在长时间的休息以后回到办公室,你已经忘了本身最后的工做是什么 。咱们能够要求 Git 简要介绍咱们以前在此项目上的工做:

$ git log --all --oneline --no-merges --author=<your email address>

我设了别名 $ git recap。

今天的工做内容

我不在这里讨论如何衡量开发人员的生产力,但我以为让客户知道我在每一天的工做内容很是有用。我不是保留我完成的任务的详细列表,而是让Git提供全部这些信息:

$ git log --since=00:00:00 --all --no-merges --oneline --author=<your email address>

这将log --all 全部分支上, --author在 --since 当天午夜开始的那一天(可是--no-merges)的全部工做,并作一个简单的--oneline概述。

别名 $ git today。

看看有哪些变更须要pull

若是你有段时间没在一个项目里工做了,从新回来的时候,在pull到本地以前你得看看以前都发生了什么事:

$ git log --oneline --no-merges HEAD..<remote>/<branch>

HEAD 是可选的。

例如,看看你度假时某个特定功能都发生了什么:

$ git checkout feature/fonts

$ git fetch

$ git log --oneline --no-merges ..origin/feature/fonts

别名$ git upstream。

检查你要push的内容

但愿你有常常commit和push的习惯,但若是因为某些缘由,你存了大量的本地commit 没有push,快速回顾一下它们是什么。

为此,咱们反转以前的命令:

$ git log --oneline --no-merges <remote>/<branch>..HEAD

例如:

$ git fetch

$ git log --oneline --no-merges origin/feature/fonts..HEAD

 

git config --global --list

 

清空不要提交的任何

git clean -xdf

 

将第六个文件提交修改到上一次一块儿

git add file6 git commit --amend --no-edit


Workspace:工做区

  Index / Stage:暂存区

  Repository:仓库区(或本地仓库)

  Remote:远程仓库

1、新建代码库

  # 在当前目录新建一个Git代码库

  $ git init

  # 新建一个目录,将其初始化为Git代码库

  $ git init[project-name]

  # 下载一个项目和它的整个代码历史

  $ git clone [url]

2、配置

Git的设置文件为.gitconfig,它能够在用户主目录下(全局配置),也能够在项目目

录下(项目配置)。

  # 显示当前的Git配置

  $ git config–list

  # 编辑Git配置文件

  $ git config -e[–global]

  # 设置提交代码时的用户信息

  $ git config[–global] user.name “[name]”

  $ git config[–global] user.email “[email address]”

3、增长/删除文件

  # 添加指定文件到暂存区

  $ git add [file1][file2] …

  # 添加指定目录到暂存区,包括子目录

  $ git add [dir]

  # 添加当前目录的全部文件到暂存区

  $ git add .

  # 添加每一个变化前,都会要求确认

  # 对于同一个文件的多处变化,能够实现分次提交

  $ git add -p

  # 删除工做区文件,而且将此次删除放入暂存区

  $ git rm [file1][file2] …

  # 中止追踪指定文件,但该文件会保留在工做区

  $ git rm –cached[file]

  # 更名文件,而且将这个更名放入暂存区

  $ git mv[file-original] [file-renamed]

 

4、代码提交

  # 提交暂存区到仓库区

  $ git commit -m[message]

  # 提交暂存区的指定文件到仓库区

  $ git commit[file1] [file2] … -m [message]

  # 提交工做区自上次commit以后的变化,直接到仓库区

  $ git commit -a

  # 提交时显示全部diff信息

  $ git commit -v

  # 使用一次新的commit,替代上一次提交

  # 若是代码没有任何新变化,则用来改写上一次commit的提交信息

  $ git commit–amend -m [message]

  # 重作上一次commit,并包括指定文件的新变化

  $ git commit–amend [file1] [file2] …

5、分支

  # 列出全部本地分支

  $ git branch

  # 列出全部远程分支

  $ git branch -r

  # 列出全部本地分支和远程分支

  $ git branch -a

  # 新建一个分支,但依然停留在当前分支

  $ git branch[branch-name]

  # 新建一个分支,并切换到该分支

  $ git checkout -b[branch]

  # 新建一个分支,指向指定commit

  $ git branch[branch] [commit]

  # 新建一个分支,与指定的远程分支创建追踪关系

  $ git branch–track [branch] [remote-branch]

  # 切换到指定分支,并更新工做区

  $ git checkout[branch-name]

  # 切换到上一个分支

  $ git checkout –

  # 创建追踪关系,在现有分支与指定的远程分支之间

  $ git branch–set-upstream [branch] [remote-branch]

  # 合并指定分支到当前分支

  $ git merge[branch]

  # 选择一个commit,合并进当前分支

  $ git cherry-pick[commit]

  # 删除分支

  $ git branch -d[branch-name]

  # 删除远程分支

  $ git push origin–delete [branch-name]

  $ git branch -dr[remote/branch]

6、标签

  # 列出全部tag

  $ git tag

  # 新建一个tag在当前commit

  $ git tag [tag]

  # 新建一个tag在指定commit

  $ git tag [tag][commit]

  # 删除本地tag

  $ git tag -d[tag]

  # 删除远程tag

  $ git push origin:refs/tags/[tagName]

  # 查看tag信息

  $ git show [tag]

  # 提交指定tag

  $ git push[remote] [tag]

  # 提交全部tag

  $ git push[remote] –tags

  # 新建一个分支,指向某个tag

  $ git checkout -b[branch] [tag]

7、查看信息

  # 显示有变动的文件

  $ git status

  # 显示当前分支的版本历史

  $ git log

  # 显示commit历史,以及每次commit发生变动的文件

  $ git log –stat

  # 搜索提交历史,根据关键词

  $ git log -S[keyword]

  # 显示某个commit以后的全部变更,每一个commit占据一行

  $ git log [tag]HEAD –pretty=format:%s

  # 显示某个commit以后的全部变更,其”提交说明”必须符合搜索条件

  $ git log [tag]HEAD –grep feature

  # 显示某个文件的版本历史,包括文件更名

  $ git log –follow[file]

  $ git whatchanged[file]

  # 显示指定文件相关的每一次diff

  $ git log -p[file]

  # 显示过去5次提交

  $ git log -5–pretty –oneline

  # 显示全部提交过的用户,按提交次数排序

  $ git shortlog-sn

  # 显示指定文件是什么人在什么时间修改过

  $ git blame[file]

  # 显示暂存区和工做区的差别

  $ git diff

  # 显示暂存区和上一个commit的差别

  $ git diff–cached [file]

  # 显示工做区与当前分支最新commit之间的差别

  $ git diff HEAD

  # 显示两次提交之间的差别

  $ git diff[first-branch]…[second-branch]

  # 显示今天你写了多少行代码

  $ git diff–shortstat “@{0 day ago}”

  # 显示某次提交的元数据和内容变化

  $ git show[commit]

  # 显示某次提交发生变化的文件

  $ git show–name-only [commit]

  # 显示某次提交时,某个文件的内容

  $ git show[commit]:[filename]

  # 显示当前分支的最近几回提交

  $ git reflog

8、远程同步

  # 下载远程仓库的全部变更

  $ git fetch[remote]

  # 显示全部远程仓库

  $ git remote -v

  # 显示某个远程仓库的信息

  $ git remote show[remote]

  # 增长一个新的远程仓库,并命名

  $ git remote add[shortname] [url]

  # 取回远程仓库的变化,并与本地分支合并

  $ git pull[remote] [branch]

  # 上传本地指定分支到远程仓库

  $ git push[remote] [branch]

  # 强行推送当前分支到远程仓库,即便有冲突

  $ git push[remote] –force

  # 推送全部分支到远程仓库

  $ git push[remote] –all

9、撤销

  # 恢复暂存区的指定文件到工做区

  $ git checkout[file]

  # 恢复某个commit的指定文件到暂存区和工做区

  $ git checkout[commit] [file]

  # 恢复暂存区的全部文件到工做区

  $ git checkout .

  # 重置暂存区的指定文件,与上一次commit保持一致,但工做区不变

  $ git reset[file]

  # 重置暂存区与工做区,与上一次commit保持一致

  $ git reset–hard

  # 重置当前分支的指针为指定commit,同时重置暂存区,但工做区不变

  $ git reset[commit]

  # 重置当前分支的HEAD为指定commit,同时重置暂存区和工做区,与指定commit一致

  $ git reset–hard [commit]

  # 重置当前HEAD为指定commit,但保持暂存区和工做区不变

  $ git reset–keep [commit]

  # 新建一个commit,用来撤销指定commit

  # 后者的全部变化都将被前者抵消,而且应用到当前分支

  $ git revert[commit]

  # 暂时将未提交的变化移除,稍后再移入

  $ git stash

  $ git stash pop

10、其余

  # 生成一个可供发布的压缩包

  $ git archive

 

仅仅克隆 拉取 dev 分支
git clone -b dev 代码仓库地址 (dev是分支名称)


目的

咱们想要获取到代码仓库中分支“a” 中的文件到本地,我了解到有三种方法。


 
代码仓库

方法一:直接获取

*首先新建个文件夹,右键打开Git Bash


 
Git Bash

*在Git Bash中直接输入指令:git clone -b dev 代码仓库地址 (dev是分支名称)


 
Clone

*查看文件夹中内容,能够看到已经拉取完毕
 
拉取成功

方法二

*打开Git Bash
*在Git Bash 中输入 git init 进行初始化
*与远程代码仓库创建链接:git remote add origin 代码仓库地址


 
创建链接

*将远程分支拉到本地:git fetch origin dev(dev即分支名)


 
拉取分支

*建立本地分支:git checkout -b LocalDev origin/dev (LocalDev 为本地分支名,dev为远程分支名)


 
建立分支

*根据分支的变化,感受这条指令多是建立并切换到该分支
*最后一步将远程分支拉取到本地:git pull origin dev(dev为远程分支名)


 
拉取成功

方法三

*打开Git Bash *输入 git clone 代码仓库地址 *进入文件夹中 命令:cd XXX(文件夹名) *继续输入指令 git submodule init *最后 git submodule update

做者:JayMeWangGL 连接:https://www.jianshu.com/p/856ce249ed78 来源:简书 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。