20191024のGitに関する記事は7件です。

Initial Commitを取り消す

gitでInitial Commit(最初のコミット)を取り消そうとして
git reset HEAD~
を実行しても、

$ git reset HEAD~
fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

となり取り消すことができません。
Initial Commitは以下のコマンドで取り消すことができます。

$ git update-ref -d HEAD
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

How to squash many commits together by git

How to squash many commits together by git

Simply there are two ways to finish this task.

  • git rebase
  • git reset

solution with git rebase

For example, if the user wishes to view 5 commits from the current HEAD in the past the command is

? git rebase -i HEAD~3

without a number, go to log to check SHA1.

? git log --oneline

--oneline will make log easy to read.

pick the SHA1 of the oldest commit you want to squash.

? git rebase -i 345678

Then get into interactive mode.

pick 123456 a
pick 234567 b
pick 345678 c

# Rebase 123456..345678 onto 456789
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

edit top of this part like below.

squash 123456 a
squash 234567 b
pick 345678 c

:wq save and quit

get another editor.

# This is a combination of 3 commits.
# The first commit's message is:
a
# This is the 2nd commit message:
b
# This is the 3rd commit message:
c

:wq save and quit editor.

and log, it should become cleaner.

as the first interactive mode shows, there are tons of things, we can do with git rebase. Maybe explore in the future?.

solution with git reset

? git reset --soft HEAD~1

Assuming ~n is the number of commits to softly un-commit (i.e. ~1, ~2,...), or like before give a SHA1 with a simple git log.

? git commit --amend

which is pretty much the same as a long-range of squash and one pick.

Repost from my blog?

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

How to undo commits locally and remote?

Basically, there are 3 ways to deal with this problem.
1. git reset
2. git revert
3. git rebase

