Git详细笔记

1 篇文章 0 订阅

1. 运行前配置

全局配置用户信息,所有提交都会用到

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

针对特定项目使用特定的用户名和邮箱式,在项目目录下运行

$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

查看所有配置

$ git config --list

查看所有配置以及他们所在的文件

$ git config --list --show-origin

查看版本

$ git --version

获取帮助

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

快速参考

$ git <verb> -h

进入~/.ssh目录查看是否已经生成了ssh密钥

$ cd ~/.ssh

如果不存在此目录,则使用以下命令会在~/.ssh目录生成id_rsa和id_rsa.pub两个文件

$ ssh-keygen -t rsa -C “xperblueray@xperblueray.com”

2. Git仓库

从现有仓库克隆

$ git clone https://github.com/libgit2/libgit2

从本地项目初始化仓库

$ cd /home/user/my_project
$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin https://github.com/xperblueray/C.git
$ git push -u origin master

检查当前文件状态

$ git status
$ git status -s

文件生命周期

Git 下文件生命周期图。

移除文件,不再纳入版本管理

$ rm PROJECTS.md
$ git rm PROJECTS.md

仅从Git仓库中删除(从暂存区移除),但仍然保留在当前工作目录中,Git不再继续追踪。适用于当忘记添加.gitignore文件时,不小心把日志或静态资源添加到暂存区。

$ git rm --cached README

移动文件

$ git mv file_from file_to

如上命令相当于

$ mv README.md README
$ git rm README.md
$ git add README

3. Git基础

查看提交历史

$ git log

提交了发现几个文件没有添加,或者提交信息写错了

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

取消暂存的文件

$ git reset HEAD CONTRIBUTING.md

撤消对文件的修改

git checkout -- <file>...

查看远程仓库

git remote -v

添加远程仓库

$ git remote add pb https://github.com/paulboone/ticgit
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)
pb	https://github.com/paulboone/ticgit (fetch)
pb	https://github.com/paulboone/ticgit (push)

获取paul的仓库中有但你没有的信息可以使用

$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
 * [new branch]      master     -> pb/master
 * [new branch]      ticgit     -> pb/ticgit

如果当前分支设置了跟踪了远程分支,可以使用git pull自动抓取后合并远程分支到当前分支

$ git pull

推送到远程仓库git push <remote> <branch>

$ git push origin master

查看某个远程仓库

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           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)

创建附注标签

$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4

创建轻量标签

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

共享标签

$ git push origin v1.5
$ git push origin --tags

4. Git分支

远程分支未显示在本地,需要进行更新

git remote update origin --prune

新建一个分支并同时切换到那个分支上

$ git checkout -b iss53
Switched to a new branch "iss53"
# 上述命令相当于
$ git branch iss53
$ git checkout iss53

提交后,将iss53合并回master

$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
 index.html | 2 ++
 1 file changed, 2 insertions(+)

删除分支

$ git branch -d iss53

查看分支

$ git branch
$ git branch -v
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值