Git_Tutorial
配置 SSH
生成 SSH 密钥
ssh-keygen -t rsa -C "email@example.com"
GitHub 配置
cat ~/.ssh/id_rsa.pub
复制这个文件中的内容,然后登录 GitHub ,配置即可。
配置用户名和邮箱
git config --global user.name "username"
git config --global user.email "email@example.com"
一台机器上配置多个 GitHub 账户参考:如何用 SSH 密钥在一台机器上管理多个 GitHub 账户
clone
只Clone指定Commit的代码
以下图所示为例:
这个仓库在原先的历史记录在根目录下有一个 assets
文件夹,但是在最新代码仓库中已经不存在了,这时候如果你只想使用那个 commit
记录的代码,那就需要如下操作:
# make a new folder
cd C:\Users\Admin\WebstormProjects
make new folder
# make a new blank repository in the current directory
git init
# add a remote
git remote add origin git@github.com:hello-github-ui/tampermokey-script-dev.git
# fetch a commit (or branch or tag) of interest
# Note: the full history up to this commit will be retrieved unless
# you limit it with '--depth=...' or '--shallow-since=...'
# git fetch origin <sha1-of-commit-of-interest>
git fetch origin e9f7b04d18cb926a1ac0147b62039d6840d94510
# reset this repository's master branch to the commit of interest
git reset --hard FETCH_HEAD
最终可以看到此时本地代码仓库是已经变成你需要的目录了:
参考来源:stackoverflow
只Clone指定分支的代码
有两种方式可以clone一个特定的分支。
- 直接clone仓库,这会获取到所有分支,然后立即切换到指定的分支。
- 只clone目标分支,其它的分支代码不获取。
==第一种方式就不演示了,默认都会。==
直接演示方式二
这个仓库有三个分支,我们现在只想获取 express
分支:
# 同样进入这个新建的空目录
cd C:\Users\Admin\WebstormProjects\new folder
# 只克隆express分支的代码,注意这种方式是不会获取到远程的其它分支的
git clone -b express --single-branch git@github.com:hello-github-ui/NodeJS_Tutorial.git
这里
-b
这是--branch
的别名注意:
--single-branch
是 Git 在版本1.7.10
及更高版本中引入的,因此使用时请注意你的 Git 版本。
此时,我们看一下本地仓库的分支情况:
只clone指定tag的代码
需求:还是使用上面演示的那个仓库,现在我们只需要克隆
v1.0.0
这个tag的代码,并且不需要历史记录
。
准备一个空目录:
# git clone --depth 1 --branch <tag_name> <repo_url>
git clone --depth 1 --branch v1.0.0 git@github.com:hello-github-ui/NodeJS_Tutorial.git
命令解释:
--branch
can also take tags and detaches the HEAD at that commit in the resulting repository.
--depth 1
is optional but if you only need the state at that one revision, you probably want to skip downloading all the history up to that revision.
参考来源:stackoverflow
.gitignore
只忽略某个具体目录下的文件
如果你想只忽略一个特定的文件,你需要提供 该文件在项目根目录下的完整路径。
例如,如果你想忽略位于根目录下的 text.txt
文件,你可以做如下操作:
/text.txt
而如果你想忽略一个位于根目录下的 test
目录中的 text.txt
文件,你需要如下做:
/test/text.txt
当然你也可以这样写上述内容:
test/text.txt