DEV Community

atsushi-ambo
atsushi-ambo

Posted on

I tried the initial setting of GitHub [For Mac user]

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

Click Sign up on the GitHub official page.
Image description
You will be asked to enter your email address, password and username after clicking on the page.
Image description
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

2. Create a repository

Click the plus button in the upper right corner to create a new repository.
Image description
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
After filling in the information, click the Create a repository button. You will be taken to a next page.
Image description

3. Basic commands for using GitHub

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
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]
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Initialize the repository

Run the following command to initialize your repository.

% git init
Reinitialized existing Git repository in /Users/username/test-github/.git/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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/'
Enter fullscreen mode Exit fullscreen mode

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
Select Fine-grained tokens in Personal access tokens tab.
GitHub recommends fine-grained tokens over tokens (classic).
Image description
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
Then, click the create button.
Copy the created token, you only have one time to copy it. Don't lose it.
Image description
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`).
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)