GIT brief introduction from cs50
❓what is git and what can it do for me?
- 代码/文件管理工具
- Keeps track of changes to code
- Test changes to code without losing the original
- Revert back to old versions of code.
git 基础指令
git init初始化仓库(repositories)git add <filename>提交一个文件到缓冲区:git add .全部提交
git diff显示所有未add的修改git diff <filename>显示与暂存区文件的差异git diff <version> <filename>显示某个文件两个版本之间的差异
git commit -m "message"把缓冲区的内容提交到分支:git commit -am "message"可实现add和commit两步合并-am会把已被 Git 跟踪(tracked)的已改文件自动暂存并提交,但不会包括未被跟踪(untracked/new)文件。要提交新文件仍需先git add。
git status仓库状态,你现在在哪个branch,文件情况git branch显示所有branchgit branch <name>创建新的branchgit checkout <name>跳转到对应branchgit checkout -b <name>同时完成上面两个操作git branch -D <name>删除分支
merge conflict example:
<<<<<<<<<<HEAD
int b = 2; (你的修改)
============
int b = 0; (别人的修改)
>>>>>>>>>> 哈希值,标识提交
int b = 2;或者把两条按自己的方式合并一下。
git log查看提交等等日志git log --all --graph --decorate可视化历史记录
git reset回滚:git reset --hard <commit>输入需要的提交hash值,可在log里查到git reset --hard origin/master回滚到remote branch
git merge <name>把某一分支与当前所在分支merge,merge到当前这个分支git mergetool使用工具处理合并冲突
pull request对他人的库修改后提交的request,希望能merge,见github。
远端操作
git remote列出远端git remote add <name> <url>添加远端
git clone <url>,从远程仓库中copy一份,存在自己的电脑里git pull把线上仓库更改下载到本地仓库,可能出现git fetch从远端获取对象/索引+git merge等价于git pull
git push提交到远程仓库(没有访问权的话必须要先pull request)git push <remote> <local branch>:<remote branch>将对象传送至远端并更新远端引用
撤销
git commit --amend: 编辑提交的内容或信息git reset HEAD <file>: 恢复暂存的文件,HEAD是指分支名git checkout -- <file>: 丢弃修改git restore: git2.32 版本后取代git reset进行许多撤销操作