git入门大全

来源:互联网 发布:scratch创意编程 pdf 编辑:程序博客网 时间:2024/05/16 15:38

http://www.cnblogs.com/ang-/p/7352909.html

前言

以前写个一个git小结,但是实际上并不够用。于是结合实际工作上碰到的一些情况,参考了一些资料,重新总结了一下。目标是在日常工作中不用再去查阅其他的资料了,如果有什么遗漏或者错误的地方,请评论指出!

基本概念

Workspace:工作区

Index / Stage:暂存区

Repository:仓库区(或本地仓库)

Remote:远程仓库

文件几种状态

  • untracked:git未跟踪的文件,新增的文件未 git add 就会处于这种状态
  • not staged:被索引过又被修改了的文件
  • staged:通过 git add后即将被提交的文件

创建新仓库

# 在当前目录git init

配置

# 显示当前的Git配置git config –list# 编辑Git配置文件git config -e [–global]# 设置提交代码时的用户信息git config [–global] user.name "example"git config [–global] user.email "example@gmail.com"# 配置自动换行,提交到git时自动将换行符转换为lfgit config --global core.autocrlf input# 配置密钥ssh-keygen -t rsa -C example@gmail.com # 生成密钥ssh -T git@github.com # 测试是否成功# 配置别名,--global 表示全局配置git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

检出仓库

# 创建一个本地仓库的克隆版本:git clone /path/to/repository# 如果是远端服务器上的仓库:git clone username@host:/path/to/repository# 克隆到自定义文件夹:git clone username@host:/path/to/repository my-cloned-repo

新建仓库常见流程

# 初始化git init# 获取状态git statusgit add README.mdgit commit -m "message"# 连接远程仓库git remote add origin git@github.com:example/test.git# 推送内容到远程仓库的同时设置默认跟踪分支git push -u origin master

gitignore

vim .gitignore!为模式取反*.a!lib.a

添加、删除

# 添加指定文件到暂存区git add [file1] [file2] ...# 添加指定目录到暂存区,包括子目录git add [dir]# 添加当前目录的所有文件到暂存区,.或*代表全部添加git add .# 添加每个变化前,都会要求确认# 对于同一个文件的多处变化,可以实现分次提交git add -p# 删除工作区文件,并且将这次删除放入暂存区git rm [file1] [file2] ...# 停止追踪指定文件,但该文件会保留在工作区git rm --cached [file]# 文件重命名,并加入暂存区git mv [file-original] [file-renamed]# 通配符批量移动git mv *.html src/

提交

git commit -m "message"# 补提交文件,提交时漏掉了某些文件时不应该再单独提交一次git commit --amend# 覆盖提交日期,不知道有啥实际用途git commit -m "message" --date "2017-01-01 00:00:00"

branch

# 列出所有本地分支git branch# 新建一个分支,但依然停留在当前分支git branch [branch-name]# 新建一个分支,并切换到该分支git checkout -b [branch]# 切换到指定分支,并更新工作区git checkout [branch-name]# 切换到上一个分支git checkout -# 建立追踪关系,在现有分支与指定的远程分支之间git branch --set-upstream [branch] [remote-branch]# 根据一个特定的提交创建新分支,忘记开新的分支就修改并提交了代码时的处理git branch test-branch HEAD~1# 删除分支git branch -d [branch-name]# 推送分支到远程仓库git push origin [branch-name]# 删除远程分支$ git push origin --delete [branch-name]$ git branch -dr [remote/branch]

tag

# 列出所有taggit tag# 显示 tag list 和注解git tag -n# 在当前commit新建一个轻标签git tag [tagname]# 在指定commit新建一个taggit tag [tagname] [commit]# 添加注解标签git tag -a [tagname]# 添加注解标签并添加注解git tag -am "message" [tagname]# 删除本地taggit tag -d [tagname]# 删除远程taggit push origin :refs/tags/[tagName]# 推送所有taggit push --tags# 新建一个分支,指向某个taggit checkout -b [branch] [tagname]# 切换到taggit checkout [tagname]# tag和分支同名时,显示指定切换到taggit checkout tags/[tagname]

远程仓库和合并分支

git remote# 查看远程仓库的 URLgit remote -v# 添加远程仓库git remote add origin git@github.com:example/test.git# 删除远程仓库git remote rm origin# 修改远程仓库地址git remote set-url origin [url]# 将本地的远端和远端进行同步git fetch origin# 将本地的远端合并到本地分支git merge origin/master# 拉取远程仓库,这相当于上面两条命令git pull origin master# 使用rebase可以使提交的历史记录显得更简洁git rebase mater# 也可以指定分支:git pull origin remote:local# 推送:git push origin local:remote

改写提交

# 摘出某个提交git cherry-pick [hash]# 交互式提交,当涉及提交修改时使用,如 squash、调整 commit 顺序等git rebase -igit rebase -i HEAD~4# 将 upstream 后的 commit 节点嫁接到 newbase,如果有 branch ,会先 checkout 到这个 branch,再 rebasegit rebase --onto <newbase> <upstream> <branch># 删除 topicA~3、topicA~4 这两个 commitgit rebase --onto topicA~5 topicA~3 topicA# 中途要停止rebase操作git rebase --abort# 复原到rebase之前的状态git reset --hard ORIG_HEAD

1)squash合并多个提交:

在编辑 commit message 时 hash 值前的p(pick)表示 use commit,s(squash)表示 use commit, but meld into privious commit