Skip to content

Commit 4d87a03

Browse files
committed
Links and tidy up for new Set-up page
1 parent 7ccaa11 commit 4d87a03

File tree

4 files changed

+149
-88
lines changed

4 files changed

+149
-88
lines changed

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ <h2>Command Line</h2>
111111
<h2>Version Control</h2>
112112
<ul>
113113
<li><a href="version-control/introduction/tutorial.html">Introduction to version control</a></li>
114-
<li><a href="version-control/command-line/tutorial.html">Introduction to the git command line</a></li>
114+
<li><a href="version-control/set-up/tutorial.html">Get set-up with Git and GitHub</a></li>
115+
<li><a href="version-control/command-line/tutorial.html">Introduction to the Git command line</a></li>
115116
</ul>
116117

117118
<h2>Other Useful Resources</h2>

version-control/command-line/tutorial.md

Lines changed: 104 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,81 @@ layout: page
33
title: Introduction to the Git command line
44
---
55

6-
## Introduction to the Git command line
7-
8-
**PREREQUISITE:** Basic understanding of the command line.
9-
10-
Git is a tool that makes sharing code and collaborating with other developers really easy. It also keeps our code tracked and safe. The following examples will help you understand how to use this tool on a daily basis.
11-
12-
## Before you begin
13-
Install command line Git for your operating system ([OS X](https://sourceforge.net/projects/git-osx-installer/), [Windows](http://msysgit.github.io/) or [Linux](https://git-scm.com/download/linux)) and open your terminal / command prompt.
14-
15-
Create a directory where you will be storing all your projects. You can call it `code` or `projects`.
16-
17-
### Setup your Git details
18-
19-
```bash
20-
$ git config --global user.name "Your Name"
21-
$ git config --global user.email "name@domain"
22-
```
6+
**PREREQUISITES:**
7+
- Basic understanding of the system command line ([Introduction to the Command Line](../../command-line/introduction/tutorial.html))
8+
- Git installed and GitHub account set-up ([Get set-up with Git and GitHub](../set-up/tutorial.html).)
239

24-
### Setup an SSH key
25-
26-
An SSH key is used to identify trusted computers, without entering a password.
27-
[Instructions on how to generate an SSH key](https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key)
10+
## Introduction to the Git command line
2811

12+
The following set of examples will help you understand how to use Git via the command line, while carrying out common day-to-day tasks.
2913

3014
## Example 1: Everyday commands
3115

3216

3317
### Create and add your project in Git
18+
To start, let's make a directory to store our files and initialise the directory as a Git repository on our local system.
3419

3520
```bash
3621
$ mkdir practising-git
3722
$ cd practising-git
3823
$ git init
3924
```
25+
> The first command above will make a new directory (`mkdir`) at the system location you're at within the terminal window. The second command will change directory (`cd`) into the new one you just made. The third command will call the Git program then initialise (`init`) your directory as a local Git repository.
4026
41-
### Create a file
27+
### Create a file.
28+
We can create a new file in our local directory and add a line to it at the same time, with the below.
4229

4330
```bash
4431
$ echo "<h1>Learning git</h1>" > index.html
4532
```
4633

4734
> The above command will output `<h1>Learning Git</h1>` and store it in `index.html`. Open up the file and have a look.
4835
49-
### Check the Git repository status.
36+
### Check the Git repository status
37+
To check the state of our working directory and the Staging area (where we hold files until we commit them), we use the following command.
5038

5139
```bash
5240
$ git status
5341
```
5442

55-
> The above command will tell you which files in the current directory have been changed, which files have not yet been added to the Git repository and so on.
43+
> The above command will tell you which files in the current directory have been changed and stqged, which haven't and which files aren't being tracked by Git.
5644
57-
### Add your file on the repository and commit your changes.
45+
### Add your file to the local repository and commit your changes
46+
When we create new files or change existing ones, we 'add' them to an index of items to be committed to the local repository. The 'commit' command saves our changes into the local repository.
5847

5948
```bash
6049
$ git add .
6150
$ git status
6251
$ git commit -m 'this is my first command-line commit!'
6352
```
6453

65-
> `.` will add all the files in the current directory and subdirectories. You should only use it when initialising your repository. Rest of the time you can specify the file names to be added.
54+
> `.` will add **all** the files in the current directory and subdirectories. You should only use it when initialising your repository. The rest of the time you should specify the individual file names to be added.
6655
67-
### Check the Git commit history.
56+
### Check the Git commit history
57+
It's easy to check a list of commits in a repository by using the `log` command.
6858

6959
```bash
7060
$ git log
7161
```
7262

73-
### Transferring project repository to an online service
63+
> Don't forget, you can hit `q` to quit out of a log if it's too long.
7464
75-
First you need to create an account to the service of your choice ([GitHub](https://github.com/join), [GitLab](https://gitlab.com/)). Then, create a new project (or repository).
65+
### Transferring files from our local project repository to an online service
7666

77-
Copy the information about adding an existing project to the repository which should be something like the details below.
67+
Before we can add files to a remote repository, we need to have created an account with a service that is hosting taht repository. If you've not done that yet, head over to our tutorial: Get set-up with [Git and GitHub](../set-up/tutorial.html).
7868

69+
If you've already done that, then great - simply run the commands below, adding in the correct remote repository URL, such as `https://github.com/codebar/tutorials.git`.
7970

8071
```bash
8172
$ git remote add origin <repository-url>
8273
$ git push -u origin master
8374
```
8475

85-
#### What is `remote`
76+
> This is worth repeating: We first add the location of a remote repository, in our case the remote repo is on Github and we've callled it 'origin' as it's the original repository we cloned down to our system. Then we 'push' our changes to the origin's 'master' branch. When there, we can raise a new 'pull Request' (PR) to get the changes 'merged' into the live code. Check out 'Get set-up with [Git and GitHub]'(../set-up/tutorial.html) tutorial for full details around this.
77+
78+
#### What is `remote`?
8679

87-
`remote` is simply the URL of your repository in any online repository hosting services. The `git remote` lists all the remote repositories you have configured. You could have the same repository stored in many places like GitHub and GitLab or Heroku and in such cases you will have a remote configured for each of the remote repository you have.
80+
`remote` is simply the URL of your repository in any online repository hosting service, such as GitHub. The command `git remote` lists all the remote repositories you have configured. You could have the same repository stored in many places like GitHub and GitLab or Heroku and in such cases you will add a remote repository reference to you local machine, as we did above, for each of the remote repositories you have.
8881

8982
The structure of the command to add a new `remote` is `git remote <add|remove> <name of remote> <url of remote>`.
9083

@@ -158,7 +151,7 @@ $ git push origin master
158151
$ git log
159152
```
160153

161-
### Check your code online (from the GitHub or GitLab website).
154+
### Check your code online (from the GitHub or GitLab website)
162155

163156

164157
# Example 3: Verifying changes before any commit
@@ -276,7 +269,7 @@ $ git commit -m 'Oops, I just deleted my list'
276269

277270
> Can you explain the commands you just used?
278271
279-
### Check the log history.
272+
### Check the log history
280273

281274
```bash
282275
$ git log
@@ -298,7 +291,7 @@ commit c0bb15bf9f75613930c66760b90b2ccc1af0d2d6
298291
...
299292
```
300293

301-
### Resetting the last commit.
294+
### Resetting the last commit
302295

303296
```bash
304297
$ git reset HEAD^
@@ -327,7 +320,7 @@ $ git status
327320
> Do you remember how to discard the changes? Have a look earlier in the tutorial.
328321
329322

330-
# Example 6: Revert committed and pushed changes.
323+
# Example 6: Revert committed and pushed changes
331324

332325
You can correct something you pushed accidentally by changing history. In the following example you will see how can you revert the last pushed commit.
333326

@@ -375,89 +368,137 @@ commit c0bb15bf9f75613930c66760b90b2ccc1af0d2d6
375368
$ git revert f4d8d2c2ca851c73176641109172780487da9c1d
376369
```
377370

378-
After reverting the changes you have to push the code to the remote repo to apply them
371+
After reverting the changes, you have to push the code to the remote repo to apply them
379372

380373
```bash
381-
git push origin master
374+
$ git push origin master
382375
```
383376

384377
# Extras
385378

386-
Following are some good resources to to help you set up Git.
387-
https://help.github.com/articles/set-up-git/
388379

389-
## Configuring your Git environment
380+
### Creating a Git Config file
381+
382+
Create a file called `.gitconfig` in the root directory (parent folder) of your local Git repo by typing
383+
`git touch .gitconfig` in the terminal window. Now practise adding the following configuration items.
390384

391-
Create the file `.gitconfig` in your root directory and add the following configuration
385+
### User name and email
386+
If you didn't add your name and email address in the Set-up tutorial, add them now. These are added to each Git commit so it's clear who made the commit to the repo.
392387

393388
```bash
389+
$ git config --global user.name "Your Name"
390+
$ git config --global user.email "name@domain"
391+
```
392+
393+
If you want to just edit the `.gitconfig` file directly then add the following:
394+
395+
```
394396
[user]
395-
name = <Your name>
396-
email = <Your email>
397+
name = "Your Name"
398+
email = "Your email address"
397399
```
398400

399401
### Creating shortcuts (aliases)
402+
In order to save time and key strokes when working on the command line in a terminal window, you can create aliases for your most commonly used Git commands. Here are some examples.
403+
404+
```bash
405+
$ git config --global alias.cm 'commit -m'
406+
$ git config --global alias.co checkout
407+
$ git config --global alias.st status
408+
````
409+
410+
To use them just type `git cm "Message here"` or `git st` for example. Simply put, use the alias in place of the full text Git command, then add any switches on the end (such as a commit message) as you would normally. If you want to just edit the `.gitconfig` file directly then you would add the following section:
400411

401412
```
402413
[alias]
403-
ci = commit
404-
dc = diff --cached
414+
cm = commit --message
415+
co = checkout
416+
st = status
405417
```
406418

407419
> Can you think of another command that you would find handy to shorten down?
408420

409421
### Telling Git to try and fix whitespace issues before committing
422+
A common issue when editing files, especially if the file has been worked on using both Mac and Windows systems, is having a) a trailing whitespace at the end of a line of code, b) a line that is entirely whitespace or c) having a leading whitespace before a tab. When performing a commit where a file has these, Git will prompt you about how (and if) you'd like to fix them. We can tell Git to always fix whitespace issues by adding the following to our `.gitconfig` file:
423+
424+
```bash
425+
$ git config --global apply.whitespace fix
426+
```
427+
428+
If you prefer to just edit the `.gitconfig` file directly, then you would add the following section:
410429
411430
```
412431
[apply]
413432
whitespace = fix
414433
```
415434
416-
### Ignoring files across directories
435+
### Ignoring files across directories (.gitignore)
436+
Very often you have certain types of files or directories in the folder where your code and other assets are. When we commit our code to the Git repository every file will be commited and added to the repository. If you want to exclude certain files on your system from being included in the Git repository, we can add a `.gitignore` file to do this. Make sure this file is included in your repo, to share the ignore rules with others.
437+
438+
Firstly, look for the `.gitignore` file in the root of the directory you're working in. If you have one, skip to the next step, if not let's make one now.
417439
440+
```bash
441+
$ git touch .gitignore
442+
```
443+
444+
To make a reference between the `.gitconfig` file we were working with and the `.gitignore` file we just made, execute the following command:
445+
```bash
446+
$ git config --global core.excludesfile ~/.gitignore
447+
```
448+
449+
If you prefer, to just edit the `.gitignore` file directly, then you would add the following section:
418450
```
419451
[core]
420452
excludesfile = ~/.gitignore
421453
```
422454
423-
To apply this you need to create a .gitignore file in your root path. There you can add either specific files or extensions that you always want excluded. This is a handy list to help you start
455+
Within the `.gitignore` file you can add specific files or extensions that you always want excluded. Here's some common examples that you may want to exclude.
424456

425457
```
426-
*.DS_Store
427-
*~
428-
*.log
429-
*.zip
430-
*.pkg
431-
*.rar
458+
*.DS_Store
459+
*~
460+
*.log
461+
*.zip
462+
*.pkg
463+
*.rar
432464
```
433465

434466
> Do you know what these files are? You normally wouldn't want to commit logs or packages.
467+
> For a more complete list of files you may want to exclude have a look at [https://gist.github.com/octocat/9257657](check out the list from Octocat).
468+
469+
470+
### Prettify your log history
471+
It's possible to make your Git logs easier to read by changing the colour and style of them.
435472

473+
On the command line, run the following:
436474

437-
### Pimping your log history
475+
```bash
476+
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
477+
```
438478

439-
In your aliases add this as an alias for viewing Git logs
479+
As previously, if you prefer to edit the `.gitconfig` file directly, add the following as an alias for viewing Git logs
440480
```
441481
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
442482
```
443483

444-
Try it out by running `git lg`
484+
Try it out by running `git lg` in a terminal window. If the log is very long, you can quit out of the log by hitting `q` on the keyboard.
485+
445486

487+
## Bonus
446488
### Store commands in your history
447489

448-
Add HISTSIZE and HISTFILESIZE to your .bashrc file. **HISTSIZE** is the number of commands stored in memory when you are using the terminal. **HISTFILESIZE** is the number of commands stored in memory when you are using the terminal
490+
On the commandline you can use the up and down arrows to cycle back over the Git commands you've typed before. This can save time when retrying certain commands or allow you to fix them without retyping the whole command. We can add Add HISTSIZE and HISTFILESIZE to your `.bashrc` file to make sure plenty of commands are stored beyond the default 500.
491+
- **HISTSIZE** is the number of commands stored in memory when you are using the terminal.
492+
- **HISTFILESIZE** is the number of commands stored in memory for future sessions.
449493
450494
```bash
451495
HISTSIZE=10000
452496
HISTFILESIZE=20000
453497
```
454498
455-
After typing a couple of command in the terminal, try executing
456-
457-
Ctrl+R followed by the command you want to run e.g. `git lg`
458-
499+
After typing a couple of commands in the terminal to generate some history, try executing `Ctrl`+`R` followed by the command you want to run e.g. `git lg` or just use the arrows on the keyboard to cycle back through them.
459500
460501
> You can see the entire history by running `history`
461502
462503
---
463-
This ends **Git: Introduction to command line** tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial [send us an email](mailto:[email protected]) and let us know.
504+
This ends **Introduction to the Git command line** tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial [send us an email](mailto:[email protected]) and let us know.

0 commit comments

Comments
 (0)