Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Git for Programmers

You're reading from   Git for Programmers Master Git for effective implementation of version control for your programming projects

Arrow left icon
Product type Book
Published in Jun 2021
Publisher Packt
ISBN-13 9781801075732
Pages 264 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jesse Liberty Jesse Liberty
Author Profile Icon Jesse Liberty
Jesse Liberty
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Introduction 2. Creating Your Repository FREE CHAPTER 3. Branching, Places, and GUIs 4. Merging, Pull Requests, and Handling Merge Conflicts 5. Rebasing, Amend, and Cherry-Picking 6. Interactive Rebasing 7. Workflow, Notes, and Tags 8. Aliases 9. Using the Log 10. Important Git Commands and Metadata 11. Finding a Broken Commit: Bisect and Blame 12. Fixing Mistakes 13. Next Steps
14. Other Books You May Enjoy
15. Index

Starting at the command line

You can start the process at any of our repositories. Last time we started in the VisualStudio repository and then pulled the changes down to the CommandLine and GitDesktop repos. This time, let's start at the command line.

Open Visual Studio and point it to the project in your CommandLine directory. Just to be certain, right-click on Solution, select Open Folder in File Explorer, and make sure you are in the right directory.

To keep this example very simple, we'll just add another line to Program.cs:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        Console.WriteLine("I just added this in Visual Studio");
        Console.WriteLine("I just added this in the command line repo");
    }
}

Normally you would make many more changes before checking in, but again, this is a demo and we're more interested in using Git than we are in fussing with this silly program. Save all your files and at the command line get the status by entering:

git status

This will give you output that looks like this:

Figure 2.20: The command line indicating one file has been modified

The key piece of information is the modified file. That is just as it should be, as that is the file we modified. You can now add it to the index and then commit it:

git add ProGitForProgrammers/ProGitForProgrammers/Program.cs
git commit -m "Add writeline indicating we are in command line"

On the other hand, you can combine these two steps with the -a flag:

git commit -a -m "Add writeline indicating we are in command line"

You will want to draw a distinction between untracked files and modified files. Untracked files are outside of Git and cannot be manipulated inside Git until they are added; modified files are tracked by Git but have changed since the last commit.

If we are happy with the commit we've added, we can (optionally) push it to the server:

Figure 2.21: Pushing our commit to the remote repository

We'll want to do that because we want to share this code with the other programmers.

Pulling to GitHub Desktop

Switching to GitHub Desktop, we see that it already knows there is something to pull, as we saw last time. (If it doesn't, push the Fetch button, which will go to the server to see if there is anything to bring back.)

That's two repos that are identical, but the VisualStudio repo is not yet up to date. Let's return to Visual Studio in the VisualStudio folder.

Pulling to Visual Studio

Open the Git menu item, and select Pull. Watch your source code and see the third line pop into existence. Once again, the three local repositories and the remote repo are all in sync.

You have been reading a chapter from
Git for Programmers
Published in: Jun 2021
Publisher: Packt
ISBN-13: 9781801075732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime