|
| 1 | +# Git basics |
| 2 | + |
| 3 | +Make sure that you have `git` installed on your machine, if not, refer to [this](/material/01-installation.md) page. |
| 4 | + |
| 5 | +## Initializing repositories |
| 6 | + |
| 7 | +### What is a Git repository? |
| 8 | + |
| 9 | +A Git repository is a directory containing all of a project's files, and it is managed by Git, where you can see a `.git` directory in **all** Git repositories, this directory allows Git to keep track of each file's revision history, and other Git data like branches and merges. |
| 10 | + |
| 11 | +### Initializing in the same directory (folder) |
| 12 | + |
| 13 | +To initialize Git in an existing project, go to that project's directory and run the command |
| 14 | + |
| 15 | +```bash |
| 16 | +git init |
| 17 | +``` |
| 18 | + |
| 19 | +This will create the `.git` directory allowing Git to keep track of your files' changes. |
| 20 | + |
| 21 | +### Initializing into a new directory (folder) |
| 22 | + |
| 23 | +To a Git managed project, run the following command |
| 24 | + |
| 25 | +```bash |
| 26 | +git init <directory-name> |
| 27 | +``` |
| 28 | + |
| 29 | +This will create a new directory with the specified name with `.git` directory inside of it allowing Git to keep track of your files' changes. |
| 30 | + |
| 31 | +### Cloning an existing repository |
| 32 | + |
| 33 | +Since Git is a distributed version controlling system, repositories may exist on some remote host like GitHub, Gitlab, or Bitbucket. |
| 34 | + |
| 35 | +For example cloning this repository |
| 36 | + |
| 37 | +```bash |
| 38 | +git clone https://github.com/jordanopensource/git-training |
| 39 | +``` |
| 40 | + |
| 41 | +This will create a new directory named `git-training` with the content of the clone repository's files. |
| 42 | + |
| 43 | +### Forking a repository |
| 44 | + |
| 45 | +Forking; and its name gives it away, it's making a fork, like road forks, where a main road is forked side road(s), and evantually it looks like an eating fork, the same concept applied to Git repositories, where there's a main branch (road) on a repository, and you create a fork branch (side road) of it. |
| 46 | + |
| 47 | +All Git hosts support forking a public repository, where you copy the repository into your account or organization. |
| 48 | + |
| 49 | +## Staging and commits |
| 50 | + |
| 51 | +Each file in a Git repository goes throgh the four Git file stages: |
| 52 | + |
| 53 | +- **Untracked**: The file is newly created or moved from another directory to a Git directory, and Git doesn't keep any track of it. |
| 54 | +- **Unmodified**: The file is tracked by Git, i.e has been commited before, and has no new changes. |
| 55 | +- **Modified**: An uncommited file that has been modified but not committed yet. |
| 56 | +- **Staged**: A modified file that has been added to be commited. |
| 57 | + |
| 58 | +Here's a figure showing the relations between the four stages. |
| 59 | + |
| 60 | + |
| 61 | +### Commiting a file |
| 62 | + |
| 63 | +After creating a file (untracked) or editing an existing one (unmodified), you can add the file to the staging area, by running this command. |
| 64 | + |
| 65 | +```bash |
| 66 | +git add <file-name> |
| 67 | +``` |
| 68 | + |
| 69 | +Or using this command to add all of the files in the current directory. |
| 70 | + |
| 71 | +```bash |
| 72 | +git add . |
| 73 | +``` |
| 74 | + |
| 75 | +Now to move the file into the unmodified stage, commit the file with a commit message |
| 76 | + |
| 77 | +> Commit messages should describe what happened to a file, in an imperative way |
| 78 | +> e.g "Create <file-name>" |
| 79 | +
|
| 80 | +```bash |
| 81 | +git commit -m "Create <file-name>" |
| 82 | +``` |
| 83 | + |
| 84 | +Now the file that you have just commited is in the unmodified stage, where if you made a new modification to it it will get moved to the modified stage, and the cycle continues... |
| 85 | + |
| 86 | +### Discarding changes done after a commit |
| 87 | + |
| 88 | +If you modified a file, after a commit, and then realized that the changes weren't needed, you can rollback to the latest tracked version by Git, by running the following command. |
| 89 | + |
| 90 | +```bash |
| 91 | +git checkout -- <file-name> |
| 92 | +``` |
| 93 | + |
| 94 | +## Pushing and Pulling |
| 95 | + |
| 96 | +## Understanding status and logs |
0 commit comments