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

Gitのコマンドの取り消し方マニュアル

よく忘れるので自分用メモ git init .gitリポジトリを作っているだけなので rm -rf .git git config --unsetを使う git config --global --unset user.name #user.nameが消去される git add git init直後の場合 git rm --cached -r . #すべてのファイル git rm --cached -r example.js #特定のファイル 2回目以降の場合 git reset HEAD #すべてのファイル git reset HEAD example.js #特定のファイル 参考: git add を取り消す git commit コミットを「取り消す」 直前のコミットの場合は、 git reset --soft HEAD^ #ローカルのファイルを残しておく場合 git reset --hard HEAD^ #ローカルのファイルも消す場合 コミットを「打ち消す」 これを使うことでコミットをなかったことにするのでなく、ファイルを戻すことができる。 git revert 1234567890abcdef1234567890abcdef12345678 #コミットのハッシュ値 コミットを「編集」 「コミットメッセージを書き換えたい」等の場合に使用する。 直前のコミットの場合は、 git commit --amend 参考: [Git]コミットの取り消し、打ち消し、上書き git push 歴史改変する場合 (こちらはローカルでコミットを打ち消したあとそれをforce-pushして歴史を改変するので、複数人で作業しているリポジトリやforce-pushを禁止しているブランチでは避けたほうが良いです) git reset --hard HEAD^ git push -f origin HEAD 打ち消す場合 こちらは比較的安全ですが、ログが見にくくなります git revert HEAD git push origin HEAD 参考: git push の取り消し方法 git remote git remote addで originを指定していれば git remote rm origin git clone ローカルリポジトリを完全に削除すればOK。 cd ../ #そのリポジトリにいた場合 rm -rf リポジトリ名 git log, reflog, diff, show など これは取り消しというよりやめ方。 qキーを押せば止まる。Ctrl+CやCtrl+Dでは止まらないため注意 git rebase git reflogで開始前の番号を特定し、 git reset --hard HEAD@{番号} git branch ブランチの削除 git branch -D ブランチ名 git fetch ローカルのブランチは更新されてないため git reset --hard HEAD git merge コンフリクトして、まだ手を付けてないとき git merge --abort コンフリクトして、手を付けたけどやめるとき git reset --hard HEAD マージすべて完了したけど戻すとき(revert) git revert -m 1 マージコミット 注意点 取り消したマージコミットに含まれていた変更を再度マージすることはできなくなります。 マージすべて完了したけど戻すとき(reset) git reset --hard ORIG_HEAD 注意点 コミット自体がなかったことになるので、複数人で作業しているリポジトリではやめたほうがいいです 参考: 【git】マージしたけどやっぱりやめたい時のやり方4種類 git pull 成功したけどやめたいとき git reflogでpull前の番号を特定し、 git reset --hard HEAD@{番号}
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Ubuntu20.04 に git-secrets をインストールする

はじめに git-secretsは、Amazon Web Services Labs が提供する、パスワードやその他の機密情報をgitリポジトリにコミットできないようにするツールです。 git と連携して git commit 実行前に自動的にsecretsの有無をチェックします。 Ubuntu 20.04 に git-secretsをインストールします。 環境 動作環境は以下の通り。 Ubuntu $ cat /etc/os-release NAME="Ubuntu" VERSION="20.04.2 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.2 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal 手順 インストール 公式サイトの手順に従って、インストールします。 まず、リポジトリをクローンして、 $ git clone https://github.com/awslabs/git-secrets インストールします。 $ cd git-secrets $ sudo make install インストールできたことを確認します。 $ git secrets -h usage: git secrets --scan [-r|--recursive] [--cached] [--no-index] [--untracked] [<files>...] or: git secrets --scan-history or: git secrets --install [-f|--force] [<target-directory>] or: git secrets --list [--global] or: git secrets --add [-a|--allowed] [-l|--literal] [--global] <pattern> or: git secrets --add-provider [--global] <command> [arguments...] or: git secrets --register-aws [--global] or: git secrets --aws-provider [<credentials-file>] --scan Scans <files> for prohibited patterns --scan-history Scans repo for prohibited patterns --install Installs git hooks for Git repository or Git template directory --list Lists secret patterns --add Adds a prohibited or allowed pattern, ensuring to de-dupe with existing patterns --add-provider Adds a secret provider that when called outputs secret patterns on new lines --aws-provider Secret provider that outputs credentials found in an ini file --register-aws Adds common AWS patterns to the git config and scans for ~/.aws/credentials -r, --recursive --scan scans directories recursively --cached --scan scans searches blobs registered in the index file --no-index --scan searches files in the current directory that is not managed by Git --untracked In addition to searching in the tracked files in the working tree, --scan also in untracked files -f, --force --install overwrites hooks if the hook already exists -l, --literal --add and --add-allowed patterns are escaped so that they are literal -a, --allowed --add adds an allowed pattern instead of a prohibited pattern --global Uses the --global git config インストールできました。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

