Search code examples
gitgithubgit-push

Git push to specific branch with username and password


I need to push my changes to specific branch with username and password. So I found the following command to push directly with username and password.

$ cd demo   //branch name
$ git add .
$ git commit -m "Test commit"
$ git push --repo https://username:password@bitbucket.org/name/repo.git

Here I need to push my changes to demo of the above url with username and password directly so that It will not ask me again. But as per the above command I found its not pushing to any particular branch. I need to commit my changes to that particular branch demo which also present in remote.


Solution

  • cd demo is not supposed to be a "branch name", just the name of the folder you have clone your repository.

    • git status would give you the name of the currently checked out branch.
    • git switch would allow you to change branches.
    • git branch -avv would show you all local and remote tracking branches

    A simple git push would push by default the current branch (for instance 'main') to origin/main, assuming it is already configured that way (a git clone would typically associate main to origin/main)

    If origin is not set, you can add it with:

    git remote add origin  https://username:password@bitbucket.org/name/repo.git
    

    Although it is best to cache those credentials, using the helper configured locally: check the output of git config credential.helper.

    Once you are on the right branch my_branch, the command would be:

    git push -u origin my_branch