Tuesday, November 22, 2022

Gitlab with Visual Studio 2022

Gitlab extension is not available for Visual Studio 2022.

VS Marketplace: GitLab Extension for Visual Studio
You can login any of your favorite GitLab servers and start your great job!
The GitLab Extension for Visual Studio provides GitLab integration in Visual Studio 2015/2017/2019.

In VS 2022, I created a new project and added to local git on user interface.

I also created an empty project on gitlab with the same name.

In gitlab project homepage it helps you what to do.


  cd existing_repo

  git remote add origin https://gitlab.com/git_username/project.git

  git branch -M main

  git push -uf origin main

I switched to command line and run commands above.

When you push the project to remote repository git shows a window for authentication.

I selected browser login and git command got credentials from browser.


C:\project>git push -uf origin main
info: please complete authentication in your browser...
Enumerating objects: 1718, done.
Counting objects: 100% (1718/1718), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1688/1688), done.
Writing objects: 100% (1718/1718), 35.76 MiB | 540.00 KiB/s, done.
Total 1718 (delta 470), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (470/470), done.
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To https://gitlab.com/git_username/project.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://gitlab.com/git_username/project.git'

The problem was VS created a branch named master, but in gitlab branch name was main.

I created a new branch named master on gitlab.

In command line switch to branch named master and push project to master.

C:\projectO>git branch -M master

C:\project>git push -uf origin master
Enumerating objects: 1718, done.
Counting objects: 100% (1718/1718), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1688/1688), done.
Writing objects: 100% (1718/1718), 35.76 MiB | 561.00 KiB/s, done.
Total 1718 (delta 470), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (470/470), done.
remote:
remote: To create a merge request for master, visit:
remote:   https://gitlab.com/git_username/project/-/merge_requests/new?merge_request%5Bsource_branch%5D=master
remote:
To https://gitlab.com/git_username/project.git

You can create a merge request in gitlab to push master branch into main.