-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNotes.txt
72 lines (49 loc) · 3.31 KB
/
Notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Init
git init is used to create a repository.
Status
git status tells us the state of our working directory.
.gitignore Files
.gitignore files tell git what it should completely ignore.
Adding Things
git add tells git to track specific files or folders
To selectively stage only certain changes using Patch Mode, we can use git add –p
Commits
A commit is a snapshot of the state of our repository at a certain point in time.
To commit changes, we first stage them using git add, then use git commit
We can also use Patch Mode and immediately commit by using git commit –p
Commit Messages
Commits should ideally contain only a few changes each: this lets us revert mistakes more easily
Commit messages are used to describe the changes we’ve made to the code
Write commit messages to explain “what this will do”, not “what I did”
Commit Logs
git log shows us all the commits that were made.
git log --oneline shows a summary view of all commits.
Diffs
Each commit has an SHA-1 hash, used to ID it
git diff <old commit> <new commit> shows us what’s changed between two commits
git diff alone shows the difference between our current state and the last commit
Branching
Git branches are like parallel universes where we can experiment or work on different things
git branch gives a list of all branches in the repository
To create a new branch, type git branch <branch name>
git checkout <branch name> switches to that branch
Branches are not automatically updated: if left alone, they remain the way they were at the time they were created
Merging
To merge changes made on another branch into the current one, we use git merge <branch name>
Merge conflicts occur when we make changes on different branches that git can’t automatically combine – the changes made contradict or conflict with each other
When a merge conflict occurs, git shows both of the changes that are conflicting – we have to manually edit the file to resolve the conflict
After resolving a conflict, we have to commit the changes manually
If no merge conflicts occur, git will automatically commit the merge for us
Checkout (for commits)
A git repository is basically like a tree made up of different commits
Branches are simply specific commits with a label attached to them. HEAD is a pointer to the commit we’re currently “visiting” in the repository
git checkout shifts the HEAD pointer around the repository: it can be moved to another branch, or a specific commit (git checkout <commit hash>)
This lets us revisit our older commits
Undoing
If we want to replace a single file with an older version of it, git checkout <commit> <file> will take the version of the file from the specified commit, then stage it for us
If we want to undo a specific commit, git revert <commit> will create a new commit which undoes the specified one
Reset
git reset can be used to undo commits: git reset <commit to restore to> <mode>
In –soft mode, reset rolls back to the specified commit but lets us immediately commit all the changes that were made since then
In –mixed mode, reset rolls back to the specified commit and unstages all the changes made since then – we have to manually stage and commit them
In –hard mode, reset rolls back to the specified commit and DROPS all changes made since then