(EC2) pushしてもlocalのcredencials.yml.encが反映されない

問題 AWSにデプロイする中でEC2のcredentialsにlocalのDB内容を反映させようとしたが、pullしても反映されない 原因 ローカルでcredentialsを編集してpushした後に、EC2内で git pull origin main を実行するとconflictが起きていたので、解決するためにEC2からgithubにpushしてpullしたのがよくなかった。(EC2からpushせずにcommitは削除するべきだった) 解決方法 githubですでにpushしてしまったEC2からのcommitを git reset --hard HEAD^ で遡り、 git push origin +main でEC2のcommitを消して、消したcommit上にあったlocalからのcommitを復活させるためにlocalのアプリケーションディレクトリで再度push。そして git pull origin main を実行するとcredentialsにはローカルと同じ内容が反映されている。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

(EC2) pullしてもlocalのcredencials.yml.encが反映されない

問題 AWSにデプロイする中でEC2のcredentialsにlocalのDB内容を反映させようとしたが、pullしても反映されない 原因 ローカルでcredentialsを編集してpushした後に、EC2内で git pull origin main を実行するとconflictが起きていたので、解決するためにEC2からgithubにpushしてpullしたのがよくなかった。(EC2からpushせずにcommitは削除するべきだった) 解決方法 githubですでにpushしてしまったEC2からのcommitを git reset --hard HEAD^ で遡り、 git push origin +main でEC2のcommitを消して、消したcommit上にあったlocalからのcommitを復活させるためにlocalのアプリケーションディレクトリで再度push。そして git pull origin main を実行するとcredentialsにはローカルと同じ内容が反映されている。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Git Flow のマージコミットについて

