Git常用命令

来源:互联网 发布:淘宝仓库管理工资 编辑:程序博客网 时间:2024/04/29 13:28

首先解释几个Git常用的名词:

1,working tree:工作目录树,下文称为工作空间

2,index:索引,下文称为暂存区

在使用git前需要先生成秘钥

ssh-keygen -t rsa -C “mail”-t Specify type of key to create-C Provide new comment

一,配置git

显示配置:git config [file_option] --list配置修改:git config [file_option] <config_name> <config_value>file_option(配置优先级从高到低):空:%REPOSITORY_HOME%/<repository_name>/.git/config--global:%USER_HOME%/.gitconfigUSER_HOME:win:/c/<user_name>linux:/home/<user_name>--system:%GIT_HOME%/etc/gitconfig

常见的config_name如下:

user.nameuser.emailcolor.uimerge.tool(必须是git可识别的工具)

在windows系统中要注意换行符的问题,如果想在提交时自动把行结束符CRLF转换成LF、签出代码时把LF转换成CRLF,只需进行如下设置即可

git config --global core.autocrlf true

二,图形界面

图形界面:git gui提交记录(需移动至库路径下):gitk [options]options:默认:当前分支提交记录--all:所有分支提交记录

三,帮助信息

git help <command>git <command> --help

四,文件操作

查看工作空间状态:git status跟踪新文件 | 已跟踪文件改动提交至暂存区 | 将文件状态由有冲突改为已解决:git add <file_names>提交修改至版本库:提交暂存区内容:git commit -m <desc>提交工作空间已跟踪文件修改(无论修改是否提交至暂存区,都会被提交至版本库):git commit -m <desc> <-a|file_names>-a:所有已跟踪文件file_names:特定已跟踪文件取消跟踪文件:git rm --cached <file_names>暂存区的修改回退至工作区:git reset HEAD <file_names>取消文件修改:git checkout <file_names>文件没有add至暂存区,则文件恢复至版本库中的状态文件add至暂存区后又做了修改,恢复至add至暂存区时的状态文件重命名:1,git mv <old_file_name> <new_file_name>2,git commit -m <desc>文件删除:1,删除工作目录下该文件:rm <file_name>2,将该删除操作提交至暂存区:git rm <file_name>3,提交暂存区修改:git commit -m <desc>文件比较:比较工作空间和暂存区之间的区别:git diff [file_name]比较暂存区和版本库之间的区别:git diff --cached [file_name]比较工作空间和版本库之间的区别:git diff HEAD [file_name]

五,工作现场

保存工作现场:git stash查看所有保存的工作现场:git stash list恢复工作空间至最近一次保存的工作现场:git stash apply恢复工作空间至特定的工作现场:git stash apply <stash_id> 恢复工作空间至最近一次保存的工作现场,之后删除该工作现场:git stash pop删除特定的工作现场:git stash drop <stash_id>

六,分支(主分支有时被称为主干,也就是master)

查看所有本地分支:git branch查看所有本地分支跟踪的远程分支:git branch -r创建分支:git branch <new_branch_name> [father_branch_name]   创建+检出新建分支:git checkout -b <new_branch_name> [father_branch_name]  检出分支(内容检出至工作空间):git checkout <branch_name>修改分支名:git branch -m <old_branch_name> <new_barnch_name>分支合并:直接合并:将指定分支合并至当前分支:git merge <branch_name>压合合并:所有历史提交压合为一个提交:git merge --squash <branch_name>不会自动提交,需要手动提交拣选合并:拣选特定提交合并至当前分支:git cherry-pick [-n] <commit_id>-n:合并不会自动提交,拣选出所有提交后,手动提交即可 冲突处理:1,手动修改文件 | git mergertool(使用merge工具修改文件)可使用git config --global merge.tool <merge_tool_name>修改默认的merge工具,merge工具路径必须已经加入系统PATH并且是git可识别的工具2,git add <file_names>将文件状态由有冲突修改为已解决删除指定分支(不能删除当前分支):git branch -[d|D] <branch_name>-D:强制删除,可用来删除未merge的分支 

七,标签

查看所有标签:git tag创建标签(默认为当前分支):git tag <tag_name> [branch_name]根据commit-id为当前版本创建标签:git tag <tag_name> <commit_id>为当前版本创建有说明的标签:git tag -a <tag_name> -m <desc>显示标签详细信息:git show <tag_name>推送本地特定标签至远程库:git push origin <tag-name>推送本地所有标签至远程库:git push origin --tags删除本地库特定标签:git tag -d <tag_name>删除远程库特定标签:git tag -d <tag_name>git push origin :refs/tags/<tag_name>

八,版本库

创建本地库:git init克隆远程库:git clone <URL> [local_repository_name]查看所有远程库别名:git remote [-v]-v:显示的更加详细,会附带URL地址查看远程库详细信息:git remote show <repository_alias>创建远程库别名+建立关联关系:git remote add <repository_alias> <URL>删除远程库别名+删除关联关系:git remote rm <repository_alias>更新本地库关联的远程库 :git fetch [repository_alias] 默认为origin更新本地库关联的远程库,并将更新合并至本地库:git pull [repository_alias local_branch_name:remote_branch_name]repository_alias默认为当前分支关联的远程库别名,如果当前分支没有关联远程版本,则为originlocal_branch_name:remote_branch_name默认为current_branch_name:current_branch_name本地库提交至远程库(只提交已经commit的修改):git push [repository_alias local_branch_name:remote_branch_name]同git pull说明

 

参考资料:

廖雪峰官方网站Git教程:

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

Git官方网站《Pro Git》中文版:

http://www.git-scm.com/book/zh

0 0
原创粉丝点击