逍遥谷

记录日常的点点滴滴

0%

Git操作

基础操作

新建git仓库,创建新文件夹

1
git init

添加文件到git索引

1
2
git add <filename> ---单个文件添加
git add . ---全部文件添加

提交到本地仓库

1
git commit -m "备注"

提交到远端仓库

1
git push [origin <master>]

分支操作

创建一个分支,并切换到分支

1
git checkout -b <branchname>

切换回master

1
git checkout master

把新建的分支删除

1
git branch -d <branchname>

push分支到远端仓库(push到远端之前,该分支不被其他人所见)

1
git push origin <branchname>

更新与合并

更新本地仓库

1
git pull

合并分支

1
git merge <branchname>

合并后需要添加

1
git add <branchname>

版本比对(合并前使用)

1
git diff <source_branch> <target_branch>

获取提交ID

1
git log

创建标签

1
git tag <tagname> [commit_id]

回退到指定版本

1
git reset --hard <commit_id>

使用reset命令后log是得不到充分信息的,这时我们需要使用reflog,然后再reset

1
git reflog

彩色git输出

1
git config color.ui true

查看远程分支与本地分支

1
git branch -a

删除远程分支

1
git push origin --delete <branch_name>

使用rm误删了文件,可以通过两步恢复

1
2
git reset HEAD <file_name>
git checkout -- <file_name>

删除文件

1
2
git rm <file_name>				###同时删除工作目录与本地仓库的文件
git rm --cached <file_name> ###删除本地仓库文件,并不影响工作目录

改变远程地址

1
git remote set-url origin <git_origin_url>

根据服务器地址创建本地git与服务器地址关联

1
git remote add origin <git_origin_url>