0
0

More than 1 year has passed since last update.

既存プロジェクトをGitで管理するコマンド

Last updated at Posted at 2022-11-04

前述:

プロジェクトをGitで管理するため、二つの方法があります

  • 既存のプロジェクトをgitで初期化してから管理すること。
  • リポジトリの中に既存プロジェクトをコピペすること。

2番目の方法が簡単なので、今回は紹介しないことにします。

実行環境

  • Windows 10
  • Git

手順

1. ユーザー名とユーザーメールアドレスを設定、初期ブランチ設定

まずはユーザ名とユーザーメールが設定済みかどうかを確認する

$ git config --global user.name
$ git config --global user.email

設定されなかった場合は下記コマンドを実行して設定する

$ git config --global user.name "username"
$ git config --global user.email "useremail"

2020年10月よりGitHubではプロジェクト作成時点でのデフォルトのブランチ名が master から main に変更されましたので、下記コマンドを実行して、初期ブランチを設定する

$ git config --global init.defaultBranch main

2. 既存のプロジェクトフォルダに移動して、Gitコマンドで初期化する

$ cd /project-path
$ git init

.gitフォルダが生成される
1.png

3. ファイル追加

新たに追加/変更/削除したファイルをインデックスに追加する

$ git add .

statusコマンドで確認し、対象ファイルが追加された

$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   xxx

4. コミット

$ git commit -m 'first commit'

logコマンドで確認し、コミット記録が確認できる

$ git log
commit xxx (HEAD -> main)
Author: xxx
Date:   Wed Nov 2 16:01:44 2022 +0900

    first commit

5. リモートのリポジトリと紐づく

こちらはSSHで紐づく

$ git remote add origin git@github.com:xxx/xxx.git

リモートのリポジトリ紐づいたかどうか確認する

$  git remote -v

origin  git@github.com:xxx/xxx.git (fetch)
origin  git@github.com:xxx/xxx.git (push)

6. SSHKeyが存在しないため、新たなSSHKeyを生成して、GitHubアカウントと紐づく

$  git pull --rebase origin main

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
$ ssh-keygen -t ed25519 -C "your mail address"

Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_ed25519
Your public key has been saved in /c/Users/Administrator/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxx
The key's randomart image is:
+--[ED25519 256]--+
|o. +...*..o+o.   |
|o B o *o+.. . .  |
| o =  .=o= . o   |
|. .   .oo.o .    |
|.o .  o S        |
|+ .    . o       |
|.+        .      |
|B*+              |
|XE.              |
+----[SHA256]-----+

1.png

7. リモートとローカルをマージする

$  git pull --rebase origin main
The authenticity of host 'github.com (20.27.177.113)' can't be established.
ED25519 key fingerprint is xxx.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 593 bytes | 5.00 KiB/s, done.
From github.com:xxx/xxx
 * branch            main       -> FETCH_HEAD
 * [new branch]      main       -> origin/main
Successfully rebased and updated refs/heads/main.

8. リモートへプッシュ

mainブランチにpushする

$  git push -u origin main
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0