gitgitlabpushbranch

How to push local code to new branch on gitlab . need specific steps (looks like a duplicate question but i need some specific steps)


i have a git lab repository , I was asked to create a new branch and push code that i have. I have verified lot of articles here and examples but not sure whehter i need all those different steps.

I have just gitbash installed on my pc and i have remote repository url and, have a folder where i have my code which needs to be added to that new branch

Can some one help me setup steps. My understanding i should perform below on my local pc is this correct?

    git config --global user.name "your_username" 

    git config --global user.email "your_email_address@example.com"
   
    cd into directory where you have local code
    
    git clone https://gitlab.com/gitlab-tests/sample-project.git
    
    git init
    
    git remote add origin <git@gitlab.com:username/projectpath.git
    
    git checkout -b <name-of-branch>
    
    git checkout <name-of-branch>
    
    git add .
    git commit -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT"
    
    git push <remote> <name-of-branch>

Solution

  • What you need to do is this:

    # This gets a copy of the remote repository to your PC (login will probably be required)
    # Replace the URL with the actual URL of the repo
    git clone https://gitlab.com/gitlab-tests/sample-project.git
    
    # get into the repository (replace with the repository's name)
    cd sample-project
    
    # get your code where it should go
    cp <where your local code is> <where it should be in the repo>
    
    # Get to the branch (use the command that applies to your case!)
    # Create a new branch if it does note exist on the remote
    git checkout -b <branch-name>
    # Check out the branch, if it already exists
    git checkout <branch-name>
    
    # Add the new code
    git add .
    git commit -m "<add a meaningful comment here>"
    
    # Push the code to the remote repository (use the command that applies to your case!)
    # like this if the branch does not exist on the remote
    git push --set-upstream origin <branch-name>
    # like this if the branch already exists on the remote
    git push origin <branch-name>
    

    Further notice: