1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【git入門】git cloneした後、手元のブランチを既存のリモートブランチ名で作成したらgit pushがうまくいかなくなった

Last updated at Posted at 2018-12-21

git初心者の頃、正しくgit pullをしなかったがために、git pushがうまくいかなくて困った時のメモです。
#【事象】
git初心者の頃、僕は進めていたタスクがmergeまで一段落する度に、local環境を綺麗にするためしプロジェクトのディレクトリごと削除していました。そして、ある日、 再度同じリポジトリを修正することになったため、git cloneし、手元のlocal環境にて、以前作業していたものと同じ名前でブランチを"作成"し、pushしようとしたらfailしました。
#【解決】
git cloneで持ってくる際に、 既存のブランチを指定 してcloneしてくることで問題なくpush ができました。
#【ダメだった時】
当時やったことをコマンドラインで。

既にmerge済みのブランチ(merged-001)を修正することとなり作業開始
# git clone URL
# git checkout -b merged-001
色々修正
# git add 対象ファイル
# git commit -m "チケット××の〇〇を修正しました!"
# git push origin merged-001
項目
既存のブランチ名 merged-001
プロジェクト名 deb-packages
激おこメッセージ
[root@localhost deb-packages]# git push origin feature/merged-001
To https://user@example.com/SRE/deb-packages.git
 ! [rejected]        feature/merged-001 -> feature/merged-001 (non-fast-forward)
error: failed to push some refs to 'https://user@example.com/SRE/deb-packages.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[root@localhost deb-packages]# 
[root@localhost deb-packages]# 

#【解決した時】
git cloneの際に、以前作業してブランチを指定してcloneすることで解決しました。

ブランチを指定してcloneする書き方。
git clone -b <指定したいブランチ名name> "リポジトリURL"

解決の際やったことをコマンドラインで。

# git clone -b feature/merged-001 "https://user@example.com/SRE/deb-packages.git"
タスクの修正追加等
# git addとかそういうやつ。
# git push origin feature/merged-001

#【詳細】
git pushに失敗した時は、リモートからmasterを新たにcloneし、修正をかけてpushしようとしていました。しかしそれだと、既に上がっているMergeRequestに記録されている各 commitのハッシュ値 と、現在の作業のcommitのものが異なってしまいます。そのため、ブランチの名前は同じでも、別々のブランチを無理に統合しようとしてると判断されるのだそうです。そして、結果、rejectされてしまっていたのですね~。

#《参考》
Gitのコミットハッシュ値は何を元にどうやって生成されているのか
 - Mercari Engineering Blog
https://tech.mercari.com/entry/2016/02/08/173000
git-clone - Clone a repository into a new directory
https://git-scm.com/docs/git-clonehttps://git-scm.com/docs/git-clone
2.3 Git Basics - Viewing the Commit History
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?