Git for Programmers - Jesse Liberty - E-Book

Git for Programmers E-Book

Jesse Liberty

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 194

Veröffentlichungsjahr: 2021

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Git for Programmers

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

Jesse Liberty

BIRMINGHAM - MUMBAI

Git for Programmers

Copyright © 2021 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Producer: Ravit Jain

Acquisition Editor – Peer Reviews: Saby Dsilva

Content Development Editor: Alex Patterson

Technical Editor: Gaurav Gavas

Project Editor: Namrata Katare

Copy Editor: Safis Editing

Proofreader: Safis Editing

Indexer: Pratik Shirodkar

Presentation Designer: Ganesh Bhadwalkar

First published: June 2021

Production reference: 1240621

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-80107-573-2

www.packt.com

Foreword

Git is powerful, but complex. Many developers, myself included, use it daily but get pretty worried when we get outside of our comfort zones. After that, it's off to copy and paste internet search results and hope for the best… Or we might just clone to another folder and merge our changes by hand. Tell me I'm not the only one who does that from time to time, hoping it goes well so we don't have to quit our job and leave the country under the cover of night.

Part of the problem is that Git has a ridiculous number of rarely used commands, each with a long list of options. There are websites that will generate fake Git command documentation (search for "git man page generator" for an example), and it's hard to tell them apart from the real thing. Type the wrong Git command and you might lose your code changes, get stuck in vim (requiring a reboot to exit), upset your co-workers by pushing to the wrong branch, publish your passwords to GitHub, or even end up with a detached head (a truly terrifying prospect).

And, let's be honest, another part of the problem is that no one wants to look stupid. Everyone is expected to be a Git expert now, but most developers will admit (under conditions of strict anonymity) that they're not. So it can be a little embarrassing to ask for help.

You're in luck – this book is here to help!

I've had the honor of knowing Jesse for at least a decade. Maybe longer – both of our memories get fuzzy past the decade range. We've been co-workers, co-presenters, co-podcasters, co-authors, and also great friends. We both turn to each other for advice. Like many in the community, I've come to trust Jesse to break down complicated concepts to the essentials, and explain them in a way that just makes sense – and, quite often, surprise us by making the whole process a lot of fun.

Jesse has outdone himself with this book. It's well designed and expertly written. When he told me that he was writing a book on Git, I honestly thought, "Good luck with that…" because there's just so much to cover, but he's done an amazing job of figuring out the truly important concepts, focusing on them, and just nailing the explanations.

He's kept things "as simple as possible, but no simpler" as the quote (often attributed to Einstein) goes. There are some places where you need to know some Git internals to understand what you're doing, and Jesse explains those well, but he doesn't waste your attention with trivia.

A few of my favorite sections of the book:

The chapters on rebasing, amending, cherry-picking, and interactive rebasing really helped me more deeply understand what those commands are doing. I had some "cargo cult" commands that mostly seemed to work for me, but I couldn't have explained what they were actually doing. Jesse's diagrams and examples made a lot of lightbulbs go on for me there.

Some of the final chapters covering aliases, log, stash, and bisect gave me a lot of tips to work more effectively.

The Fixing Mistakes chapter is an amazing list of hands-on steps to get yourself out of common Git emergencies, and Jesse explains what the commands are doing. I absolutely see myself diving for this chapter the next time Git threatens to detach my head.

And finally, the challenges at the end of each chapter are an amazing resource. After learning the content in a chapter, these challenges (with detailed answers to follow) give you some fun puzzles to test your knowledge. I plan to revisit the challenges periodically to test my retention and read up on the solutions to fill in the spots I've forgotten.

Congratulations to you on picking a great book to take your Git knowledge to the next level, and to Jesse for proving me wrong and writing an amazing book about Git.

Jon Galloway

Senior Program Manager on the .NET Community Team

Contributors

About the author

Jesse Liberty is a Principal Software Engineer at StoryBoardThat. He is the author of two dozen programming books and a not-yet-published novel, as well as over two dozen training videos for Pluralsight, Packt, and Udemy.

