-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_first_commit.txt
79 lines (66 loc) · 3.88 KB
/
my_first_commit.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
73
74
75
76
77
78
79
So now GIT is set up, lets use it!
Under-the-hood, GIT tracks 3 things. These are:
1) The working tree - where the development is happening and files are created or modified
2) The staging area - where modified files are waiting for commit
3) The .git directory (the repo) - where commits are tracked
##########
# STATUS #
##########
So now you have some files and you have been busy and modified, closed and saved the files.
Next you need to let GIT know that it is time to update the repo.
You do this by adding the modified files to the staging area and then committing the modifications to the repo.
But first lets take a look at one of the most common commands you will use
git status -s
Using the -s (or --short) flag produces more consice git status output showing 3 columns:
column 1: staged are status
column 2: working tree status
column 3: filename
Example: ?? file1 = new untracked file
A_ file2 = staged new file
_M file3 = modified + unstaged
M_ file4 = modified + staged
MM file1 = indicates earlier modifications are staged, and latter modifications are unstaged
##########
# Commit #
##########
REMEMBER: after making modifications, close all files before staging and commiting to remove the environment lock on the files. Otherwise, things may not go as expected!
# First add new/modified files to the staging area
git status -s # notice new files are untracked and modified files are unstaged
git add . # stages all new or modified files only + ignores deleted files. (NB: don't use '*')
# if relocating a file, git status should output 'R oldlocation/file -> newlocation/file'
git status -s # notice new/modified files are now ready (staged and tracked) to be committed
# Now the files have been staged, they can be commited (but check status first so you know what you are commiting)
git commit -m 'meaningful description' # Add an informative verbose 'meaningful description' of the modifications to be committed
##########################
# Veiw commit history #
##########################
# See: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
git log --oneline # lists commits made
git log -p # adds detailed description of changes
git log-p -2 # shows only last 2 commits
git log --stat # adds graph showing amount of change in each file
################
# Undo Changes #
################
See: https://blog.git-init.com/how-to-undo-changes-in-git-using-reset-revert-and-restore/
git restore <file> # discard modifications to filename, file reverts to previous commit
git restore --staged <file> # unstage filename (undo 'git add filename')
git reset filename # unstage filename (undo 'git add filename')
git reset --hard <hash> # return repo to previous commit, careful, you will lose all changes not commited
- If you accidently delete from working directory with 'rm filename', it will still be tracked by git so either:
- 'git rm filename' then commit to make permanent or
- return to previous commit
###########################
# Other relevent commands #
###########################
git add filename # stages filename, and tracks if new
git diff # details of unstaged changes
git diff --staged # details of staged changes (i.e. what will change when you commit)
git diff hash1 hash2 # show differences between previous commits
git commit --amend -m 'meaningful message' # amend previous commit
# Warning: don't use --amend if you’ve already pushed the last commit
# Fix: If you do make that mistake a forced push should resolve it 'git push -f'
# https://stackoverflow.com/questions/18588974/git-prevents-pushing-after-amending-a-commit
git mv oldname newname # to rename files
git rm filename # untrack filename + delete from working directory
git rm --cached filename # untrack filename + DON'T delete from working directory