On-premises GIT repository with Visual Studio Team Services Project - azure-devops

We have on-premises installed GIT. There we have our code repositories.
Is it possible to connect a repository from this on-premises instance to Visual studio team services project?
So they display under "Code" bar?
vsts
I need it hosted on premises, but see code changes/commits and other GIT stuff in VSTS project

No, it isn’t supported to display files or code of another repository under Code bar. You need to import that repository to the repository in VSTS. After that, you can push updates to VSTS repository if there are changes in your on-premises git repository.

You can't connect on premise GIT to VSTS. You may however use the VSTS rest APIs to push in code from your on premise GIT to VSTS.
Typically you will setup a hook/trigger on your on premise GIT repo in order to automate the replication process.

As others have said, you can have a "git hook", which is basically a git trigger to take action on some event. In this case, when code is pushed, to push it up to VSTS, but I assume you need to know the technical commands.
I had to do this the opposite way, and I did this quick and dirty. This pushes everything in one go, not each commit. This can also catch up a repo that is behind.
# Clone source repo (your local git repo)
git clone some_repo_path_goes_here
# I am skipping steps and assuming you are only syncing master branch.
# I have code to get all branches down before proceeding, but not posting it here.
# Assuming tags are on master branch..
# Get all tags
git fetch origin --tags
# Test to see if remote alias already exists
git ls-remote http://path_to_.visualstudio.com/org/project/_git/TargetRepoSameName
# Add a remote alias
git remote add any_name_123 http://path_to_.visualstudio.com/org/project/_git/TargetRepoSameName
# push local repo to 'any_name_123'
git push any_name_123 --all
# optional: delete all tags before attempting to push local tags
git push any_name_123 --delete `$(git tag -l)
# push local tags to remote repo
git push any_name_123 --tags
You can schedule this job if you would like. I have a PowerShell job to do this with a lot more functions to do pull down the branches.

Related

how do i automatically pull code from master to fork in azure repos using azure devOps pipeline

I was trying to pull the code from master to fork repository automatically through the azure pipeline. If any one know about this?
If you fork azure repo and want to automatically sync the Fork repo using VSTS Git, please follow below steps.
Supposed the url of original repo is https://dev.azure.com/{organization}/{project}/_git/test, and the url of forked repo is https://dev.azure.com/{organization}/{project}/_git/test_fork.
If you click "Clone" button in test repository, you will see below panel.
Clicking "Generate Git Credentials" button will show the following panel.
So we can use command git remote add upstream https://username:password#dev.azure.com/{organization}/{project}/_git/test to specify it as the upstream of test_fork repo in script.
Now creating a build pipeline using Microsoft-hosted Windows agents, setting the test_fork as the source.
Adding the Command Line task with below script.
git remote add upstream https://username:password#dev.azure.com/{organization}/{project}/_git/test
git fetch upstream
git rebase upstream/master
git push -f origin HEAD:master
Queuing a new build and it will succeed to sync the test_fork repo using VSTS Git.
You can also configure schedules for this pipeline. Now everything is done.
If you use GitHub repo, please refer to this thread for guidance.

Is it possible to push to a new remote on VSTS git?

Is it possible to create a new VSTS Git remote repository from a Git client?
Right now, I have to go to visualstudio.com first to create the new repository before adding and pushing it from PC git, using the url created in VSTS.
I tried the following:
git remote add brandnew xx.visualstudio.com/PROJECT/_git/qqq
git push brandnew --all
where I am the owner of xx.visualstudio.com and PROJECT is an existing project, but qqq does not exist.
The above gives me:
remote: TF401019: The Git repository with name or identifier qqq does not exist or you do not have permissions for the operation you are attempting.
fatal: repository 'https://xx.visualstudio.com/PROJECT/_git/qqq/' not found
Yes, it's possible.
Even you can not create a VSTS git repo by git command line by default, but you can achieve it via git hooks.
And for the reason why you can create and publish new VSTS git repo via VS, it that VS will create the new VSTS git repo by API not by git commands.
So if you want achieve this feature in git command line, you can use pre-push hook for assistance. Functions need to achieve in pre-push hook as below:
Detect if the remote repo https://xx.visualstudio.com/PROJECT/_git/qqq exist or not.
If the repo is not exist in VSTS, then create by REST API.

"Disconnect" forked Git repos in VSTS

In VSTS, I forked a repository to develop a separate product from the original repo.
When I make a PR from a branch into master in my forked repo, VSTS defaults to merging into the original repo. I have to be sure not to mistakenly merge into the original repo with every PR.
VSTS seems to think that I may want to merge changes from my forked repo into the original one. I have no plans to do so. How do tell VSTS to 'disconnect' my forked repo from the original?
No, there isn’t such feature in VSTS, there is the user voice that you can vote: Allow option of converting forks to repos
Work-around
As a work-around (in Visual Studio) until it's fixed:
Pull the repo.
Delete the remote repo.
Create a new remote repo with the same name.
Push the repo.
You might have to create a temporary remote repo (named temp if you'd like) in order to be able to delete the remote repo. You can delete the temporary remote repo, named "temp", after you've pushed to the new remote repo.
You'll most-likely break anything (pull requests, work items) linked to the repo and also have to re-apply any policies and security stuff.
repo = the faulty fork in VSTS.
temporary remote repo = a temporary tepo created if you cant delete the fork repo.
new remote repo = the new repo to be used instead of the fork.
It seems like one should disconnect the old remote origin and set the upstream before pushing. Maybe the push with the --set-upstream overwrites that?
Here's what I'm did:
git clone ACCOUNT#vs-ssh.visualstudio.com:v3/ACCOUNT/PROJECT/FORK-NAME NEW-NAME
git remote rm origin
Create the new repository on VSTS for NEW-NAME
git push --set-upstream ACCOUNT#vs-ssh.visualstudio.com:v3/ACCOUNT/PROJECT/NEW-NAME develop
This worked for me to change from a fork to a repo. I can see the full history and open changesets from the web viewer. I did this in Azure DevOps (formerly Visual Studio Team Services AKA VSTS). I did not test against GitHub.
Pull requests and pushes are lost. Commits are still linked
I'm not sure if you need to do the push multiple times for different branches. My fork only has a develop branch anyway. I only create a master branch so git flow doesn't complain.

How to import a Bitbucket repo into my Github account?

I have a large repo on Bitbucket that I need to import into Github. The repo contains a ton of historical information that I do not want to lose. I've googled around and can't find a definitive resource which explains the process. Am I missing something obvious?
Thanks in advance!
Am I missing something obvious?
In many cases simply adding a new remote and pushing to it will do what you want:
git remote add github git#github.com:user/repo.git
git push github master
This will push your master branch to GitHub. You can push other branches in a similar manner, and you can push your tags with git push github --tags.
A more comprehensive option is to use the --mirror option, e.g.
# Add the github remote as above, then
git push --mirror github
From the documentation:
--mirror
Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not limited to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the remote repository. Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and deleted refs will be removed from the remote end. This is the default if the configuration option remote.<remote>.mirror is set.
Note that this implies --force, so be careful with it. Some users like to do this from a new bare clone of the remote (i.e. first do git clone --bare git#bitbucket.com:user/repo.git, then do the rest of the steps from the newly created bare repository).

Integrating Github code to TFS - auto check-in

We are using one of the project from Github. We need to check-in code of this project in our TFS.
We need to automate this process. Else everyday we need to download the code and then check-in.
Is there some plugin or some tool to automate this?
If you are using a TFVC repository in TFS then you'll probably want to build some scripts and a process around Git-TF to help automate some of this work.
If you are using a Git repository in TFS then you can create set up two remotes in a local Git repository, i.e
git remote add upstream https://github.com/foo/bar.git
git remote add origin https://tfsserver/DefaultCollection/_git/bar
And then simply do a git pull upstream master followed by a git push origin master assuming that master is the branch that you want to keep in sync.
With either version control system, you probably want to keep a branch in version control in your TFS repository to match what is in your upstream GitHub project so that you can easily see change coming in the one place and then handle your merges inside your local repository.