Git学习总结

来源:互联网 发布:fgo淘宝石头号注意事项 编辑:程序博客网 时间:2024/06/04 18:32

Repository

要实现的功能 命令 创建Repository git init 克隆Repository git clone <repo>默认会克隆远程中央仓库origin的master分支 克隆特定分支 git clone -b <branch-name> <repo> 将本地repo推向远程repo git 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> 版本回退上一次commit git reset --hard HEAD^ 版本回退到HEAD之前的第n版本 git reset --hard HEAD~n 版本回退到某一次commit git 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 --local Repository级,仅对当前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