はじめに
課題
ブランチを切り替えてpushしようとしたけど、コマンド(git branch)で表示されなかった。それを解決した過程をメモします。
・最近githubを使い始めたgit初心者です。
・cloud9からgithubに反映します。
・ブランチ以外のところは以下記事を見ながら進めました。
https://qiita.com/NaokiIshimura/items/b318ebc2daa4ec07a31e
作業概要
- ブランチをコマンド上で反映
- ブランチ切り替え
- push
実行したこと
メインのコマンド
■ブランチ関連
$ git fetch --all //ブランチ反映
$ git checkout -t origin/newbranch //ブランチの切り替え
$ git branch -a //ブランチの表示
$ git remote show origin //新しいブランチの表示
■通常のコマンド
$ git add ***/***/newbranch/***/***.py
$ git status
$ git commit -m "test"
$ git push -f origin newbranch
作業ログ
1. ブランチの切り替え
$git checkout newbranch
error: pathspec 'newbranch' did not match any file(s) known to git.
2. ブランチ表示
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/gcs2bq-upgrade
remotes/origin/impl-chrome-headless
remotes/origin/master
反映されない。。
3. 反映されないブランチを表示
反映されないブランチ(newbranch)が表示された。
$git remote show origin
* remote origin
Fetch URL: git@github.com:XXX/batches.git
Push URL: git@github.com:XXX/batches.git
HEAD branch: master
Remote branches:
newbranch new (next fetch will store in remotes/origin)
gcs2bq-upgrade tracked
impl-chrome-headless tracked
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (local out of date)
4. fetch
$ git fetch --all
Fetching origin
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 15 (delta 7), reused 1 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
From github.com:***/batches
c670459..097f7e4 master -> origin/master
* [new branch] newbranch -> origin/newbranch
5.再度ブランチを表示
ブランチ(newbranch)が反映された
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/newbranch
remotes/origin/gcs2bq-upgrade
remotes/origin/impl-chrome-headless
remotes/origin/master
5. ブランチ切り替え
$git checkout -t origin/newbranch fetch --all
git checkout -t origin/newbranch
error: Your local changes to the following files would be overwritten by checkout:
***/***/newbranch/***/***.py
Please, commit your changes or stash them before you can switch branches.
Aborting
エラーが出て切り替えできない。。
6. stash
$git stash save
Saved working directory and index state WIP on master: 097f7e4 Delete .tmp.py
HEAD is now at 097f7e4 Delete .tmp.py
https://backlog.com/ja/git-tutorial/reference/stash/
ようわからんがこれで大丈夫そう。
7. ブランチの切り替え
$ git checkout -t origin/newbranch
Branch newbranch set up to track remote branch newbranch from origin.
Switched to a new branch 'newbranch'
うん、切り替えできてる。
8. add
$git add ***/***/newbranch/***/***.py
$git status
# On branch newbranch
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .python-version
# .../.../newbranch/.keep
# .../.../newbranch/.../...py
...
statusを見ると「On branch newbranch」になっている。
9. commit
$git commit -m "test"
[newbranch 1168972] test
3 files changed, 72 insertions(+)
create mode 100644 ***/***/newbranch/.keep
create mode 100644 ***/***/newbranch/***/***.py
create mode 100644 ***/***/newbranch/***/***.py
10. push
$ git push -f origin newbranch
Counting objects: 13, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 1.44 KiB | 0 bytes/s, done.
Total 8 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To git@github.com:***/batches.git
5506300..1168972 newbranch -> newbranch
githubに反映された!