You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**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).)
23
9
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
28
11
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.
29
13
30
14
## Example 1: Everyday commands
31
15
32
16
33
17
### 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.
34
19
35
20
```bash
36
21
$ mkdir practising-git
37
22
$ cd practising-git
38
23
$ git init
39
24
```
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.
40
26
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.
42
29
43
30
```bash
44
31
$ echo"<h1>Learning git</h1>"> index.html
45
32
```
46
33
47
34
> The above command will output `<h1>Learning Git</h1>` and store it in `index.html`. Open up the file and have a look.
48
35
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.
50
38
51
39
```bash
52
40
$ git status
53
41
```
54
42
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.
56
44
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.
58
47
59
48
```bash
60
49
$ git add .
61
50
$ git status
62
51
$ git commit -m 'this is my first command-line commit!'
63
52
```
64
53
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.
66
55
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.
68
58
69
59
```bash
70
60
$ git log
71
61
```
72
62
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.
74
64
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
76
66
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).
78
68
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`.
79
70
80
71
```bash
81
72
$ git remote add origin <repository-url>
82
73
$ git push -u origin master
83
74
```
84
75
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`?
86
79
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.
88
81
89
82
The structure of the command to add a new `remote` is `git remote <add|remove> <name of remote> <url of remote>`.
90
83
@@ -158,7 +151,7 @@ $ git push origin master
158
151
$ git log
159
152
```
160
153
161
-
### Check your code online (from the GitHub or GitLab website).
154
+
### Check your code online (from the GitHub or GitLab website)
162
155
163
156
164
157
# Example 3: Verifying changes before any commit
@@ -276,7 +269,7 @@ $ git commit -m 'Oops, I just deleted my list'
> Do you remember how to discard the changes? Have a look earlier in the tutorial.
328
321
329
322
330
-
# Example 6: Revert committed and pushed changes.
323
+
# Example 6: Revert committed and pushed changes
331
324
332
325
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.
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
379
372
380
373
```bash
381
-
git push origin master
374
+
$ git push origin master
382
375
```
383
376
384
377
# Extras
385
378
386
-
Following are some good resources to to help you set up Git.
387
-
https://help.github.com/articles/set-up-git/
388
379
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.
390
384
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.
392
387
393
388
```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
+
```
394
396
[user]
395
-
name = <Your name>
396
-
email = <Your email>
397
+
name = "Your Name"
398
+
email = "Your email address"
397
399
```
398
400
399
401
### 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`forexample. Simply put, use the aliasin 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:
400
411
401
412
```
402
413
[alias]
403
-
ci = commit
404
-
dc = diff --cached
414
+
cm = commit --message
415
+
co = checkout
416
+
st = status
405
417
```
406
418
407
419
> Can you think of another command that you would find handy to shorten down?
408
420
409
421
### 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:
410
429
411
430
```
412
431
[apply]
413
432
whitespace = fix
414
433
```
415
434
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.
417
439
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:
If you prefer, to just edit the `.gitignore` file directly, then you would add the following section:
418
450
```
419
451
[core]
420
452
excludesfile = ~/.gitignore
421
453
```
422
454
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.
424
456
425
457
```
426
-
*.DS_Store
427
-
*~
428
-
*.log
429
-
*.zip
430
-
*.pkg
431
-
*.rar
458
+
*.DS_Store
459
+
*~
460
+
*.log
461
+
*.zip
462
+
*.pkg
463
+
*.rar
432
464
```
433
465
434
466
> 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.
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
+
445
486
487
+
## Bonus
446
488
### Store commands in your history
447
489
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.
449
493
450
494
```bash
451
495
HISTSIZE=10000
452
496
HISTFILESIZE=20000
453
497
```
454
498
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.
459
500
460
501
> You can see the entire history by running `history`
461
502
462
503
---
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