简体   繁体   中英

How to push new project code to specific branch in existing github repository?

I added the origin of remote repository in my code.
My existing repository have three branches: master , test , user .

But after adding and confirming the remote origin by command ' git remote -v ', the ' git branch --list ' command is displaying only one branch that is master .

I don't know if it is exactly pointing to that origin where I want to push the code.
I want to push the new code to test branch.

git branch -avv

 master                     first commit
 origin                     first commit
* test                       first commit
 remotes/origin/master      Update register.tsx
 remotes/origin/test        Update next.config.js
 remotes/origin/aman        june 25

I want to push it to remote test branch and delete local branches origin, test and master.

 git switch -c test
 fatal: only one reference expected

And when I tried to switch to remote/origin/test branch from vscode it says branch 'test' already exists.

For that, you need:

git fetch

Then a git branch -avv will show you all branches and their SHA1.

Note: only git branch --all/-a would list local as well as remote branches.

You can then compare master and origin/master commits.

Explanation: see " Git Internals - The Refspec "

 $ git remote add origin https://github.com/schacon/simplegit-progit

Running the command above adds a section to your repository's .git/config file, specifying the name of the remote ( origin ), the URL of the remote repository, and the refspec to be used for fetching:

 [remote "origin"] url = https://github.com/schacon/simplegit-progit fetch = +refs/heads/*:refs/remotes/origin/*

The fetch refspec is the reason why a git fetch would populate the origin namespace with all the remote branches Git can find from the remote repository.


I want to push the new code to test branch.

If you have:

  • code currently being modified, but not yet added/committed
  • only one local branch ( master )

You can do:

git fetch
git switch -c test
git add .
git commit -m "Code for test"
git rebase origin/test
git push

Since you do a git fetch first, the local branch test will follow automatically origin/test .
See git switch :

If <branch> is not found, but there does exist a tracking branch in exactly one remote (call it <remote> ) with a matching name, treat as equivalent to:

 $ git switch -c <branch> --track <remote>/<branch>

The rebase step ensures your local commit is done/reploayed on top origin/test .


I have one local commit but that was not intentional.
I want to push the code to remote/test branch and rest local branches like master , origin and test I want delete them all

Then I would, to make sure I don't loose anything:

  • clone your remote repository elsewhere, directly on the test branch (using git clone --branch/-b )
  • import your current code
  • make a new commit in the test branch
  • push test

That is:

cd /path/to/existing/local/repo
cd ..
git clone -b test https://github.com/<me>/<myRemoteRepo> repo2
cd repo2
git --work-tree=../repo add .
git commit -m "Import from original local repo"
git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM