|
| 1 | +--- |
| 2 | +title: Setting Up Git |
| 3 | +teaching: 5 |
| 4 | +exercises: 0 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Configure `git` the first time it is used on a computer. |
| 10 | +- Understand the meaning of the `--global` configuration flag. |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 13 | + |
| 14 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 15 | + |
| 16 | +- How do I get set up to use Git? |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +When we use Git on a new computer for the first time, |
| 21 | +we need to configure a few things. Below are a few examples |
| 22 | +of configurations we will set as we get started with Git: |
| 23 | + |
| 24 | +- our name and email address, |
| 25 | +- what our preferred text editor is, |
| 26 | +- and that we want to use these settings globally (i.e. for every project). |
| 27 | + |
| 28 | +On a command line, Git commands are written as `git verb options`, |
| 29 | +where `verb` is what we actually want to do and `options` is additional optional information which may be needed for the `verb`. So here is how |
| 30 | +Alfredo sets up his new laptop: |
| 31 | + |
| 32 | +```bash |
| 33 | +$ git config --global user.name "Alfredo Linguini" |
| 34 | +$ git config --global user.email "[email protected]" |
| 35 | +``` |
| 36 | + |
| 37 | +Please use your own name and email address instead of Alfredo's. This user name and email will be associated with your subsequent Git activity, |
| 38 | +which means that any changes pushed to |
| 39 | +[GitHub](https://github.com/), |
| 40 | +[BitBucket](https://bitbucket.org/), |
| 41 | +[GitLab](https://gitlab.com/) or |
| 42 | +another Git host server |
| 43 | +after this lesson will include this information. |
| 44 | + |
| 45 | +For this lesson, we will be interacting with [GitHub](https://github.com/) and so the email address used should be the same as the one used when setting up your GitHub account. If you are concerned about privacy, please review [GitHub's instructions for keeping your email address private][git-privacy]. |
| 46 | + |
| 47 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 48 | + |
| 49 | +## Keeping your email private |
| 50 | + |
| 51 | +If you elect to use a private email address with GitHub, then use GitHub's no-reply email address for the `user.email` value. It looks like `[email protected]`. You can look up your own address in your GitHub [email settings ](https://github.com/settings/emails). |
| 52 | + |
| 53 | + |
| 54 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 55 | + |
| 56 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 57 | + |
| 58 | +## Line Endings |
| 59 | + |
| 60 | +As with other keys, when you press <kbd>Enter</kbd> or <kbd>↵</kbd> or on Macs, <kbd>Return</kbd> on your keyboard, |
| 61 | +your computer encodes this input as a character. |
| 62 | +Different operating systems use different character(s) to represent the end of a line. |
| 63 | +(You may also hear these referred to as newlines or line breaks.) |
| 64 | +Because Git uses these characters to compare files, |
| 65 | +it may cause unexpected issues when editing a file on different machines. |
| 66 | +Though it is beyond the scope of this lesson, you can read more about this issue |
| 67 | +[in the Pro Git book](https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_core_autocrlf). |
| 68 | + |
| 69 | +You can change the way Git recognizes and encodes line endings |
| 70 | +using the `core.autocrlf` command to `git config`. |
| 71 | +The following settings are recommended: |
| 72 | + |
| 73 | +On macOS and Linux: |
| 74 | + |
| 75 | +```bash |
| 76 | +$ git config --global core.autocrlf input |
| 77 | +``` |
| 78 | + |
| 79 | +And on Windows: |
| 80 | + |
| 81 | +```bash |
| 82 | +$ git config --global core.autocrlf true |
| 83 | +``` |
| 84 | + |
| 85 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 86 | + |
| 87 | +Alfredo also has to set his favorite text editor, following this table: |
| 88 | + |
| 89 | +| Editor | Configuration command | |
| 90 | +| :----------- | :------------------------------ | |
| 91 | +| Atom | `$ git config --global core.editor "atom --wait"` | |
| 92 | +| nano | `$ git config --global core.editor "nano -w"` | |
| 93 | +| BBEdit (Mac, with command line tools) | `$ git config --global core.editor "bbedit -w"` | |
| 94 | +| Sublime Text (Mac) | `$ git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w"` | |
| 95 | +| Sublime Text (Win, 32-bit install) | `$ git config --global core.editor "'c:/program files (x86)/sublime text 3/sublime_text.exe' -w"` | |
| 96 | +| Sublime Text (Win, 64-bit install) | `$ git config --global core.editor "'c:/program files/sublime text 3/sublime_text.exe' -w"` | |
| 97 | +| Notepad (Win) | `$ git config --global core.editor "c:/Windows/System32/notepad.exe"` | |
| 98 | +| Notepad++ (Win, 32-bit install) | `$ git config --global core.editor "'c:/program files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"` | |
| 99 | +| Notepad++ (Win, 64-bit install) | `$ git config --global core.editor "'c:/program files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"` | |
| 100 | +| Kate (Linux) | `$ git config --global core.editor "kate"` | |
| 101 | +| Gedit (Linux) | `$ git config --global core.editor "gedit --wait --new-window"` | |
| 102 | +| Scratch (Linux) | `$ git config --global core.editor "scratch-text-editor"` | |
| 103 | +| Emacs | `$ git config --global core.editor "emacs"` | |
| 104 | +| Vim | `$ git config --global core.editor "vim"` | |
| 105 | +| VS Code | `$ git config --global core.editor "code --wait"` | |
| 106 | + |
| 107 | +It is possible to reconfigure the text editor for Git whenever you want to change it. |
| 108 | + |
| 109 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 110 | + |
| 111 | +## Exiting Vim |
| 112 | + |
| 113 | +Note that Vim is the default editor for many programs. If you haven't used Vim before and wish to exit a session without saving |
| 114 | +your changes, press <kbd>Esc</kbd> then type `:q!` and press <kbd>Enter</kbd> or <kbd>↵</kbd> or on Macs, <kbd>Return</kbd>. |
| 115 | +If you want to save your changes and quit, press <kbd>Esc</kbd> then type `:wq` and press <kbd>Enter</kbd> or <kbd>↵</kbd> or on Macs, <kbd>Return</kbd>. |
| 116 | + |
| 117 | + |
| 118 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 119 | + |
| 120 | +Git (2.28+) allows configuration of the name of the branch created when you |
| 121 | +initialize any new repository. Alfredo decides to use that feature to set it to `main` so |
| 122 | +it matches the cloud service he will eventually use. |
| 123 | + |
| 124 | +```bash |
| 125 | +$ git config --global init.defaultBranch main |
| 126 | +``` |
| 127 | + |
| 128 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 129 | + |
| 130 | +## Default Git branch naming |
| 131 | + |
| 132 | +Source file changes are associated with a "branch." |
| 133 | +For new learners in this lesson, it's enough to know that branches exist, and this lesson uses one branch. |
| 134 | +By default, Git will create a branch called `master` |
| 135 | +when you create a new repository with `git init` (as explained in the next Episode). This term evokes |
| 136 | +the racist practice of human slavery and the |
| 137 | +[software development community](https://github.com/github/renaming) has moved to adopt |
| 138 | +more inclusive language. |
| 139 | + |
| 140 | +In 2020, most Git code hosting services transitioned to using `main` as the default |
| 141 | +branch. As an example, any new repository that is opened in GitHub and GitLab default |
| 142 | +to `main`. However, Git has not yet made the same change. As a result, local repositories |
| 143 | +must be manually configured have the same main branch name as most cloud services. |
| 144 | + |
| 145 | +For versions of Git prior to 2.28, the change can be made on an individual repository level. The |
| 146 | +command for this is in the next episode. Note that if this value is unset in your local Git |
| 147 | +configuration, the `init.defaultBranch` value defaults to `master`. |
| 148 | + |
| 149 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 150 | + |
| 151 | +The five commands we just ran above only need to be run once: the flag `--global` tells Git |
| 152 | +to use the settings for every project, in your user account, on this computer. |
| 153 | + |
| 154 | +Let's review those settings and test our `core.editor` right away: |
| 155 | + |
| 156 | +```bash |
| 157 | +$ git config --global --edit |
| 158 | +``` |
| 159 | + |
| 160 | +Let's close the file without making any additional changes. Remember, since typos in the config file will cause |
| 161 | +issues, it's safer to view the configuration with: |
| 162 | + |
| 163 | +```bash |
| 164 | +$ git config --list |
| 165 | +``` |
| 166 | + |
| 167 | +And if necessary, change your configuration using the |
| 168 | +same commands to choose another editor or update your email address. |
| 169 | +This can be done as many times as you want. |
| 170 | + |
| 171 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 172 | + |
| 173 | +## Proxy |
| 174 | + |
| 175 | +In some networks you need to use a |
| 176 | +[proxy](https://en.wikipedia.org/wiki/Proxy_server). If this is the case, you |
| 177 | +may also need to tell Git about the proxy: |
| 178 | + |
| 179 | +```bash |
| 180 | +$ git config --global http.proxy proxy-url |
| 181 | +$ git config --global https.proxy proxy-url |
| 182 | +``` |
| 183 | + |
| 184 | +To disable the proxy, use |
| 185 | + |
| 186 | +```bash |
| 187 | +$ git config --global --unset http.proxy |
| 188 | +$ git config --global --unset https.proxy |
| 189 | +``` |
| 190 | + |
| 191 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 192 | + |
| 193 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 194 | + |
| 195 | +## Git Help and Manual |
| 196 | + |
| 197 | +Always remember that if you forget the subcommands or options of a `git` command, you can access the |
| 198 | +relevant list of options typing `git <command> -h` or access the corresponding Git manual by typing |
| 199 | +`git <command> --help`, e.g.: |
| 200 | + |
| 201 | +```bash |
| 202 | +$ git config -h |
| 203 | +$ git config --help |
| 204 | +``` |
| 205 | + |
| 206 | +While viewing the manual, remember the `:` is a prompt waiting for commands and you can press <kbd>Q</kbd> to exit the manual. |
| 207 | + |
| 208 | +More generally, you can get the list of available `git` commands and further resources of the Git manual typing: |
| 209 | + |
| 210 | +```bash |
| 211 | +$ git help |
| 212 | +``` |
| 213 | + |
| 214 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 215 | + |
| 216 | +[git-privacy]: https://help.github.com/articles/keeping-your-email-address-private/ |
| 217 | + |
| 218 | + |
| 219 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 220 | + |
| 221 | +- Use `git config` with the `--global` option to configure a user name, email address, editor, and other preferences once per machine. |
| 222 | + |
| 223 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 224 | + |
| 225 | + |
0 commit comments