Git学习记录:Notes for Codecademy

Codecademy学习记录

Basic Git Workflow(Git工作流初探)

  • git三大部分
  • 编辑一份文件,名为scene-1.txt
  • 查看当前工作目录
  • 将当前工作目录变为Git项目
  • check the status of the changes
    untracked files说明Git已经检测到文件,但是Git尚未开始追踪变化(Git sees the file but has not started tracking changes yet)
  • 将文件加入到git的staging area
  • 查看添加之后的状态
  • 比较改动后的文件差异
    在scene-1.txt文件上略作改动,并用git diff scene-1.txt看Staging Area上该文件和Working Directory上的差异
    注意,此时对文件的改动是在Working Directory上的,如果没有使用git add filename的话,改动的文件不会上传到staging area
  • 使用git commit将文件提交到Repository
  • 使用git log查看提交记录
    橘黄色的40字节的编码(40-character code)是SHA码,即本次提交的指纹码
    除此之外,还包括提交者、提交日期以及提交注释


**How to Backtrack(如何撤销更改) **

  • 编辑一下scene-5.txt文件,git commit之后使用git show HEAD命令查看当前头节点
    scene-5.txt上新增加的内容被标记成绿色并展示出来


  • 在工作目录上的scene-5.txt文件上做若干改动
    比如将flames改成balloons


  • 使用git checkout HEAD scene-5.txt使文件恢复成服务器上的版本
    此时,你并没有把在本地工作目录上改动的文件提交到Staging Area或者Repository,如果你想要丢弃改动
    使用git checkout HEAD filename,其效果等同于Android Studio中revert


  • 使用git add filename1 filename2增加多个文件到Staging Area
    在scene-3.txt以及scene-7.txt两个文件进行改动,并且提交到Staging Area
  • 使用git reset HEAD filename将指定的文件从Staging Area中移除
    此时,scene-3.txt以及scene-7.txt文件已经提交到了Staging Area中,故意在scene-2.txt文件上做一些改动,然后将scene-2.txt文件add到Staging Area中,此时如果想要还原scene-2.txt的提交,使用git reset HEAD scene-2.txt
改动scene-2.txt并add到Staging Area中的状态
  • 重新设置提交节点
重设节点之前的状态

现在想要还原提交至指纹码首6位为8f18e6的节点,使用git reset 8f18e6重设节点,重设节点之后,scene-3.txt以及scene-7.txt上的改名变成了unstaged changes

重设节点示意图


Git Branching(Git分支)

  • 使用git branch查看当前所在分支
  • 使用git branch fencing创建一个名为“fencing”的新分支
  • 使用git checkout fencing切换到fencing分支
  • 合并fencing分支到master分支
    先通过git checkout master切换到master分支,然后通过git merge fencing,将fencing分支以fast forward方式合并到master分支
  • 当merge conflict发生时的处理
    在master分支上对一份文件进行了改动,而后又在其他branch上对文件进行了改动,此时,如果要将其他branch合并到master上,就会发生合并冲突。
    比如说在master的resume.txt文件上改动了第30行并且最终commit到Repository上,而后又切换到了fencing分支,并且在resume.txt文件的同一行进行了改动,然后commit到Repository上,此时再切换回master分支,使用git merge fencing命令,会出现如下结果:

此时删除掉<<<、===这样的字符,重新编辑resume.txt文档,然后再次提交文件

  • 使用git branch -d fencing删除fencing分支

Git Teamwork(Git协同工作)

  • 使用git clone remote_location clone_name
    使用git clone science-quizzes my-quizzes将remote上的science-quizzes文件夹克隆到本地,并且命名为my-quizzes
  • 使用git remote -v查看已经存在的远程分支
  • 使用git fetch查看远程分支上的改动
  • 使用git merge origin/master将远程分支上的更新同步到本地
  • 多人协作基本流程
    1.从远程服务器同步更新
    2.创建新的分支,以编辑新的内容
    3.commit改动的文件到该新分支上
    4.从远程服务器同步更新(以防在自己编辑新内容的过程中,远程分支上的内容又更新了)
    5.将自己的分支push到远程分支以供review
将本地的改动commit到自己创建的本地分支
使用git push origin your_branch_name将本地分支的改动提交到远程分支

Git命令汇总

git init:creates a new Git repository
git status:inspects the contents of the working directory and staging area
git add:adds files from the working directory to the staging area
git diff:shows the difference between the working directory and the staging area
git commit:permanently stores file changes from the staging area in the repository
git log:shows a list of all previous commits
git show HEAD:shows the last commit
git checkout HEAD filename:Discards changes in the working directory
git reset HEAD filename:Unstages file changes in the staging area
git reset SHA:Can be used to reset to a previous commit in your commit history.
git branch:Lists all a Git project's branches.
git branch new_branch_name:Creates a new branch.
git checkout branch_name:Used to switch from one branch to another.
git merge branch_name:Used to join file changes from one branch to another.
git branch -d branch_name:Deletes the branch specified.
git clone:Creates a local copy of a remote.
git remote -v:Lists a Git project's remotes.
git fetch:Fetches work from the remote into the local copy.
git merge origin/master:Merges origin/master into your local branch.
git push origin <branch_name>:Pushes a local branch to the origin remote.

其他资料

Why did my Git repo enter a detached HEAD state?
git 查看远程分支、本地分支、删除本地分支
Git之detached HEAD
git checkout之一 HEAD基本和detached 状态

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. GIT命令 git init在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个re...
    江边一蓑烟阅读 842评论 0 0
  • 1.题目相关 标签:DP 单调队列优化 题目地址:不是VIP没法看。。。。 题目大意:给定N和K,表示有N个数,在...
    Zhu8655阅读 314评论 0 1
  • 又是一年一度的高考季,今天是高考第一天,仿佛老天爷也眷顾这些孩子,今天的气温特别适宜。 今年的高考,是史上最严的一...
    迪迪2011阅读 257评论 0 1
  • 看这部电影并非本意,而是戏剧导论必学篇目。剧本很精彩,梅姨的眼神杀真是绝了,没有数过到底看了多少部她的电影,可是部...
    说吧_记忆阅读 497评论 0 0