背景 自分一人のプロジェクトで開発中,GitHub で PR を開くのを横着してローカルで git flow feature finish ... とした.その時,マージコミットが発生せず直接 develop にコミットが生える形でマージされた. それだと後からのコミットログの見通しが悪いので,できればマージコミットを作ってほしい.そのため,原因の調査を行った. reproduce.sh $ git init Initialized empty Git repository in /path/to/project/.git/ $ echo 'test' > test.txt $ git add test.txt $ git commit -m 'initial commit' [main (root-commit) ab0702e] initial commit 1 file changed, 1 insertion(+) create mode 100644 test.txt $ git flow init Which branch should be used for bringing forth production releases? - main Branch name for production releases: [main] Branch name for "next release" development: [develop] How to name your supporting branch prefixes? Feature branches? [feature/] Release branches? [release/] Hotfix branches? [hotfix/] Support branches? [support/] Version tag prefix? [] $ git flow feature start hoge Switched to a new branch 'feature/hoge' Summary of actions: - A new branch 'feature/hoge' was created, based on 'develop' - You are now on branch 'feature/hoge' Now, start committing on your feature. When done, use: git flow feature finish hoge $ echo 'update' > test.txt $ git commit -m 'update test.txt' [feature/hoge 6a501b3] update test.txt 1 file changed, 1 insertion(+), 1 deletion(-) $ git flow feature finish hoge Switched to branch 'develop' Updating ab0702e..6a501b3 Fast-forward test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) Deleted branch feature/hoge (was 6a501b3). Summary of actions: - The feature branch 'feature/hoge' was merged into 'develop' - Feature branch 'feature/hoge' has been removed - You are now on branch 'develop' $ git log commit 6a501b3ca67efd86b9deca9f2861784a7d0f2c09 (HEAD -> develop) Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:33:41 2021 +0900 update test.txt commit ab0702e2740306296e87e21f9b717407903a14f4 (main) Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:32:58 2021 +0900 initial commit 調査 GitFlow のリポジトリで, issue が建てられていた. オーナーのコメント によると, Hi Scott, By design, git-flow uses the --no-ff option when merging in order to record that the commits belong together historically. However, when the feature branch contains only a single commit, the extra merge commit does not add anything and only complicates the branch tree needlessly. So for single-commit branches, fast-forward merges are being made as if the commit was done on develop directly. Cheers, Vincent ということなので,コミットが一つしかない Feature ブランチを finish するときは,自動で fast-forward するようになっているらしい.理由としては,「コミットが一つしかないブランチのためにマージコミットを作成しても,不必要に複雑になるから」とのこと. その考え方は確かにもっともなので, 解決 は望まずこれからはこの思想に従っていこうと思う.そもそも, Feature ブランチを 1 コミットで終わらせることはそれほどないが. ちなみに,二つ以上のコミットで確かめたところ,ちゃんとマージコミットが発生した. no-ff.sh $ git flow feature start fuga Switched to a new branch 'feature/fuga' Summary of actions: - A new branch 'feature/fuga' was created, based on 'develop' - You are now on branch 'feature/fuga' Now, start committing on your feature. When done, use: git flow feature finish fuga $ echo 'recent update' > test.txt $ git add test.txt $ git commit -m 'new changes' [feature/fuga 02f429a] new changes 1 file changed, 1 insertion(+), 1 deletion(-) $ git $ echo 'latest update' > test2.txt $ git add test2.txt $ git commit -m 'newest commit' [feature/fuga 1b96b5d] newest commit 1 file changed, 1 insertion(+) create mode 100644 test2.txt $ git flow feature finish fuga ## エディタが起動してマージコミットメッセージの編集画面に入る ## Switched to branch 'develop' Merge made by the 'recursive' strategy. test.txt | 2 +- test2.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 test2.txt Deleted branch feature/fuga (was 1b96b5d). Summary of actions: - The feature branch 'feature/fuga' was merged into 'develop' - Feature branch 'feature/fuga' has been removed - You are now on branch 'develop' $ git log commit 749095dca880aab548e0415c76b879b319cdcf34 (HEAD -> develop) Merge: 6a501b3 1b96b5d Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:47:33 2021 +0900 Merge branch 'feature/fuga' into develop commit 1b96b5db38eae5c26a6740e5856f689e50741f56 Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:47:24 2021 +0900 newest commit commit 02f429abaa787b9c4d969186e06a13b1707177e3 Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:46:56 2021 +0900 new changes commit 6a501b3ca67efd86b9deca9f2861784a7d0f2c09 Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:33:41 2021 +0900 update test.txt commit ab0702e2740306296e87e21f9b717407903a14f4 (main) Author: kino-ma <ma[AT]kino.ma> Date: Sun Dec 5 11:32:58 2021 +0900 initial commit 結論 GitFlow では,コミットが一つしかない場合は Fast Forward するようになっている.
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

(Unity LFS)UnityでLFSを使用した時の拡張子が無名ファイルへの対処法

UnityでLFS(Large File Storage)を使用すると、100MB以上のファイルを扱えます。 今回は、 Library/Artifacts/以下の拡張子が無名のファイルへの対処法です。 LFSは、.gitattributesにファイルの種類を指定して、使用します。 Library/Artifacts/以下に拡張子が無名のファイルがかなり存在します。 これらのファイルには、半角の空白を指定することで対応できました。 .gitattributes * filter=lfs diff=lfs merge=lfs -text インターネットの検索に書いていなかったので、困っている方がいれば一助になれれば嬉しいです。 ※この設定をすると、LFSのデータが結構溜まります。 VidelLinkでした。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

