-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgit_commands
211 lines (112 loc) · 5.43 KB
/
git_commands
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This is borrowed from https://gist.github.com/neilgee/9442209
// =============================================================
/* Set up Git Configuration */
git config --global user.email "[email protected]"
git config --global user.name "Thom Parkin"
git config --global core.editor "vi"
git config --global color.ui true
/* See Git configuration */
git config --list
/* to initialise a local repository */
git init
/* adds a file to the repo */
git add
/* commit the change to git */
git commit -m "Describe how this change affects the project"
/* see the commits */
git log
/* Basic Commands */
git status /* the command 'git status' tells which files are not added or committed from Working to Staging to Repository */
git commit -m "Describe how this change affects the project" /* Commits and changes to all files that are in Staging */
git diff /* show changes between Working and Repository, no file supplied shows all files */
git diff --staged /* shows changes between Staged and Respository */
git rm file.txt /* will remove file from working then git commit to also remove from Repo */
git mv /* rename or move files - then git commit to move to Repo */
git commit -am "Describe how these changes affect the project" /* adds all files straight to Repo from Staging if they have changes - meaning they skip git add */
git checkout -- file.txt /* restore Repo file to Working Directory from current branch */
git reset HEAD file.txt /* Move a file from Staging back to Working Tree */
git commit --amend -m "Updated Message" file.txt /* Change last commit to Repo (only last one can change) */
/* Reverting --soft --mixed --hard */
git log /* gets the SHA-1 hash so you can see the commits where you will return */
git reset --soft SHA /* changes Repo but not Index or Working Tree */
git reset --mixed SHA /* changes Repo and Index but not Working */
git reset --hard SHA /* changes Index and Working Tree */
git clean -f /* remove untracked files from Working Tree */
.gitignore /* ignores files to track in Working Tree / track the .gitignore file */
Global Ignore /* create in home folder */
.gitignore_global
/* Add in */
.DS_Store
.Trashes
.Spotlight_V100
git config --global core.excludesfile ~/.gitignore_global /* add to gitconfig */
/* Stop tracking changes */
rm --cached file.txt /* leaves copy in Repo and Working Tree */
/* Track Folders changes
Add an invisble file to a folder like .gitkeeper then add and commit */
/* Commit Log */
git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="Thom"
git log --grep="temp"
/* Show Commits */
git show dc094cb /* show SHA1 */
/* Compare Commits
Branches */
git branch /* Show local branches * is the current one */
git branch -r /* Shows remote branches */
git branch -a /* Shows local and remote */
git branch newbranch /* creates a new branch */
git checkout newbranch /* switch to new branch */
git checkout -b oldbranch /* creates and switches to new branch */
/* Diff in Branches */
git diff master..otherbranch /* shows diff */
git diff --color-words master..otherbranch /* shows diff in color */
git branch --merged /* shows any merged branches */
/* Rename Branch */
git branch -m oldname newname
/* Delete Branch */
git branch -d nameofbranch
/* Merge Branch */
git merge branchname /* be on the receiver branch */
/* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */
git merge --abort /* Abort basically cancels the merge */
/* Manually Fix Files and commit
The Stash */
git stash save "Describe what this contains"
git stash list /* shows whats in stash */
git stash show -p stash@{0} /* Show the diff in the stash */
git stash pop stash@{0} /* restores the stash deletes the tash */
git stash apply stash@{0} /* restores the stash and keeps the stash */
git stash clear /* removes all stash */
git stash drop stash@{0}
/* Remotes
You fetch from the remote server, merge any differences - then push any new to the remote - 3 branches work remote server branch, local origin master and local master
Create a repo in GitHub, then add that remote to your local repo */
git remote add origin https://github.com/ParkinT/mastering_git
git remote /* to show all remotes */
git remote remove origin /* to remove remote */
git remote rm origin /* to remove remote */
/* Push to Remote from Local */
git push -u origin master
/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */
git clone https://github.com/ParkinT/mastering_git folderName
/* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */
git push
/* Fetch changes from a cloned Repo */
git fetch origin /* Pulls down latest committs to origin/master not origin
Fetch before you work
Fetch before you pull
Fetch often */
/* Merge with origin/master */
git merge origin/master
/* git pull = git fetch + git merge
Checkout/Copy a remote branch to local */
git branch branchname origin/branchname /* this will bring the remote branch to local and track with the remote */
/* Delete branch */
git branch -d branchname
/* Checkout and switch branch and track to remote */
git checkout -b nontracking origin/nontracking
/* Remove remote branch */
git push origin --delete branch