Git for Programmers
eBook - ePub

Git for Programmers

Master Git for effective implementation of version control for your programming projects

Jesse Liberty

  1. 264 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Git for Programmers

Master Git for effective implementation of version control for your programming projects

Jesse Liberty

Book details
Book preview
Table of contents
Citations

About This Book

Learn to track, branch, merge, and manage code revisions for real-world development scenarios

Key Features

  • Master Git and maintain your projects better through version control
  • Get to grips with Git's typical workflows, advanced functions, and their implementations
  • Learn the key Git commands to better manage your repository

Book Description

Whether you're looking for a book to deepen your understanding of Git or a refresher, this book is the ultimate guide to Git.

Git for Programmers comprehensively equips you with actionable insights on advanced Git concepts in an engaging and straightforward way. As you progress through the chapters, you'll gain expertise (and confidence) on Git with lots of practical use cases.

After a quick refresher on git history and installation, you'll dive straight into the creation and cloning of your repository. You'll explore Git places, branching, and GUIs to get familiar with the fundamentals. Then you'll learn how to handle merge conflicts, rebase, amend, interactive rebase, and use the log, as well as explore important Git commands for managing your repository.

The troubleshooting part of this Git book will include detailed instructions on how to bisect, blame, and several other problem handling techniques that will complete your newly acquired Git arsenal.

By the end of this book, you'll be using Git with confidence. Saving, sharing, managing files as well as undoing mistakes and basically rewriting history will be a breeze.

What you will learn

  • Create remote and local repositories and learn how to clone them
  • Understand the difference between local and remote repositories
  • Use, manage, and merge branches back into the main branch
  • Utilize tools to manage merge conflicts
  • Manage commits on your local machine through interactive rebasing
  • Use the log to gain control over all the data in your repository
  • Use bisect, blame, and other tools to undo Git mistakes

Who this book is for

If you have basic understanding of Git and want to strengthen your command over advanced techniques and navigate different functions, this book is for you. Knowing the fundamentals of Git will help you get the most out of this book, but beginners willing to invest some extra effort will be able to follow along as well.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Git for Programmers an online PDF/ePUB?
Yes, you can access Git for Programmers by Jesse Liberty in PDF and/or ePUB format, as well as other popular books in Computer Science & Software Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2021
ISBN
9781801076036
Edition
1

4

Merging, Pull Requests, and Handling Merge Conflicts

In this chapter, you will see how to merge branches, using different types of merges. You will also see how to handle merge conflicts and tools to make managing conflicts easier. You will learn about pull requests and the difference between a fast-forward merge and a "true" merge.
In this chapter, you will learn:
  • How to push a commit to the server
  • How to manage your commits with the command line, Visual Studio, and GitHub Desktop
  • How to merge into the main branch
  • What a pull request is
  • What merge conflicts are and how to resolve them
  • What a fast-forward merge is
  • What a true merge is
Let's start with an overview of merging.

Merging overview

If you are on a feature branch, and the feature is sufficiently complete and tested, you will want to merge your branch back into the main branch. Some organizations let you simply merge, others (most?) require that you create a Pull Request (PR). A PR says, essentially, "Please examine my code and if you think it is right, merge it into the main branch."
Having a second (or third) set of eyes on your code before merging can save a lot of headaches later on (see Chapter 12, Fixing Mistakes (Undo), on fixing mistakes).
Often, if you've been careful (see below) you will merge without a problem. From time to time, however, you will run into the dreaded merge conflict. You'll see below a couple ways to handle that conflict.

Book

You will remember from the previous chapter that we have a directory, C:\GitHub\VisualStudio\ProGitForProgrammers, that is the home of the Books application and that we've been editing in Visual Studio. Of course, we don't have to manage it in Visual Studio; we can use any of our tools. For example, I can open the terminal and change directories to the Books app:
Figure 4.1: Opening the terminal
Notice that it says I have one commit to push (as indicated by the up-pointing arrow followed by the 1). I must have forgotten to do so the last time I was working with this code. I don't want to just push it, however—who knows what's in there? There are a few ways to find out.

What's in that push?

From the command line, we can use the git show command:
Figure 4.2: Examining the push
There's a lot of information here. First, we see the author and the date. Then we see the message that was attached to this commit (Add properties). Next, Git does a diff (difference) between Book.cs and Book.cs naming the first one a and the second b. The one labeled a is Book.cs before this commit, the one labeled b is the new contents in this commit.
You may have noticed the line that says /dev/null. This indicates that a file is being compared against nothing, and thus everything is new.
The next line shows that /dev/null is being compared against file b (the new Book.cs file):
Figure 4.3: Comparing against dev/null
What follows are the changes. Deletions will be marked in red, modifications in green, and new code in yellow. (This display and these colors may depend on which shell you are using.) We see here that three using statements, a namespace, and the class Book were all added in this commit. Before we push it, let's see what we can learn in Visual Studio.

Visual Studio

Opening the same directory in Visual Studio and going to the Git view reveals, as we would expect, that we have one commit to push (outgoing):
Figure 4.4: Visual Studio showing one file to push
Before we push, let's see what's in that push. Clicking on 1 outgoing opens two windows. The Branches window shows us which branch we are on (Book):
Figure 4.5: Visual Studio showing contents of the local repository
The middle panel has the really cool info. It tells you the local (as opposed to origin) history of your branches:
Figure 4.6: Visual Studio showing commit history
We can see that main has five commits (reading newest to oldest) and that preceding the newest commit in main, we have an outgoing commit on the Book branch, whose message is Add properties. This is consistent with what we saw at the command line.
We can go further, and return to Solution Explorer. Because there is more to see in Program.cs (rather than Book.cs), right-click on Program.cs and choose Git and then History. ...

Table of contents

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

APA 6 Citation

Liberty, J. (2021). Git for Programmers (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/2708221/git-for-programmers-master-git-for-effective-implementation-of-version-control-for-your-programming-projects-pdf (Original work published 2021)

Chicago Citation

Liberty, Jesse. (2021) 2021. Git for Programmers. 1st ed. Packt Publishing. https://www.perlego.com/book/2708221/git-for-programmers-master-git-for-effective-implementation-of-version-control-for-your-programming-projects-pdf.

Harvard Citation

Liberty, J. (2021) Git for Programmers. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/2708221/git-for-programmers-master-git-for-effective-implementation-of-version-control-for-your-programming-projects-pdf (Accessed: 15 October 2022).

MLA 7 Citation

Liberty, Jesse. Git for Programmers. 1st ed. Packt Publishing, 2021. Web. 15 Oct. 2022.