Jesse is an international speaker and blogger (jesseliberty.com) and he is the host of the popular Yet Another Podcast, which you can access through his web site: jesseliberty.com/podcast.

He has been programming for 30 years (starting in 8086 Assembly and C!) and in C# since 2001. He is currently focused on .NET 5/6, C# 9, and related technology (Git, Azure DevOps, etc.).

When he is not programming… just kidding. He's always programming.

There are too many people to thank, but let's begin with the folks at Packt. Others, in no particular order, include: Namrata Katare, the people at StoryBoardThat, Mads Torgersen, Scott Guthrie, Scott Hanselman, Bill Wagner, Jared Parsons, Brent Laster, Jon Galloway, John Papa, Ward Bell, Adam Summers, Seth Weiss, Stacey Liberty, Skip Gilbrech, Dan Hurwitz, the folks in the Microsoft MVP program, the Albahari brothers, Douglas Crockford, Julie Lerman, Andrew Lock, Ken Henderson, and all the others I've forgotten.

About the reviewers

Wilson Mar has been trying to figure out Git and GitHub since 2010. In 2020 he helped to migrate 3,000 developers and their repositories to GitHub.com. He collects PROTIPs at his blog, https://wilsonmar.github.io.

Johannes Schindelin has been involved in the Git project since 2005. He invented the interactive rebase and is the maintainer of Git for Windows.

Preface

This is a book about Git, the most popular version control system in the world. I will explain what version control is and then lead you from introductory topics to quite advanced material. See chapter 1 for what background experience and software you will need (all free).

Who this book is for

This book is for programmers of any skill level. Full Stop.

Version control is essential to every programming project, as will be explained in chapter 1, and Git is the world's most popular version control system. The larger the team the more urgent it is that you use version control, but it can be essential for a single programmer as well.

What this book covers

Chapter 1 is the Introduction; you will learn about how the book is structured, what Git is, what version control systems are, where Git came from, and what tools are available for Git. You will also learn how to install Git on your computer.

In Chapter 2, Creating Your Repository, you will learn how to create a repository and clone it from GitHub. You will see the relationship between the repository on your disk and the one you've created on GitHub. A simple example program will be introduced.

In Chapter 3, Branching, Places, and GUIs, we'll cover the work area, the index, the repository, the remote repository, and the stash; what they are; and what they are for.

In Chapter 4, Merging, Pull Requests, and Handling Merge Conflicts, you'll learn about branching: a central concept in version control and one of Git's great strengths. Branching allows you to work on more than one thing at a time and keeps your release code clean. Git has especially fast branching.

In Chapter 5, Rebasing, Amend, and Cherry-Picking, you'll learn how to re-write history with Rebase and Amend. The very term Rebase makes some programmers quiver with fear, but I'll show you how to master this useful (and safe!) tool.

In Chapter 6, Interactive Rebasing, we'll look at how you can change the message associated with your commit, rearrange multiple commits, and even drop commits before pushing them to the server.

In Chapter 7, Workflow, Notes, and Tags, we examine the basic workflow for managing your repository, as well as how to add metadata to keep your repository clean and clear.

In Chapter 8, Aliases, we'll examine aliases and how they can save you a lot of work. Aliases can combine commands and all their flags to make your life much easier.

In Chapter 9, Using the Log, we will look at the very powerful log command. Log is much overlooked, and yet it can give you insight into every aspect of your current and past status of your projects.

In Chapter 10, Important Git Commands and Metadata, we'll go on to examine some very useful and important Git commands. These powerful statements can get you out of trouble when things appear to have gone wrong.

In Chapter 11, Finding a Broken Commit: Bisect and Blame, we'll look at a life-saving command, Bisect, which helps Git help you find where your program broke.

In Chapter 12, Fixing Mistakes, you'll learn how to get yourself out of difficulty if you make a mistake while using Git.

Finally, in Chapter 13, Next Steps, we'll take a quick look at additional resources.

To get the most out of this book