UnityでGitのリビジョンを表示したい!

0.前提 本記事はhcscode Advent Calendar 2021 の11日目の記事です。 この記事ではGit管理しているUnityプロジェクトで現在のリビジョン等を表示する方法について書いていきたいと思います。 ※Gitに関しての説明は省きますので、分からない方は別途調べてください。 1.この記事を読んで出来るようになること Unity内で、現在のリビジョンを確認できる! こんな感じで表示したりできるようになります。 2.やり方 1.環境構築 まずはGitを入れます。(Gitコマンドを使用するため) ここから最新版をダウンロードします。 ダウンロードしたexeを実行し、インストールを行います。(保存先のパスをこの後で使いますので、覚えておいてください) 下記の設定は「Use Windows' default console window」を選択してください。 PATHを通します。 「path」と検索して、「システム環境変数の編集」を開き画像の手順を行います。 先ほどインストールしたGitフォルダ内のbinフォルダを選択してください。 プロジェクトの作成、リポジトリの作成等をおこなってください。 2.スクリプト作成 1.作成したスクリプト using System.Diagnostics; /// <summary> /// Git情報を取得するクラス /// </summary> public class GitInfoGetter { /// <summary> /// プロセス /// </summary> private Process _process; /// <summary> /// コンストラクタ /// </summary> public GitInfoGetter() { _process = new Process(); //起動するアプリケーションを指定する _process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec"); //シェルを使用するか _process.StartInfo.UseShellExecute = false; //出力をStandardOutPutに書き込むか _process.StartInfo.RedirectStandardOutput = true; } /// <summary> /// デストラクタ /// </summary> ~GitInfoGetter() { _process = null; } /// <summary> /// リビジョンを取得 /// </summary> /// <returns>リビジョン</returns> public int GetRevision() { //実行するコマンドを設定 _process.StartInfo.Arguments = "/k git log --date=iso --pretty=format:\"% h\" | find /c \" \""; //実行 _process.Start(); //結果を取得 string result = _process.StandardOutput.ReadLine(); //終了 _process.Close(); //文字列を数値に変換 if (!int.TryParse(result, out int revision)) { UnityEngine.Debug.LogErrorFormat("Error : GetRevision, result ={0}", result); revision = -1; } return revision; } /// <summary> /// 現在のコミットIDを取得 /// </summary> /// <returns>コミットID</returns> public string GetCurrentCommitID() { //実行するコマンドを設定 _process.StartInfo.Arguments = "/k git rev-parse --short HEAD"; //実行 _process.Start(); //結果を取得 string result = _process.StandardOutput.ReadLine(); //終了 _process.Close(); return result; } } 後は必要に応じて、処理を呼んで結果を受け取ってください。 2.説明 //起動するアプリケーションを指定する _process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec"); ここで、コマンドプロンプトを起動することを設定しています。 //実行するコマンドを設定 _process.StartInfo.Arguments = "/k git log --date=iso --pretty=format:\"% h\" | find /c \" \""; このコマンドは、「git log --date=iso --pretty=format:\"% h\"」で下記のような出力が行われます。 7e70e6a ///最新のコミットID(短縮版)※このコメントは出力には含まれません。 87ad678 e4883fd 33fa433 6868ba7 5bcfb3b  ///最初のコミットID(短縮版)※ そして、「| find /c \" \""」でこの出力から、行数を数えて出力しています。(正確には、空白が含まれている行数) こうやって現在のリビジョン数を取得しています。 //実行するコマンドを設定 _process.StartInfo.Arguments = "/k git rev-parse --short HEAD"; このコマンドでは最新のコミットID(短縮版)を出力しています。 上でいう「7e70e6a」が出力されます。 3.最後に いかがでしたでしょうか。 コマンドを実行することで、色々なこともできるので、是非試してみて下さい。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む