git reset (doesn't leave any record)

* First undo last commit locally

Force to undo the last commit in a local repository, HEAD^ means return last one commit.

? git reset --hard HEAD^
? git reset --soft HEAD^
? git reset --soft 8a15r31

--hard discard changes totally.
--soft keep changes. If git commit, there will be the same commit that undone one.

rather than return to one commit before, give a specific commit id like 8a15r31, is the same.

* Force push to remote

After a local undo procedure, then push remote. If you just normal push, there will be conflict. Therefore, we need to use Force Push with a -f flag.

? git push -f origin master

[origin master] is the branch that has the wrong commit.
Normally, don't use this way except you have full control of the repository that you're working on.

git revert(leave a record)

* First undo last commit locally

? git revert HEAD
? git revert HEAD --no-commit

These will create a new commit to 'kill' the last commit, without --no-commit or -n, get into vi-mode to edit commit comment, if no editing comment will seem like

Revert "commit comment"
    This reverts commit d7795f88ebd5bf427c4c20faa9d372c997c8e490.

* Push to remote

? git push origin master

Push commit to the remote.

Here is the complete solution, if you deal with local, Github and server.

git rebase(edit commits history)

Most of the time, just undo a commit, don't need to use this command, but combine several commits to ONE commit, git rebase is useful.

Repost from my blog?

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【WSL開発環境】構築からGit操作(コマンドライン / IDE) まで

wsl.png

目次

  • 1. 環境
  • 2. Aptコマンドによるパッケージ管理
    • 2-1. パッケージアップデート
    • 2-2. パッケージアップグレード
  • 3. Gitインストール
    • 3-1. デフォルトパッケージ
    • 3-2. ソースからインストール
  • 4. Git設定
  • 5. SSH設定
  • 6. GitHub / GitLab 設定
    • 6-1. GitHub
    • 6-2. GitLab
    • 6-3. 接続確認
  • 7. Atom
  • 8. 参考

1. 環境

  • Windows 10 Home
  • WSL
    • Ubuntu (Ver. 1804.2019.521.0)
  • GitHub / GitLab アカウントは登録済
  • Atom (1.40.1)

2. Aptコマンドによるパッケージ管理

2-1. パッケージアップデート

$ sudo apt update

2-2. パッケージアップグレード

$ sudo apt upgrade

3. Gitインストール

※ 通常まっさらな状態から sudo apt update sudo apt upgrade 実行後にはすでにGitは入った状態だが、削除したり何らかの理由で入っていない場合に実行。

3-1. デフォルトパッケージ

※ この方法でインストールするGitは最新より古いバージョンがインストールされる。

  • Gitをインストールする。
$ sudo apt install git
  • インストールしたGitのバージョンを確認する。
$ git --version

3-2. ソースからインストール

※ この方法でインストールする場合は時間が少々かかり、Aptでパッケージ管理されないが、最新バージョンをインストールできる。

  • Gitをインストールする。
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
  • インストールしたGitのバージョンを確認する。
$ git --version

4. Git設定

  • ユーザー名とメールアドレスを設定する。
$ git config --global user.name "[username]"
$ git config --global user.email "[email]"

※ プロジェクトごとに別のユーザー名とメールアドレスを利用する場合は global を省略する。

  • 設定を確認する。
$ git config --list
user.name=[username]
user.email=[email]

5. SSH設定

  • SSH鍵を生成する
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/[username_ubuntu]/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/[username_ubuntu]/.ssh/id_rsa.
Your public key has been saved in /home/[username_ubuntu]/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hxbYsu6UHql6qA4tIIA7BnbVktsubFbEV6ewBQw+hmM [username_ubuntu]@DESKTOP-QDJ9F6V
The key's randomart image is:
+---[RSA 2048]----+
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
+----[SHA256]-----+
  • config ファイルと生成し、ホスト名、ドメイン、ユーザー名、秘密鍵のパスを記述する。
vim /home/[username_ubuntu]/.ssh/config
Host github
  HostName github.com
  User git
  IdentityFile /home/[username_ubuntu]/.ssh/config/id_rsa

もしくは

Host gitlab
  HostName gitlab.com
  User git
  IdentityFile /home/[username_ubuntu]/.ssh/config/id_rsa

ここからが厄介で、自分が踏んだ地雷は以下の3つだった。

  1. コマンドラインでGit操作をするときに参照する鍵はLinux側の /home/[username_ubuntu]/.ssh/id_rsa 、IDEで参照する鍵は /mnt/c/Users/[username_windows]/.ssh/id_rsa を参照するので、それぞれを上手く管理しなければならない。
  2. Linux側の /home/[username_ubuntu]/.ssh/ のシンボリックファイルをWindows側に作ると、パーミッションで読み取りが許可されていないので秘密鍵を読み込めない。IDEでGit操作ができない。
  3. Windows側の /mnt/c/Users/[username_windows]/.ssh/id_rsa のシンボリックファイルをLinux側に作ると、パーミッションが緩すぎて怒られる。

結局、シンボリックファイルではなく同一の鍵をWindows側にコピーして対処した。

どなたか良い方法あれば教えて頂きたい。

6. GitHub / GitLab 設定

公開鍵ファイルの文字列をGitHubもしくは GitLabに登録する。

cat /mnt/c/Users/[username_windows]/.ssh/id_rsa.pub
ssh-rsa HOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobar [username_ubuntu]@DESKTOP-XXXXXXX

6-1. GitHub

  • Setting をクリックする。

github-1.png

  • SSH and GPG keys をクリックする。

github-2.png

  • New SSH key をクリックする。

github-3.png

  • 任意の名前をつけ、先ほどの公開鍵ファイルの文字列を貼り付け、鍵を登録する。

github-4.png
github-5.png

6-2. GitLab

  • Setting をクリックする。
    gitlab-1.png

  • SSH keys をクリックする。
    gitlab-2.png

  • 先ほどの公開鍵ファイルの文字列を貼り付け、鍵を登録する。
    gitlab-3.png
    gitlab-4.png

6-3. 接続確認

  • ssh [/home/[username_ubuntu]/.ssh/config に登録したホスト名] を実行して接続を確認する。以下のようなメッセージが返って来ればOK。
$ ssh github
PTY allocation request failed on channel 0
Hi oasis-forever! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

7. Atom

AtomなどのIDEではUIでGit操作が可能である。


atom-1.png
* Stage: $ git add [filename]
* Discard Changes: $ git stash (individual)
* Stage All: $ git add -A


atom-2.png
* Discard All Changes: $ git stash (all)
* Undo Last Discard: $ git stash pop


atom-3.png
* Click File: $ git diff
* Stage Selection: $ git add [filename] (partly)
* Discard Selection: $ git stash (partly)


atom-4.png
* Input a message and click Commit to [branch name]: git commit -m "[message]"


atom-5.png
* Push: git push origin [branch name]


atom-6.png
* Fetch: git fetch origin [branch name]
* Pull: git pull origin [branch name]
* Push: git push origin [branch name]
* Push Force: git push origin [branch name] -f

8. 参考

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

WSL開発環境 〜構築からGit操作(コマンドライン / IDE) まで〜

wsl.png

目次

  • 1. 環境
  • 2. Aptコマンドによるパッケージ管理
    • 2-1. パッケージアップデート
    • 2-2. パッケージアップグレード
  • 3. Gitインストール
    • 3-1. デフォルトパッケージ
    • 3-2. ソースからインストール
  • 4. Git設定
  • 5. SSH設定
  • 6. GitHub / GitLab 設定
    • 6-1. GitHub
    • 6-2. GitLab
    • 6-3. 接続確認
  • 7. Atom
  • 8. 参考

1. 環境

  • Windows 10 Home
  • WSL
    • Ubuntu (Ver. 1804.2019.521.0)
  • GitHub / GitLab アカウントは登録済
  • Atom (1.40.1)

2. Aptコマンドによるパッケージ管理

2-1. パッケージアップデート

$ sudo apt update

2-2. パッケージアップグレード

$ sudo apt upgrade

3. Gitインストール

※ 通常まっさらな状態から sudo apt update sudo apt upgrade 実行後にはすでにGitは入った状態だが、削除したり何らかの理由で入っていない場合に実行。

3-1. デフォルトパッケージ

※ この方法でインストールするGitは最新より古いバージョンがインストールされる。

  • Gitをインストールする。
$ sudo apt install git
  • インストールしたGitのバージョンを確認する。
$ git --version

3-2. ソースからインストール

※ この方法でインストールする場合は時間が少々かかり、Aptでパッケージ管理されないが、最新バージョンをインストールできる。

  • Gitをインストールする。
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
  • インストールしたGitのバージョンを確認する。
$ git --version

4. Git設定

  • ユーザー名とメールアドレスを設定する。
$ git config --global user.name "[username]"
$ git config --global user.email "[email]"

※ プロジェクトごとに別のユーザー名とメールアドレスを利用する場合は global を省略する。

  • 設定を確認する。
$ git config --list
user.name=[username]
user.email=[email]

5. SSH設定

  • SSH鍵を生成する
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/[username_ubuntu]/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/[username_ubuntu]/.ssh/id_rsa.
Your public key has been saved in /home/[username_ubuntu]/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hxbYsu6UHql6qA4tIIA7BnbVktsubFbEV6ewBQw+hmM [username_ubuntu]@DESKTOP-QDJ9F6V
The key's randomart image is:
+---[RSA 2048]----+
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
+----[SHA256]-----+
  • config ファイルと生成し、ホスト名、ドメイン、ユーザー名、秘密鍵のパスを記述する。
vim /home/[username_ubuntu]/.ssh/config
Host github
  HostName github.com
  User git
  IdentityFile /home/[username_ubuntu]/.ssh/config/id_rsa

もしくは

Host gitlab
  HostName gitlab.com
  User git
  IdentityFile /home/[username_ubuntu]/.ssh/config/id_rsa

ここからが厄介で、自分が踏んだ地雷は以下の3つだった。

  1. コマンドラインでGit操作をするときに参照する鍵はLinux側の /home/[username_ubuntu]/.ssh/id_rsa 、IDEで参照する鍵は /mnt/c/Users/[username_windows]/.ssh/id_rsa を参照するので、それぞれを上手く管理しなければならない。
  2. Linux側の /home/[username_ubuntu]/.ssh/ のシンボリックファイルをWindows側に作ると、パーミッションで読み取りが許可されていないので秘密鍵を読み込めない。IDEでGit操作ができない。
  3. Windows側の /mnt/c/Users/[username_windows]/.ssh/id_rsa のシンボリックファイルをLinux側に作ると、パーミッションが緩すぎて怒られる。

結局、シンボリックファイルではなく同一の鍵をWindows側にコピーして対処した。

どなたか良い方法あれば教えて頂きたい。

6. GitHub / GitLab 設定

公開鍵ファイルの文字列をGitHubもしくは GitLabに登録する。

cat /mnt/c/Users/[username_windows]/.ssh/id_rsa.pub
ssh-rsa HOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobar [username_ubuntu]@DESKTOP-XXXXXXX

6-1. GitHub

  • Setting をクリックする。

github-1.png

  • SSH and GPG keys をクリックする。

github-2.png

  • New SSH key をクリックする。

github-3.png

  • 任意の名前をつけ、先ほどの公開鍵ファイルの文字列を貼り付け、鍵を登録する。

github-4.png
github-5.png

6-2. GitLab

  • Setting をクリックする。
    gitlab-1.png

  • SSH keys をクリックする。
    gitlab-2.png

  • 先ほどの公開鍵ファイルの文字列を貼り付け、鍵を登録する。
    gitlab-3.png
    gitlab-4.png

6-3. 接続確認

  • ssh [/home/[username_ubuntu]/.ssh/config に登録したホスト名] を実行して接続を確認する。以下のようなメッセージが返って来ればOK。
$ ssh github
PTY allocation request failed on channel 0
Hi oasis-forever! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

7. Atom

AtomなどのIDEではUIでGit操作が可能である。


atom-1.png
* Stage: $ git add [filename]
* Discard Changes: $ git stash (individual)
* Stage All: $ git add -A


atom-2.png
* Discard All Changes: $ git stash (all)
* Undo Last Discard: $ git stash pop


atom-3.png
* Click File: $ git diff
* Stage Selection: $ git add [filename] (partly)
* Discard Selection: $ git stash (partly)


atom-4.png
* Input a message and click Commit to [branch name]: git commit -m "[message]"


atom-5.png
* Push: git push origin [branch name]


atom-6.png
* Fetch: git fetch origin [branch name]
* Pull: git pull origin [branch name]
* Push: git push origin [branch name]
* Push Force: git push origin [branch name] -f

8. 参考

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Git初期設定

アカウントの設定

# git config --global user.email "imamu1202@gmail.com"
# git config --global user.name "imamu1202"

アカウントの設定確認

$ git config user.email
imamu1202@gmail.com
$ git config user.name
imamu1202
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

とりあえずの最低限Gitコマンド

コマンドで扱う

プログラミングを初めてから、ずっとGitHubDesktopを利用していた。
しかし現場に入ってから、コマンドでGitを扱うことが増えたため、使用する頻度が高いと感じるコマンドを備忘録として書いておく。

コマンド一覧

//ファイルの登録
$ git add <ファイル名>
//ファイルの変更、追加をコミット
$ git commit -m "コミットメッセージ"
//ローカルの変更を確認
$ git status
//コミットの変更履歴を確認する
$ git status
//リモートにプッシュ
$ git push origin <ブランチ名>
//ブランチをマージ
$ git merge <ブランチ名>
//ブランチの移動
$ git checkout <ブランチ名>
//作業内容を一時避難
$ git stash
//一時避難させた内容を一覧表示
$ git stash list
//一時避難させた内容を復活させる
$ git stash apply <stash名>
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む