20220318のGitに関する記事は3件です。

間違ったgit commitを修正したい

PSHU後にコミットミスに気がついた % git add . % git commit -m "#8 課題1 マジックナンバーを修正" % git push origin main // コミットの場所を間違えていた!時を遡りたい... ------------------------------------------------------ % git commit --amend -m "#10 課題2 マジックナンバーを修正" // 直前のコミットが更新された! ------------------------------------------------------ % git status // 変更内容の確認1 % git log // 変更内容の確認2 確認したらqを押すともどる // 万が一テンパってローカルに更新内容があったりすると以下のように怒られる % git push origin main To github.com:hogehogeStady/Lesson_ymp-a.git ! [rejected] main -> main (non-fast-forward) error: failed to push some refs to 'github.com:hogehogeStady/Lesson_ymp-a.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. -------------------------------------------------------- % git push -f origin main // 私は冷静に強制プッシュします(ローカルの内容をリモートリポジトリへ反映) -------------------------------------------------------- コミットとプッシュは心を落ち着かせてから 冷静職人 次回はスマートに修正したい。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

git-filter-repo NameError: name 'git' is not defined

事象 : Scoopでインストールしたgit-filter-repoが動かない 環境 Windows10 Pro バージョン21H1 git version 2.35.1.windows.1 Python 3.8.5 pip 20.2.4 git-filter-repoを使おうと思い「Scoopをインストール」して「Scoopでgit-filter-repoをインストール」しました。 しかし、エラーになって動きません。 # PowerShellに切替える $ powershell Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. 新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6 # セキュリティポリシーが初期値(Restricted)場合は、「RemoteSigned」へ変更する PS C:\path\to> Get-ExecutionPolicy Restricted PS C:\path\to> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process # Scoopをインストールする PS C:\path\to> Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') Initializing... Downloading... Extracting... Creating shim... Adding ~\scoop\shims to your path. Scoop was installed successfully! Type 'scoop help' for instructions. # 常用しているBashに切替える PS C:\path\to> bash # Scoopでgit-filter-repoをインストール $ scoop install git-filter-repo Installing 'git-filter-repo' (2.34.0) [64bit] Downloading https://github.com/newren/git-filter-repo/archive/v2.34.0.zip (-1 B)... Checking hash of v2.34.0.zip ... ok. Extracting v2.34.0.zip ... done. Running pre-install script... Linking ~\scoop\apps\git-filter-repo\current => ~\scoop\apps\git-filter-repo\2.34.0 Creating shim for 'git-filter-repo'. Running post-install script... 'git-filter-repo' (2.34.0) was installed successfully! 'git-filter-repo' suggests installing 'python'. # しかし、動かない・・・・ $ git filter-repo -h Traceback (most recent call last): File "C:\Users\chovin\scoop\apps\git-filter-repo\current\git_filter_repo.py", line 1, in <module> git-filter-repo NameError: name 'git' is not defined 原因 : 不明 いろいろ調べても試しても状況は変わりらず、原因は分かりませんでした。 試したこと scoop/apps/git-filter-repo/2.34.0/git-filter-repoの1行目のシェ版を#!/usr/bin/env python3から#!/usr/bin/env pythonに変える 私の環境では、Pythonをpython3ではなくpythonでパスを通しています。 参考 : git-filter-repoを利用してGitからファイルを履歴も合わせて削除する - こんにちは寝る ターミナルのシェルをBashではなくPowerShellにしてから、Scoopでgit-filter-repoをインストールする 既存のGitとは別にScoopでGitをインストールする 既存のPythonとは別にScoopでPythonをインストールする 対応 : pipでgit-filter-repoをインストールする git-filter-repoのINSTALL.mdを見て深く考えずに「Scoop」を使ってインストールしました。 This list covers at least Windows (Scoop), Mac OS X (Homebrew), and Linux (most the rest). Note that I do not curate this list (and have no interest in doing so); https://repology.org tracks who packages these versions. git-filter-repo/INSTALL.md しかし、どうせPythonが必要になるのであればpipを使えばいいことに思い当たりました。この思い付きが功を奏しました。 参考 : git-filter-repo · PyPI # Scoopのgit-filter-repoをアンインストールする $ scoop uninstall git-filter-repo Uninstalling 'git-filter-repo' (2.34.0). Removing shim 'git-filter-repo'. Removing shim 'git-filter-repo.cmd'. Unlinking ~\scoop\apps\git-filter-repo\current 'git-filter-repo' was uninstalled. # Scoop自体もアンインストールする $ scoop uninstall scoop WARN This will uninstall Scoop and all the programs that have been installed with Scoop! Are you sure? (yN): y Removing ~\scoop\shims from your path. Scoop has been uninstalled. # pipでgit-filter-repoをインストールする $ pip install git-filter-repo Collecting git-filter-repo Downloading git_filter_repo-2.34.0-py2.py3-none-any.whl (99 kB) |████████████████████████████████| 99 kB 1.3 MB/s Installing collected packages: git-filter-repo Successfully installed git-filter-repo-2.34.0 WARNING: You are using pip version 20.2.4; however, version 22.0.4 is available. You should consider upgrading via the 'c:\apps\python38\python.exe -m pip install --upgrade pip' command. # 動いた! $ git filter-repo -h Rewrite (or analyze) repository history git-filter-repo destructively rewrites history (unless --analyze or --dry-run are given) according to specified rules. It refuses to do any rewriting unless either run from a clean fresh clone, or --force was given. #...省略...
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

