LoginSignup
2
6

More than 1 year has passed since last update.

gitの使い方

Last updated at Posted at 2017-01-19

1 環境

VMware Workstation 14 Playerでゲストマシンを作成しました。

OS版数
[root@centos74 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

[root@centos74 ~]# uname -r
3.10.0-693.el7.x86_64
構成
  ゲストマシン------ Internet --------- GitHub

2 事前準備

2.1 GitHubでアカウント作成

GitHubでアカウントを作成する。

(1) GitHubホームページにアクセスする
(2) step1:ユーザ名,メールアドレス,パスワードを入力したあと、「create an account」をクリックする。
(3) step2:無料の「フリープラン」を選択する。
git1.png

2.2 gitのインストール

[root@centos74 ~]# yum -y install git
[root@centos74 ~]# git --version
git version 1.8.3.1

4 リポジトリの作成方法

4.1 GitHubの設定

リポジトリの名前、説明を入力して、「Create repository」をクリックする。
github1.png

4.2 ローカル(ゲストマシン)の設定

config実行
[root@centos74 git]# git config --global user.name  "hana-shin"
[root@centos74 git]# git config --global user.email "xxx@example.com"
init実行
[root@centos74 git]# git init
Reinitialized existing Git repository in /root/git/.git/
add実行
[root@centos74 git]# echo "# test1" >> README.md
[root@centos74 git]# git add README.md
commit実行
[root@centos74 git]# git commit -m "first commit"
[master (root-commit) e496a22] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
remote実行
[root@centos74 git]# git remote add origin https://github.com/hana-shin/test1.git
push実行
[root@centos74 git]# git push -u origin master
Username for 'https://github.com': hana-shin
Password for 'https://hana-shin@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/hana-shin/test1.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

5 リポジトリにファイルをアップロードする方法

ファイルの作成
[root@centos74 git]# touch test.txt
[root@centos74 git]# echo "12345" > test.txt
[root@centos74 git]# cat test.txt
12345
add実行
[root@centos74 git]# git add test.txt
commit実行
[root@centos74 git]# git commit -m "first new file"
[master 3f97dc1] first new file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
push実行
[root@centos74 git]# git push origin master
Username for 'https://github.com': hana-shin
Password for 'https://hana-shin@github.com':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/hana-shin/test1.git
   e496a22..3f97dc1  master -> master

6 GitHubのファイルを削除する方法

ローカルファイルの削除
[root@centos74 git]# ls
README.md  test.txt
[root@centos74 git]# git rm test.txt
rm 'test.txt'
[root@centos74 git]# ls
README.md
commit実行
[root@centos74 git]# git add .
[root@centos74 git]# git commit -m "delete test.txt"
[master 4c94841] delete test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
push実行
[root@centos74 git]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'https://github.com': hana-shin
Password for 'https://hana-shin@github.com':
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 236 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/hana-shin/test1.git
   3f97dc1..4c94841  master -> master

7 リモートリポジトリをローカルリポジトリにcloneする方法(clone)

clone実行
[root@centos74 ~]# mkdir project
[root@centos74 ~]# cd project/

[root@centos74 project]# git clone https://github.com/hana-shin/test1
Cloning into 'test1'...
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (5/5), done.
Unpacking objects: 100% (8/8), done.
remote: Total 8 (delta 2), reused 6 (delta 0), pack-reused 0
cloneしたファイルの確認
[root@centos74 project]# ls
test1
[root@centos74 project]# cd test1/
[root@centos74 test1]# ls
README.md  test.txt
[root@centos74 test1]# cat test.txt
12345

8 ファイルをチェックアウトをする方法(checkout)

ローカルリポジトリのファイル削除
[root@centos74 test1]# ls
README.md  test.txt

[root@centos74 test1]# rm test.txt
rm: 通常ファイル `test.txt' を削除しますか? y

[root@centos74 test1]# ls
README.md
checkout実行
[root@centos74 test1]# git checkout .
[root@centos74 test1]# ls
README.md  test.txt

Y メモ

ディレクトリの追加
[root@server ~]# git clone https://github.com/hana-shin/kernel-module-programming.git
[root@server ~]# cd kernel-module-programming/
[root@server kernel-module-programming]# mkdir test
[root@server kernel-module-programming]# touch test/README.md
[root@server kernel-module-programming]# git add test
[root@server kernel-module-programming]# git commit -m "Add Directory"
[root@server kernel-module-programming]# git push -u origin master
ディレクトリの削除
[root@server kernel-module-programming]# rm -f -r test
[root@server kernel-module-programming]# git add --all test
[root@server kernel-module-programming]# git commit -m "Delete Directory"
[root@server kernel-module-programming]# git push -u origin master
ファイルの追加
[root@server 01_MachineA]# ls
README.md  dump-1-install-3.sh  dump-1-install-4.sh  dump-install.sh

[root@server 01_MachineA]# git add --all
[root@server 01_MachineA]# git commit -m "First Commit"
[root@server 01_MachineA]# git push -u origin master
ファイルの削除
[root@server include]# ls
README.md  define.sh  functions.sh  test

[root@server include]# rm -f test
[root@server include]# git add --all
[root@server include]# git commit -m "Delete File"
[root@server include]# git push -u origin master
ファイルの修正
[root@server 01_MachineA]# vi dump-1-install-3.sh
[root@server 01_MachineA]# git add dump-1-install-3.sh
[root@server 01_MachineA]# git status
[root@server 01_MachineA]# git commit -m "Mod File"
[root@server 01_MachineA]# git log
[root@server 01_MachineA]# git push -u origin master
変更を取り込む方法
[root@kvm src]# cd 01_MachineA/
[root@kvm 01_MachineA]# ls
README.md  dump-1-install-3.sh  dump-1-install-4.sh  dump-install.sh
[root@kvm 01_MachineA]# git pull origin master

x 参考情報

今さら聞けない!GitHubの使い方【超初心者向け】
ローカルで作業ブランチを作成しリモートへPushするまでの手順
サルでもわかるgit入門
git / GitHubからいらないファイルを削除する
Git の基本 - Git リポジトリの取得
hana-shin

個人アクセストークンを使用する

2
6
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
2
6