You will want to be somewhat comfortable with a programming language. Familiarity with C# is a big plus, but not required.You will need Git (free) installed on your computer, and it would be best also to have the latest free version (or better) of Visual Studio 2019. Finally, you'll want to download and install (free) GitHub Desktop. Thus, you do not have to spend any money to follow the examples in this book.A note to macOS users: All of the above applies to you as well, and I don't anticipate you having any additional issues.A note to Linux users: I don't work with Unix, but I strongly suspect that all of the above (except Visual Studio) will apply to you as well.

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781801075732_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "If you look at the log, git log --oneline, you should see all three commits: the one created when you cloned the repository and the two you created by hand."

A block of code is set as follows:

publicintAdd (int left, int right) { return left + right; } publicintSubtract (int left, int right) { return left - right; }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

publicintAdd (int left, int right) { return left + right; } public int Subtract (int left, int right) { return left - right; }

Any command-line input or output is written as follows:

git add . git commit -m "Add calculator class"

Bold: Indicates a new term, an important word, or words that you see on the screen. Words in menus or dialog boxes appear in the text like this. For example: "Select System info from the Administration panel."

Warnings or important notes appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email Packt at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you could report this to us. Please visit www.packtpub.com/support/errata, select your book, click on the Errata Submission Form link, and enter the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you've read Git for Programmers, we'd love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.

1

Introduction

In this chapter, we will cover the following topics:

A very brief history of version control and GitGetting and setting up Visual Studio 2019, GitHub Desktop, and your terminalGetting and installing GitConfiguring Git for Visual Studio, GitHub Desktop, and GitHub at the command line

Let's get started!

About this book

"Begin at the beginning," the King said gravely, "and go on till you come to the end, then stop." – Alice in Wonderland

In this book, we will cover Git from the very beginning all the way through to advanced topics. No experience with Git is expected. Though if you have been using Git, you'll probably want to skim the initial chapters. Git is arguably the most popular version control system in the world, but this raises the question: "What is version control?"

Version control

Before version control, I would code a bit and then when I became afraid of losing that code, I would make a backup of my directory. This is slow, inefficient, takes up a lot of disk space, and is very hard to share with others.

A Version Control System (VCS) does all this work for you (and more) and does so in a way that is fast, efficient, and takes up a minimum of disk space. One of the fastest and most efficient is Git, although there are others. This book will not spend a lot of time convincing you that Git is better than the others. First, the market has spoken, and Git prevails. Second, if you've purchased this book, you've already decided. And if you haven't already purchased this book, go do so. I'll wait here.

About the code examples

In order to demonstrate the use of Git, we need to have a small program that we can evolve. The code examples are given in C#, but they are so simple that you'll be able to follow them regardless of your experience with the language. For example, if you can figure out what this does, you're all set.

publicclassProgram { public voidPrintHello() { Console.WriteLine("Hello World!"); } }

This code declares a class (don't worry about what that is) named Program. Inside that class is a method (function) called PrintHello that prints Hello World to the console (your screen).

This is about as complex as it gets, and I'll explain each code snippet as we go.

Just a brief history

In July of 2005, after just a few month's work, Linus Torvalds, the genius behind Linux, released Git to meet his own needs, the needs of the Linux community, and eventually, the rest of us. The goal of Git was to be fast and efficient. It succeeded.

While most VCSes at the time were centralized (all the files were kept on a big server), Git uses a distributed system, in which everyone has their own repository. Technically, no central server is required for Git, although if you are working in a team, a central place for sharing code is convenient. But the huge difference is that with Git, the vast majority of your interactions with the VCS are local – right there on your disk.

Tools for working with Git

There are a number of easily confused terms (such as Git versus GitHub) and there are many tools for working with Git – from the command line to Graphical User Interface (GUI) tools. This section will review some of these options.

GitHub, et al.

There are many services that allow you to create shared "repositories" (the location of all the versions of your program). The most famous and popular are GitHub and Microsoft's Azure, as well as BitBucket and GitLab. Azure is a very powerful system for DevOps, while GitHub is a very straightforward way to host your program. We'll be using GitHub in this book. (Recently, Microsoft acquired GitHub for $7.5 billion in stock – and made a huge commitment to GitHub, open source and, of course, to Git.)

Key point: Git is the system we're covering in this book. GitHub is a central repository system for sharing code (we'll make this more specific later in the book) and GitHub Desktop is a GUI for working with Git.

