1

I have non empty folder with project. I want to push not to the master but to the branch . How to do this?

cd <folder>
git init
git remote add origin <url> //at this point my folder connected to the master

What should be next?

git branch <branch1>
git add .
git commit -m "commint to branch1"
git push
A191919
  • 3,422
  • 7
  • 49
  • 93
  • Does "branch1" exist on the remote `origin` or do you need to create it? – Richard May 29 '18 at 09:10
  • @Richard, need to create. – A191919 May 29 '18 at 09:11
  • And the obvious question: have you done a `pull` or ` fetch` to get everything already in the remote? – Richard May 29 '18 at 09:13
  • *"What should be next?"* -- first thing to do after you add a remote or change the URL of an existing remote is [`git fetch`](https://git-scm.com/docs/git-fetch). It gets on the local repo the information about the commits and branches present on the remote repo. – axiac May 29 '18 at 09:14
  • @Richard, no currently did not done pull, fetch. – A191919 May 29 '18 at 09:14
  • You can `git fetch` to get information from the remote, then `git checkout -b ` to create the new branch. Then add, commit and push (then maybe pull to check everything is OK). – Aimery May 29 '18 at 09:27
  • Possible duplicate of [How to convert existing non-empty directory into a Git working directory and push files to a remote repository](https://stackoverflow.com/questions/3311774/how-to-convert-existing-non-empty-directory-into-a-git-working-directory-and-pus) – phd May 29 '18 at 13:39

2 Answers2

1

As you've not done a git fetch you don't have the changes from the remote in any branches including the one you've created. Assuming you want to use branch1 as the name in the remote. I would suggest

  • Backup your local repository (copy the .git folder somewhere else)
  • Switch to master

    git checkout master
    
  • Rename your local working branch for the time being

    git branch -m branch1 tempBranch
    
  • Get the content of the remote locally

    git fetch
    
  • Ensure your master is up to date with origin/master

    git pull
    
  • Create a new working branch and set it up in the origin

    git checkout -b branch1
    git push -u origin branch1
    
  • Put the changes you wanted into the real branch1

    git merge tempBranch
    

    you may need to handle conflicts at this point.

  • Push your changes

    git push
    

Alternatively, if you've only made trivial changes, just start anew with a clean clone of the remote and redo you changes in that new local repository.

Richard
  • 106,783
  • 21
  • 203
  • 265
0

You can use following commands :

git checkout -b <branch1>
git add .
git commit -m "commint to branch1"
git push -u origin <branch1>
dr. strange
  • 665
  • 7
  • 22