or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Introduction to Git –- Fall 2020
Lecture 4: Commits
Slides: https://hackmd.io/@hpc2n-git-2020/L4-commits
Investigating history
The history can be investigated with the
git log
command:git log <ref>
lists commits that are reachable from<ref>
HEAD
git log -n <n> <ref>
list only<n>
most recent commitsgit log --oneline <ref>
list only the shortened hash and the commit message:git log --format=oneline <ref>
oneline
,short
,medium
,full
,fuller
,email
, andraw
git log --grep=<regexp> <ref>
displays commits that match a regular expression<regexp>
:git log --{after,before} <date> <ref>
displays changes committed after/before<date>
:git log --name-status <ref>
display files that were changed:git log --stat <ref>
displays more informationgit log -p <ref>
displays even more information (diffs)--graph
visualizes the commit tree and--all
displays all branches:Reflog
git log
command displays only changes that effect the commit treegit reflog
commandgit log -g <ref>
)2f529ae
) still exists in the reference log:Comparing commits
We can compare commits with
git diff
:HEAD
:Navigating the commit tree
git checkout
andgit reset
commandsmaster
:git checkout
to move tod3c6c63
:git checkout
moves only theHEAD
, the tip of the branch is not modified:git checkout
attempts to keep local modifications to the files in the working treeDetached
HEAD
If we
git checkout
on the tip of a branch…Then we will get the following warning:
HEAD
no longer points to the tip of the branch:HEAD
mode, then:git checkout <tip_ref>
:Git reset
git reset
to move theHEAD
:<option>
--soft
--mixed
--hard
HEAD
mode, then only theHEAD
gets moved:git checkout
to a file discards all changes made to the file:git reset
to a file unstages the file:An example:
Tagging
HEAD
.Naming commits
HEAD
,master
,first
, etcrefs/heads/master
,refs/tags/first
We can also refer to the ancestors of a commit:
ref~n
returns then
'th ancestor ofref
.ref = ref~0
ref~ = ref~1
ref~~ = ref~2
We can also refer to the parents of a commit:
ref^n
returns then
'th parent ofref
.ref = ref^0
ref^ = ref^1
ref^ = ref~
ref^^ != ref^2
:Cleaning the working tree
git reset <option> HEAD
<option>
--soft
--mixed
--hard
Unstaged files are cleared with
--dry-run
-d
-i
-f
-i
is not given-x
-X
Stashing changes
HEAD
.HEAD
to1cb12030
:git stash
stashes files that are already added to the index.--keep-index
option.Multiple commits from a single set of edits
--keep-index
).Making changes to the commit tree
Remark
The following techniques should be used with caution!
It is generally a bad idea to modify the commit tree if the changes have been pushed to a remote.
We will return to this during Lecture 6.
Reminder
HEAD
determines the branch you are on:HEAD
mode, thengit reset
moves only theHEAD
:Discarding the last commit
HEAD
mode, then we can discard the latest commit with thegit reset
command:git reset
defaults togit reset --mixed
=> the state of the working tree is kept.Changing the last commit
Imagine a situation where you have just committed your changes and then realized that you have made a mistake.
Forgotten file, incorrect commit message, etc
The problem can be fixed easily in two steps: Step #1 is to stage the forgotten changes:
HEAD
with a corrected commit:git commit
call.git reset --soft HEAD^
:Reverting a commit
We are first going to create a commit that replaces the content of a file:
We later discover that the commit was a mistake and revert it:
We can see that the revert commit simply removes the changes made in the first commit and restores changes make in the preceding commit:
Exercises
git log
HEAD
git stash