GUIs and the command line

There are many ways in which to interact with Git. The principal ones are to work at the command line or to use a GUI. There are a wide number of GUIs. This book will focus on GitHub for Desktop and Visual Studio 2019, as well as the command line. In fact, one of the things that makes this book different is that all of Git will be explained using these three tools. This will give you a deep understanding of Git along with how the GUIs can help (and can hinder) your work.

There are many other excellent GUI tools, and the ecosystem of tools keeps evolving. That said, the ones we will use are both powerful and representative of what you get with nearly all the other GUI tools. In addition, these tools are well established and likely to stick around. Furthermore, if you learn one GUI and see how it relates to command-line instructions, you'll find it easy to use other GUIs.

We will look first at the command-line interface to Git. While there are some limitations in GUI tools, if it can be done with Git, it can be done at the command line, that is, all of Git's functionality is exposed at the command line. Furthermore, as has often been pointed out, understanding Git at the command line will make your use of a GUI much easier as you will know what is actually happening.

Does it help to understand how your car works? Not at all, until something goes wrong. Then you turn to the expert mechanic who can fix the problem. In Git, the expert is the programmer who knows the command line.

The command line

Out of the box, on Windows 10, the command line is available straight from your terminal. It gives you everything you need, but it is a bit ugly:

Figure 1.1: Git at the command line

It is hard to read, but we can make it pretty, so that it's easier on the eye.

Making the command line pretty

If you are using Git on Windows 10, you can make the command line prettier and more useful, as shown here:

Figure 1.2: Example of Pretty Print

To do this, download and install Power Shell. Then, follow Scott Hanselman's excellent directions at http://jliberty.me/PrettyGit.

A similar effect can be had on mac and Linux by following the directions at https://github.com/diogocavilha/fancy-git. There are other options available on the GitHub site that do pretty much the same thing.

Visual Studio 2019

Visual Studio 2019 has extensive support for Git built in. There is a separate menu specifically designed for Git, and Git information is integrated into the general UI:

Figure 1.3: Git menu in Visual Studio 2019

The first order of business is to get and set up Visual Studio.

Getting Visual Studio for free

All three of the products used for illustration purposes in this book (the command line, Visual Studio 2019, and GitHub Desktop) have free versions available. To obtain your copy of Visual Studio 2019, go to http://visualstudio.com and hover over the Visual Studio button. Select Community 2019. It will download and all you have to do is double-click on it and follow the instructions:

Figure 1.4: Obtaining Visual Studio

If you are using a Mac, there are some significant differences between how Visual Studio 2019 for Windows handles Git and how Visual Studio for Mac does. You will probably have no trouble following along, but you can always use the Terminal or GitHub Desktop.

GitHub Desktop

A very popular GUI, especially for use with GitHub, is GitHub Desktop. This cross-platform application makes working with Git extremely easy: anticipating what you might want to do and making that simple. The downside is that there are limitations, as there are for virtually every GUI.

Figure 1.5: GitHub Desktop

We will be using all three of these throughout the book: command line, Visual Studio 2019, and GitHub Desktop. As we proceed, the selections and Git commands shown above will make much more sense; for now, just get a feel for their appearance.

Getting Git

The very first thing you need to do is to install Git on your computer. The official Git site states that "even if Git is already installed on your computer, it is probably a good idea to reinstall to update to the latest version."

Getting Git on Windows

There are also a few ways in which to install Git on Windows. I recommend using the official build. The most recent one is available from the Git website: https://git-scm.com/download/win.

Another way to get Git, and to kill two birds with one stone, is to download and install GitHub Desktop. The installer will also install a command-line version of Git. You can get it at https://desktop.github.com/:

Figure 1.6: Obtaining GitHub Desktop

This book will show its demonstrations on Windows 10, using Git version 2.30.0.windows.2, but the examples should work with just about any version of Git.

Getting Git on a Mac

There are several ways to