Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 "[email protected]"
   
    cd into directory where you have local code
    
    git clone https://gitlab.com/gitlab-tests/sample-project.git
    
    git init
    
    git remote add origin <[email protected]: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>
like image 804
asp Avatar asked Oct 23 '25 16:10

asp


1 Answers

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:

  • You should do the git config commands before, if you did not do that yet, but it is not required.
  • git init will create a new repository which is not what you want to do. In case of gitlab, it will be done for you by gitlab.
  • Usually, you not need to run git remote add yourself, if you clone from the correct URL.
  • You need to use git push --set-upstream if your branch does not exist on the remote repository as you can see in my code.
like image 112
toydarian Avatar answered Oct 26 '25 07:10

toydarian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!