Git学习总结

Repository

要实现的功能命令
创建Repositorygit init
克隆Repositorygit clone <repo>默认会克隆远程中央仓库origin的master分支
克隆特定分支git clone -b <branch-name> <repo>
将本地repo推向远程repogit remote add origin <remote repo url>
git push -u origin --all # pushes up the repo and its refs for the first time
git push -u origin --tags # pushes up any tags

版本控制

要实现的功能命令
查看当前文件状态git status
跟踪新文件或暂存已修改的文件git add <file>
移除文件git rm <file>
取消对文件的修改git checkout -- <file>
提交更新git commit -m
暂存已修改文件 + 提交更新git commit -a -m
修改最后一次提交git commit --amend
取消已经暂存的文件git reset HEAD <file>
版本回退上一次commitgit reset --hard HEAD^
版本回退到HEAD之前的第n版本git reset --hard HEAD~n
版本回退到某一次commitgit reset --hard <commit-id>

历史记录

要实现的功能命令
查看提交历史git log
查看最近n次的提交历史git log -n
查看提交历史,每条记录一行显示git log --pretty=oneline
查看命令历史git reflog

分支Branch

分支有两种,本地分支和远程分支。远程分支名 = 远程仓库名/分支名,例如origin/master。

要实现的功能命令
创建本地分支git branch <branch-name>
切换分支git checkout <branch-name>
创建并切换分支git checkout -b <branch-name>
删除分支git branch -d/-D <branch-name>
-d表示该分支必须处于完全merge的状态,-D表示不管该分支merge状态
合并分支git merge <branch-name>
将branch-name分支合并到当前分支
创建远程分支git push --set-upstream origin <branch-name>
git push origin <local-branch-name>:<remote-branch-name>
git push -u origin <remote-branch-name>
本地分支跟踪远程分支git branch --set-upstream-to=origin/<branch> <local branch>
删除远程分支git push origin :<branch-name>
记忆这条命令的方法:创建远程分支 git push [远程名] [本地分支]:[远程分支] ,如果省略 [本地分支],那就等于是将空白分支把它变成[远程分支]。
获取远程分支git pull <远程主机名> <远程分支名>:<本地分支名>
git fetch <远程主机名> <远程分支名>:<本地分支名>
git checkout -b <本地分支名> <远程主机名>/<远程分支名>
git branch --track <本地分支名> <远程主机名>/<远程分支名>
显示所有本地分支git branch
查看各个分支最后一次提交的信息git branch -v
查看远程分支git branch -r
显示所有分支包括本地和远程分支git branch -a
将本地提交重新置于HEAD之后git rebase <branch-name>

Git配置

git config <file> name value

Git配置文件信息

配置文件位置file-option说明
$(prefix)/etc/gitconfig--system系统级
$XDG_CONFIG_HOME/git/config
~/.gitconfig--global用户级
$GIT_DIR/config--localRepository级,仅对当前Repo有效


使用示例

配置命令
长期存储密码git config --global credential.helper store
用户名git config --global user.name 'Git User'
邮箱git config --global user.email me@gmail.com
代理git config --global http.proxy 'http://192.168.1.100:8080'


最终配置文件内容如下

[credential]
    helper = store
[user]
    name = Git User
    email = me@gmail.com
[http]
    proxy = http://192.168.1.100:8080

忽略文件

有时希望忽略掉一些文件或目录(如.class文件等),这是就可以创建.gitignore文件来实现。

文件 .gitignore 的格式规范如下:
1. 所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
2. 可以使用标准的 glob 模式匹配。
3. 匹配模式最后跟反斜杠(/)说明要忽略的是目录。
4. 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

使用示例

#ignore all .class file
*.class
#ignore .metadata folder
.metadata/

学习资源

Git官网: http://git-scm.com/book/zh/v1
Atlassian的Git Tutorial:https://www.atlassian.com/git/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值