私のためのGitコマンド

Gitコマンド 自分自身のための gitコマンド集です git status Gitリポジトリの状態を表示 git status git add ステージ領域にファイルを登録(コミットする前の一時領域) git add ファイル名 git add . // カレントディレクトリ配下全て git add README.md git commit ステージ領域に登録されているファイル群をリポジトリの記録として登録 git commit -m 'コミットメッセージ' git commit --amend // 直前のコミットメッセージを変更 -m を付けずに実行した場合、エディタが立ち上がり詳細なメッセージの入力が可能 一行目 コミットする内容の要約 二行目 空業 三行目 変更理由や詳細 git remote add リモートリポジトリを登録 git remote add origin リポジトリのURL git push 現在のローカルリポジトリの内容をリモートリポジトリに送信 git push origin main *origin リモートリポジトリの main ブランチに送信する git log リポジトリにコミットされたログを確認 git log git log README.md // 指定したディレクトリ、ファイルのみログに表示 git log -p README.md // READMEファイルの差分を確認 git log --graph // ブランチをグラフとして確認 git diff 現在のワークツリーとステージ領域との差分を確認 git diff git diff HEAD // 今回のコミットと前回のコミットの差分を確認 git branch ブランチを一覧表示 git branch git branch ブランチ名 // ブランチを作成する git checkout ブランチを切り替える git checkout ブランチ名 git checkout -b ブランチ名 // ブランチを作成し切り替える git merge ブランチを結合 git merge 結合対象ブランチ名 git merge --no-ff 結合対象ブランチ名 // マージコミットメッセージを作成 git reset 過去にコミットした時点まで遡る git reset --hard ハッシュ ハッシュは git log で表示したコミットログの先頭に記載されている git reflog リポジトリで行われた作業を確認 git reflog git resetで過去の状態まで遡ってブランチを切り修正を加えコミットした後、過去に遡る前の状態まで戻りたい場合(つまり未来に移動したい場合) git reflogで表示されたログのハッシュを使用し、git reset --hardをすれば過去に遡る前の状態まで進めることができ、本来作業していたブランチと、過去に戻り修正したブランチをマージすれば現在のコードに修正を結合することができる。 git rebase -i 過去のコミットを削除して改変 git rebase -i HEAD~2 // 現在のブランチのHEADを含めた二つまでの歴史を対象とする エディタが立ち上がり、削除したいコミットに記載されている pick という部分を削除し fixup に変更し保存する。 削除したコミットの内容をもう一つのコミットの内容として加え、それを新しいコミットとすることになる。 (すでにコミットしたコミットメッセージにタイプミスなどがあった場合、新しいコミットを作成し、タイプミスしたコミットメッセージは削除し、新しいコミットメッセージに書き換えたい場合などに便利) git clone リモートリポジトリを取得 git clone リポジトリのURL リモートリポジトリの情報を現在のディレクトリにコピーする git pull 最新のリモートリポジトリブランチを取得 git pull origin ブランチ名 Git機能 Issue Issueはソフトウェア開発におけるバグや議論などを管理するために発行するもの。 タスクリストを作成したり、各Issueに独自のラベルを張り付けることができる。 Fork 任意のリポジトリを自分のリポジトリとして作成する。 Pull Request 自分で変更したコードを相手のリポジトリに取り込んでもらえるように依頼するための機能。 とあるリポジトリをForkし、cloneしてローカル環境にリポジトリを持ってくる そこでブランチを切り、機能の追加や問題点を修正し、Pull Requestを送る といった流れで、他のソフトウェア開発に参戦することができる。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む