git基础
建仓
git init //去本地项目目录中建仓
git clone <url> //复制远程的项目到本地目录中
文件状态
上图把项目文件状态分为:未跟踪和已跟踪;已跟踪的文件有三种情况:未修改、已修改、放入暂存区
查看当前文件状态:
git status
跟踪文件(未跟踪–>已跟踪)
放入暂存区(修改–>暂存区):
git add <filename>
取消暂存(修改<–暂存区):
git reset <filename>
忽略文件:
创建一个.gitignore文件,用通配符去设置需要忽略的文件。详见https://github.com/github/gitignore
查看未暂存的文件对已暂存文件的修改:
git diff
提交跟新:
git commit -a(把跟踪文件全提交,不论是否暂存)
移除文件(移除版本管理):
git rm <filename>
移动文件(改名)
git mv <file_from> <file_to>
等同于
mv <file_from> <file_to>
git rm <file_from>
git add <file_to>
查看提交历史:
git log
选项 说明
-p 按补丁格式显示每个提交引入的差异。
–stat 显示每次提交的文件修改统计信息。
–shortstat 只显示 --stat 中最后的行数修改添加移除统计。
–name-only 仅在提交信息后显示已修改的文件清单。
–name-status 显示新增、修改、删除的文件清单。
–abbrev-commit 仅显示 SHA-1 校验和所有 40 个字符中的前几个字符。
–relative-date 使用较短的相对时间而不是完整格式显示日期(比如“2 weeks ago”)。
–graph 在日志旁以 ASCII 图形显示分支与合并历史。
–pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline、short、full、fuller 和
format(用来定义自己的格式)。
–oneline --pretty=oneline --abbrev-commit 合用的简写。
撤销:
覆盖上一次提交:
git commit --amend
取消暂存的文件(add)
git reset HEAD <filename>
撤销对文件修改(modified)
git checkout --<filename>
远程仓库
查看
git remote
查看具体的仓库
git remote show <remote(远程仓库名)>
添加
git remote add <shortname> <url>
从远程仓库抓取
git fetch <remote> (remote默认是origin)
推送到远程仓库
git push <remote> <branch>
重命名
git remote rename <oldname> <newname>
删除
git remote remove <remote>
分支
创建
git branch <branchname>
拉取远程分支在本地创建一个同样的分支
git checkout -b 本地分支名x origin/远程分支名x
git fetch origin 远程分支名x:本地分支名x
切换
git checkout <branchname>
新建和切换
git checkout -b <branchname>
合并到本分支上
git merge <frombranch>
删除
git branch -d <branchname>
远程分支:
查看
git remote show <remote>
推送
git push <remote> <branch>
跟踪
git checkout -b <branch> <remote>/<branch>
删除
git push <remote> --delete <branch>
拉取
git pull
等同于
git fetch
git merge
变基
git rebase <tobranch>
版本间移动
版本回退(之前)
git log(查询版本号xxx)
git reset --hard xxx
git reset --soft xxx
git reset --mixed xxx
版本还原(之后)
git reflog(查询版本号xxx)
git reset --hard xxx
git reset --soft xxx
git reset --mixed xxx
参数区别:
soft:仅仅将头指针位置改变,工作区和暂存区(已add内容)不变
mixed(default):头指针和暂存区恢复,工作区内容不变
hard:全都变了
配置等其他相关
.gitconfig配置文件中修改(/etc/gitconfig或者:~.gitconfig)
别名(图形化分支)
记住密码
[alias]
logg = log --graph --decorate --oneline --simplify-by-decoration --all
[credential]
helper = store