The tech giant, based in Washington state, is a heavyweight in terms of software whose source codes are not openly available or modifiable, exactly the counter of GitHub's philosophy.
Created in 2008, GitHub allows developers to cooperatively manage software and has more than 28 million users around the world.
"Microsoft is a developer-first company, and by joining forces with GitHub we strengthen our commitment to developer freedom, openness and innovation," Microsoft Chief Executive Satya Nadella said in a statement.
"We recognize the community responsibility we take on with this agreement and will do our best work to empower every developer to build, innovate and solve the world's most pressing challenges."
The veteran tech firm said it "will acquire GitHub for $7.5 billion in Microsoft stock."
Subject to customary closing conditions and regulatory review, the deal is expected to be finalised by the end of the year, Microsoft said in a statement on its website.
"GitHub will retain its developer-first ethos and will operate independently to provide an open platform for all developers in all industries," Microsoft said.
"Developers will continue to be able to use the programing languages, tools and operating systems of their choice for their projects -- and will still be able to deploy their code to any operating system, any cloud and any device."
Microsoft Corporate Vice President Nat Friedman, founder of Xamarin and an open source veteran, will become GitHub CEO.
GitHub's current Chief Executive, Chris Wanstrath, will move to Microsoft as a technical fellow to work on strategic software initiatives.
Writing on The GitHub Blog, Wanstrath said that he "could have never imagined" news of such a merger, when open source and business were considered as different "as oil and water" a decade ago.
But he said Microsoft and GitHub have already collaborated on projects, and "their vision for the future closely matches our own."
He said "both believe that software development needs to become easier, more accessible, more intelligent, and more open, so more people can become developers and existing developers can spend more time focusing on the unique problems they're trying to solve."
In April, Microsoft reported that its earnings rose 35 percent to $7.4 billion (roughly Rs. 49,600) in the fiscal third quarter, with revenue up 16 percent to $26.8 billion (roughly Rs. 1.8 lakh crores).
Earnings were lifted by gains in its core cloud computing operations for business.
Microsoft said the GitHub acquisition is expected to have a negative impact on 2019 earnings but positive beginning in 2020.