如何将本地代码推送到 gitlab 上的新分支。需要具体步骤(看起来像一个重复的问题,但我需要一些具体步骤)

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

我是 gitlab 的新手。我有一个 git 实验室存储库,我被要求创建一个新分支并推送我拥有的代码(第一次,我在本地电脑上有代码)。我已经验证了这里的很多文章和示例,但没有确定我是否需要所有这些不同的步骤。

我刚刚 gitbash 安装在我的电脑上,我有远程存储库 url 并且有一个文件夹,我的代码需要添加到那个新分支

有人可以帮我设置步骤吗?我的理解是我应该在我的本地电脑上执行以下操作是否正确?

    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>

你需要做的是:

# 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>

另行通知:

  • 你应该先执行 git config 命令,如果你还没有这样做的话,但这不是必需的。
  • git init 将创建一个新的存储库,这不是您想要的。如果是gitlab,gitlab会帮你搞定。
  • 通常,如果您从正确的 URL.
  • 克隆,您不需要自己 运行 git remote add
  • 如果您的分支在远程存储库中不存在,您需要使用 git push --set-upstream,正如您在我的代码中看到的那样。