Skip to content

Commit d6aa157

Browse files
committed
Multiple additions, specially to Handling remote repositories' section
1 parent cff755e commit d6aa157

File tree

1 file changed

+73
-10
lines changed

1 file changed

+73
-10
lines changed

git-commands.md

+73-10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ vim hello.py
4242
git add hello.py myname.py
4343
git status
4444
git commit -m "Added myname module. Minor modification to hello.py"
45+
```
46+
47+
- Instead of specifying each file in the `git add` line, we can do:
48+
```
49+
git add --all
4550
```
4651

4752
7. The `.gitignore` file is special, because it contains the names of the files which should be ignored by Git, like for instance, python `.pyc` files. It is Ok to use wildcards inside it
@@ -56,26 +61,35 @@ git commit -m "Created .gitignore"
5661
git log
5762
```
5863

59-
9. You made a change in `.gitignore`, but then changed your mind and **dropped it**. This instruction changes file back to where it was at last commit
64+
9. The command `git log` provides, besides the commit messages of the changes, the **_SHA_** ID of every commit. Then, one can use the command `git show SHA` to get a detailed description of the changes related to that commit:
65+
```
66+
git show fcf22e5d3cf1c
67+
```
68+
69+
10. You made a change in `.gitignore`, but then changed your mind and **dropped it**. This instruction changes the file back to where it was at last commit
6070
```
6171
vim .gitignore
6272
git checkout -- .gitignore
6373
```
6474

65-
10. You decided to apply and commit other change
75+
11. You decided to apply and commit other change
6676
```
6777
vim .gitignore
6878
git status
6979
git add .gitignore
7080
git commit -m "Modifying .gitignore to exclude all .pyc files"
7181
```
7282

73-
11. Take a look at what is different from our last commit. In this case we want the diff of our most recent commit, and we can refer to it using **_HEAD_**
83+
12. Take a look at what is different from our last commit. In this case we want the diff of our most recent commit, and we can refer to it using **_HEAD_**
7484
```
7585
git diff HEAD
7686
```
87+
If what we want to check is whether we are about to commit a file with whitespace errors, let's use:
88+
```
89+
git diff --check
90+
```
7791

78-
12. We can unstage files by using the `git reset` command
92+
13. We can unstage files by using the `git reset` command
7993
```
8094
git reset octofamily/octodog.txt
8195
```
@@ -97,18 +111,21 @@ git checkout -b my_new_feature
97111
git branch
98112
```
99113

100-
4. Back to the top of the main branch (**_master_**)
114+
4. Back to the top of the main branch (**_master_**), and confirm we are indeed there
101115
```
102116
git checkout master
117+
git branch
103118
```
104119

105-
5. Change back to the new branch
120+
5. Change back to the new branch. Confirm we are where we want
106121
```
107122
git checkout my_new_feature
123+
git branch
108124
```
109125

110-
6. We are inside the NEW `my_new_feature` branch, so further changes will go in there
126+
6. We are inside the **NEW** `my_new_feature` branch, so further changes will go in there
111127
```
128+
vim hello.py
112129
git add hello.py
113130
git commit -m "Added code for feature x"
114131
```
@@ -162,11 +179,13 @@ git commit -m "Renaming file"
162179
2. Removing files (wildcards are also valid)
163180
```
164181
git rm <target-file>
182+
git commit -m "Removed target file"
165183
```
166184

167185
3. Removing directories
168186
```
169187
git rm -r folder_of_cats
188+
git commit -m "Removed directory 'folder_of_cats'"
170189
```
171190

172191
4. Set user email and name _for every repository_ in your computer
@@ -181,9 +200,21 @@ git config user.email "[email protected]"
181200
git config user.name "User Surname"
182201
```
183202

203+
6. Several aspects of Git behavior, like aliases, may be set up at file `~/.gitconfig`. An example content could be:
204+
```
205+
[user]
206+
name = architest
207+
208+
[color]
209+
ui = true
210+
[alias]
211+
co = checkout
212+
br = branch
213+
```
214+
184215
## Handling remote repositories
185216

186-
1. Download a full repository
217+
1. Download (_clone_) a full repository
187218
```
188219
git clone https://github.com/jima80525/github-playground.git
189220
```
@@ -208,16 +239,48 @@ git push origin master
208239
git push -u origin master
209240
```
210241

211-
6. Pulling, but in a more compact way
242+
6. _Pulling_, but in a more compact way
212243
```
213244
git pull origin master
214245
```
246+
'Pulling' can be seen as the combination of two commands: `git fetch`, which fetches down all the changes on the server that we don't have yet, but doesn't modify the working directory, and `git merge`, which combines remote and local data.
215247

216-
7. Add a remote repository to push our local repo to the GitHub server (i.e.: We created out repository from scratch and we are setting `origin` in order to be able to _push it_ to a remote server)
248+
7. Add a remote repository to push our local repo to the GitHub server (i.e.: We created our repository from scratch, and we are setting `origin` in order to be able to _push it_ to a remote server)
217249
```
218250
git remote add origin https://github.com/try-git/try_git.git
219251
```
220252

253+
8. In order to make a _tracking branch_, i.e., a local branch that automatically tracks a remote branch, we can do:
254+
```
255+
git checkout -b serverfix origin/serverfix
256+
```
257+
In this way, the local _serverfix_ branch tracks the remote _serverfix_ branch in _origin_. A shorthand for the former operation is:
258+
```
259+
git checkout --track origin/serverfix
260+
```
261+
If the local branch doesn't exist, and you know the (unique) name of the branch in the remote, we can even do:
262+
```
263+
git checkout serverfix
264+
```
265+
If one wants to use a different name for the local branch (_myfix_ in this example):
266+
```
267+
git checkout -b myfix origin/serverfix
268+
```
269+
270+
9. In order to see which tracking branches have been setup, we use the `-vv` option with `git branch`:
271+
```
272+
git branch -vv
273+
iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets
274+
master 1ae2a45 [origin/master] deploying index fix
275+
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it
276+
testing 5ea463a trying something new
277+
```
278+
Or, even better, to get completely up-to-date numbers from all tracked remotes:
279+
```
280+
git fetch -all
281+
git branch -vv
282+
```
283+
221284
## Configure Git to sync your fork with the original repository
222285

223286
1. First, fork the repository using GitHub facilities for that

0 commit comments

Comments
 (0)