File size: 7,326 Bytes
292d92c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Install and Authenticate GitHub CLI (gh)

You are helping the user install and authenticate the GitHub CLI tool.

## Your tasks:

1. **Check if gh is already installed:**
   ```bash
   which gh
   gh --version
   ```

   If already installed and authenticated:
   ```bash
   gh auth status
   ```

2. **Install GitHub CLI (if not installed):**

   **Method 1: Using official repository (recommended):**
   ```bash
   # Add the GPG key
   sudo mkdir -p -m 755 /etc/apt/keyrings
   wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
   sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg

   # Add the repository
   echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null

   # Install
   sudo apt update
   sudo apt install gh
   ```

   **Method 2: Using snap:**
   ```bash
   sudo snap install gh
   ```

   **Method 3: Using Homebrew (if installed):**
   ```bash
   brew install gh
   ```

3. **Verify installation:**
   ```bash
   gh --version
   which gh
   ```

4. **Authenticate with GitHub:**

   **Interactive authentication (recommended):**
   ```bash
   gh auth login
   ```

   This will prompt for:
   - GitHub.com or GitHub Enterprise Server
   - Preferred protocol (HTTPS or SSH)
   - Authentication method (web browser or token)

   **Via web browser (easiest):**
   - Select "Login with a web browser"
   - Follow the one-time code and URL
   - Authorize in browser

   **Via token:**
   - Generate a token at https://github.com/settings/tokens
   - Select "Login with authentication token"
   - Paste the token

5. **Verify authentication:**
   ```bash
   gh auth status
   ```

   Should show:
   - Logged in to github.com
   - Account name
   - Token scopes

6. **Configure gh settings:**

   **Set default editor:**
   ```bash
   gh config set editor vim
   # or
   gh config set editor nano
   # or
   gh config set editor code  # VS Code
   ```

   **Set default protocol:**
   ```bash
   gh config set git_protocol ssh
   # or
   gh config set git_protocol https
   ```

   **View all config:**
   ```bash
   gh config list
   ```

7. **Set up SSH key (if using SSH protocol):**
   ```bash
   # Generate SSH key if needed
   ssh-keygen -t ed25519 -C "your_email@example.com"

   # Add to ssh-agent
   eval "$(ssh-agent -s)"
   ssh-add ~/.ssh/id_ed25519

   # Add to GitHub
   gh ssh-key add ~/.ssh/id_ed25519.pub --title "My Ubuntu Desktop"

   # Or copy public key to GitHub manually
   cat ~/.ssh/id_ed25519.pub
   ```

8. **Test GitHub connectivity:**
   ```bash
   # Test SSH connection
   ssh -T git@github.com

   # Test gh CLI
   gh repo list
   gh auth status
   ```

9. **Configure git to use gh for credentials:**
   ```bash
   gh auth setup-git
   ```

   This configures git to use gh as a credential helper.

10. **Show basic gh commands:**

    **Repository operations:**
    - `gh repo create` - Create a repository
    - `gh repo clone <repo>` - Clone a repository
    - `gh repo view` - View repository details
    - `gh repo list` - List your repositories
    - `gh repo fork` - Fork a repository

    **Pull requests:**
    - `gh pr create` - Create a pull request
    - `gh pr list` - List pull requests
    - `gh pr view <number>` - View a PR
    - `gh pr checkout <number>` - Checkout a PR
    - `gh pr merge <number>` - Merge a PR
    - `gh pr review <number>` - Review a PR

    **Issues:**
    - `gh issue create` - Create an issue
    - `gh issue list` - List issues
    - `gh issue view <number>` - View an issue
    - `gh issue close <number>` - Close an issue

    **Workflows:**
    - `gh workflow list` - List workflows
    - `gh workflow view <workflow>` - View workflow
    - `gh workflow run <workflow>` - Trigger a workflow
    - `gh run list` - List workflow runs
    - `gh run view <run>` - View a run

    **Gists:**
    - `gh gist create <file>` - Create a gist
    - `gh gist list` - List gists
    - `gh gist view <gist>` - View a gist

11. **Set up shell completion:**

    **For bash:**
    ```bash
    gh completion -s bash > ~/.gh-completion.bash
    echo 'source ~/.gh-completion.bash' >> ~/.bashrc
    source ~/.bashrc
    ```

    **For zsh:**
    ```bash
    gh completion -s zsh > ~/.gh-completion.zsh
    echo 'source ~/.gh-completion.zsh' >> ~/.zshrc
    source ~/.zshrc
    ```

12. **Configure multiple accounts (if needed):**
    ```bash
    # Add another account
    GH_HOST=github.com gh auth login

    # Switch between accounts
    gh auth switch
    ```

13. **Set up aliases (optional):**
    ```bash
    gh alias set pv 'pr view'
    gh alias set co 'pr checkout'
    gh alias set bugs 'issue list --label=bug'
    ```

    List aliases:
    ```bash
    gh alias list
    ```

14. **Authenticate with GitHub Enterprise (if applicable):**
    ```bash
    gh auth login --hostname github.example.com
    ```

15. **Troubleshooting common issues:**

    **Permission denied:**
    - Check auth status: `gh auth status`
    - Re-authenticate: `gh auth login`
    - Check token scopes

    **SSH issues:**
    - Verify SSH key: `ssh -T git@github.com`
    - Add SSH key to GitHub: `gh ssh-key add`
    - Check ssh-agent: `ssh-add -l`

    **Rate limiting:**
    - Check rate limit: `gh api rate_limit`
    - Use authentication to increase limits

16. **Update gh:**
    ```bash
    sudo apt update
    sudo apt upgrade gh
    # or
    brew upgrade gh
    # or
    sudo snap refresh gh
    ```

17. **Advanced configuration:**

    **Custom API endpoint:**
    ```bash
    gh config set api_endpoint https://api.github.com
    ```

    **Disable prompts:**
    ```bash
    gh config set prompt disabled
    ```

    **Configure pager:**
    ```bash
    gh config set pager less
    ```

18. **Security best practices:**
    - Use SSH keys instead of HTTPS when possible
    - Use tokens with minimal required scopes
    - Rotate tokens regularly
    - Don't share tokens
    - Use different tokens for different machines
    - Enable 2FA on GitHub account
    - Review authorized applications regularly

19. **Provide workflow examples:**

    **Create a repo and push:**
    ```bash
    mkdir my-project
    cd my-project
    git init
    echo "# My Project" > README.md
    git add README.md
    git commit -m "Initial commit"
    gh repo create my-project --public --source=. --push
    ```

    **Fork and clone:**
    ```bash
    gh repo fork owner/repo --clone
    ```

    **Create PR from current branch:**
    ```bash
    gh pr create --title "My changes" --body "Description of changes"
    ```

20. **Report findings:**
    Summarize:
    - Installation status
    - Authentication status
    - Configured settings
    - Available accounts
    - Next steps

## Important notes:
- gh is the official GitHub CLI
- Requires GitHub account
- Can use HTTPS or SSH protocol
- SSH is generally more secure and convenient
- gh can replace many git operations with simpler syntax
- Shell completion is very helpful
- Keep gh updated for latest features
- Multiple accounts are supported
- Works with both GitHub.com and GitHub Enterprise
- Tokens should have minimal required scopes