I tried the initial setting of GitHub [For Mac user]
Tags: github, git, beginners
Published: 2023-01-29 09:41:20

I plan to write a bunch of blogs this year. I would like to use GitHub to manage my blogs, so this time around I tried the initial setup of GitHub to get started.
# Table Of Contents
* [1. Create an account](#chapter-1)
* [2. Create a repository](#chapter-2)
* [3. Basic commands for using GitHub](#chapter-3)

## 1. Create an account <a name="chapter-1"></a>
Click Sign up on [the GitHub official page.](https://github.com/)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zog8m59d5zkbyefbrso0.png)
You will be asked to enter your email address, password and username after clicking on the page.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5th8b0413dpd9dpnhna.png)
Then you need to do puzzle. You will receive an email with a passcode for authentication.
If the password is OK, you will be taken to the GitHub home page.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6jhn6b438vax27ir5qe.png)
## 2. Create a repository <a name="chapter-2"></a>
Click the plus button in the upper right corner to create a new repository.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7vyj0qjenm62l0dmvix.png)
After clicking it, you have to fill in the name of the repository and other information.
README is like a description of a project, you may add it later.
.gitignore file is a list of files you don't like to be managed on GitHub.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkkdvry4rvub07qc5yz4.png)
After filling in the information, click the Create a repository button. You will be taken to a next page.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kx1b2v1u8ju570pcjcd.png)
## 3. Basic commands for using GitHub <a name="chapter-3"></a>
OK, let's try a couple of basic commands to make use of the repository we have just created.
Copy an URL of the repository by clicking the code button in the upper right corner. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag90hjzqbu7r2z7s1u7l.png)
Then open a terminal on your PC and run the following command.
You will get a copy of a remote repository (on GitHub) into your local repository.
```
% git clone [URL of the repository]
```
You will get comments like the following:
This is a public repository, so I can show my URL.
```
% git clone https://github.com/atsushi-ambo/test-github
Cloning into 'test-github'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
```
### Initialize the repository
Run the following command to initialize your repository.
```
% git init
Reinitialized existing Git repository in /Users/username/test-github/.git/
```
### Add a new file to your local repository
First, create a new file in the local repository by the following command.
```
% echo “Hello World” > GitHubtest.txt
% ls
GitHubtest.txt  README.md
```
Then, add the file to the local repository with the following command.
Actually, the "add" command does not add to a local repository, it adds to an index before the local repository.
```
% git add GitHubtest.txt
```
### Commit the file to the local repository
Having added the file to the index, you can now commit it to the local repository using the following "commit" command. You will need to add a message such as "Add a file".
```
git commit -m "[your message]"
```
I got the following result:
```
% git commit -m "add a file"
[main aa3f19e] adda file
 Committer: Atsushi Ambo <username-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 GitHubtest.txt
```
### Push the change to the remote repository
Now, you have committed the change (file) to your local repository. Next, add it the remote repository by the following command. You have only a main branch, so branch name is "main."
```
git push origin [branch name]
```
However, I received the following error.
```
% git push origin main          
Missing or invalid credentials.
Error: connect ENOENT /var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'connect',
  address: '/var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock'
}
Missing or invalid credentials.
Error: connect ENOENT /var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'connect',
  address: '/var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock'
}
remote: No anonymous write access.
fatal: Authentication failed for 'https://github.com/atsushi-ambo/test-github/'
```
I did some research on Google and found out that I have to set an access token for the repository.
### Setup personal access token
So, let's setup the token.
Go to the Settings tab and click on Developer settings at the bottom.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xwj7zc08jlopgu59amb.png)
Select Fine-grained tokens in Personal access tokens tab.
GitHub recommends fine-grained tokens over tokens (classic).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y74ovwoh9a505xzj1dyh.png)
Fill in required fields such as Token Name.
Select the Only select repositories check box so that you can push to the remote repository this time.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7xkybbxhb68slxuebox.png)
Then, click the create button.
Copy the created token, you only have one time to copy it. Don't lose it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9vkswaton4w8y0iu2kj.png)
To setup the token, you will need to install GitHub CLI.
```
% brew install gh
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 2 taps (hashicorp/tap and homebrew/core).
==> New Formulae
ancient                      clang-build-analyzer         hashicorp/tap/copywrite      m1ddc                        mdless                       xcdiff                       zsh-autopair

You have 9 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Fetching gh
==> Downloading https://ghcr.io/v2/homebrew/core/gh/manifests/2.22.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/gh/blobs/sha256:e704f932cbc7b237e9143e9db3a9a32f0acd753e03d302299b69d660d3875011
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:e704f932cbc7b237e9143e9db3a9a32f0acd753e03d302299b69d660d3875011?se=2023-01-28T10%3A25%3A00Z&sig=u57FLpso8S3mj2ErguwRz
######################################################################## 100.0%
==> Pouring gh--2.22.1.arm64_ventura.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/gh/2.22.1: 153 files, 40.3MB
==> Running `brew cleanup gh`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```
Run the following command to setup the token.
You will be asked some questions, answer them to continue.
```
% gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser
...
✓ Authentication complete.
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as atsushi-ambo
```
If you receive an authentication complete message, you have completed the setup.
So, now let's try "push" command again.
```
% git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/atsushi-ambo/test-github
   e30a2c6..7253ffa  main -> main
```
You have completed the commit to the remote repository on GitHub.
You can see the test file uploaded to the repository on GitHub.
My blog is finished at this point. There are some other git commands like "git branch" and "git checkout". I will try to use them next time.