Learning Git A Hands-On and Visual Guide to the Basics of Git (Anna Skoulikari) (Z-Library)
Author: Unknown Author
技术
No Description
📄 File Format:
PDF
💾 File Size:
18.6 MB
231
Views
83
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
Learning Git A Hands-On and Visual Guide to the Basics of Git Anna Skoulikari
📄 Page
3
Learning Git by Anna Skoulikari Copyright © 2023 Anna Skoulikari. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Development Editor: Shira Evans Acquisition Editor: Melissa Duffield Production Editor: Christopher Faucher Copyeditor: Rachel Head Proofreader: Piper Editorial Consulting, LLC Indexer: nSight, Inc. Cover Designer: Karen Montgomery Interior Designers: Ron Bilodeau and Monica Kamsvaag Illustrator: Kate Dullea May 2023: First Edition Revision History for the First Edition: 2023-05-16 First Release See https://www.oreilly.com/catalog/errata.csp?isbn=0636920728078 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Git and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. Although the publisher and author have used reasonable care in preparing this book, the information it contains is distributed “as is” and without warranties of any kind. This book is not intended as legal or financial advice, and not all of the recommendations may be suitable for your situation. Professional legal and financial advisors should be consulted, as needed. Neither the publisher nor the author shall be liable for any costs, expenses, or damages resulting from use of or reliance on the information contained in this book. 978-1-098-13391-7 [LSI]
📄 Page
4
I dedicate this book to my parents. Mom, Dad, thank you for all your support through this crazy journey we call life.
📄 Page
5
[ contents ] [ Preface ] xi Chapter 1. Git and the Command Line 1 What Is Git? 1 The Graphical User Interface and the Command Line 2 Opening a Command Line Window 3 Executing Commands in the Command Line 5 Installing Git 7 Command Options and Arguments 7 Clearing the Command Line 8 Opening the Filesystem Window 9 Working with Directories 9 Closing the Command Line 16 Setting Git Configurations 17 Preparing a Text Editor 19 Integrated Terminals 19 Summary 20 Chapter 2. Local Repositories 21 Current Setup 21 Introducing Repositories 21 Initializing a Local Repository 22 The Areas of Git 26 Adding a File to a Git Project 30 Summary 31
📄 Page
6
Chapter 3. Making a Commit 33 Current Setup 33 Why Do We Make Commits? 34 The Two Steps to Make a Commit 34 Viewing a List of Commits 40 Summary 42 Chapter 4. Branches 43 State of the Local Repository 43 Why Do We Use Branches? 44 Unmodified and Modified Files 48 Making Commits on a Branch 51 Creating a Branch 53 What Is HEAD? 55 Switching Branches 57 Working on a Separate Branch 61 Summary 62 Chapter 5. Merging 63 State of the Local Repository 63 Introducing Merging 64 Types of Merges 64 Doing a Fast-Forward Merge 70 Checking Out Commits 80 Creating a Branch and Switching onto It in One Go 86 Summary 87 Chapter 6. Hosting Services and Authentication 89
📄 Page
7
Hosting Services and Remote Repositories 90 Setting Up a Hosting Service Account 90 Setting Up Authentication Credentials 91 Summary 93 Chapter 7. Creating and Pushing to a Remote Repository 95 State of the Local Repository 95 The Two Ways to Start Work on a Git Project 96 The Interaction Between Local and Remote Repositories 97 Why Do We Use Remote Repositories? 98 Creating a Remote Repository with Data 99 Working on a Remote Repository Directly on a Hosting Service 112 Summary 112 Chapter 8. Cloning and Fetching 113 State of the Local and Remote Repositories 113 Cloning a Remote Repository 114 Deleting Branches 122 Git Collaboration and Branches 125 Incorporating Changes from the Remote Repository 132 Deleting Branches (Continued) 137 Summary 139 Chapter 9. Three-Way Merges 141 State of the Local and Remote Repositories 141 Why Are Three-Way Merges Important? 143 Setting Up a Three-Way Merge Scenario 146
📄 Page
8
Defining Upstream Branches 147 Editing the Same File Multiple Times Between Commits 151 Working at the Same Time as Others on Different Files 158 Three-Way Merge in Practice 161 Pulling Changes from a Remote Repository 167 State of the Local and Remote Repositories 170 Summary 171 Chapter 10. Merge Conflicts 173 State of the Local and Remote Repositories 173 Introducing Merge Conflicts 175 How to Resolve Merge Conflicts 176 Setting Up a Merge Conflict Scenario 177 The Merge Conflict Resolution Process 182 Resolving Merge Conflicts in Practice 185 Staying Up to Date with a Remote Repository 187 Syncing the Repositories 188 State of the Local and Remote Repositories 191 Summary 191 Chapter 11. Rebasing 193 State of the Local and Remote Repositories 193 Integrating Changes in Git 195 Why Is Rebasing Helpful? 196 Setting Up the Rebasing Example 199 Unstaging and Staging Files 201 Preparing to Rebase 211
📄 Page
9
The Five Stages of the Rebase Process 213 Rebasing and Merge Conflicts 217 Rebasing a Branch in Practice 218 The Golden Rule of Rebasing 223 Syncing the Repositories 226 State of the Local and Remote Repositories 229 Summary 229 Chapter 12. Pull Requests (Merge Requests) 231 State of the Local and Remote Repositories 231 Introducing Pull Requests 233 Hosting Service Specifics 234 Why Use Pull Requests? 235 Understanding How Pull Requests Are Merged 237 Preparing to Make a Pull Request 240 An Easier Way to Define Upstream Branches 242 Creating a Pull Request on a Hosting Service 245 Reviewing and Approving a Pull Request 246 Merging a Pull Request 248 Deleting Remote Branches 249 Syncing the Local Repositories and Cleaning Up 250 State of the Local and Remote Repositories 254 Summary 255 [ Epilogue ] 257 Appendix A: Chapter Prerequisites 259
📄 Page
10
Appendix B: Command Quick Reference 283 Appendix C: Visual Language Reference 287 [ Index ] 291
📄 Page
11
[ Preface ] I never thought I would write a book teaching Git. But through a fortunate series of events, I found myself with a creative idea for how I could teach this technology in a simple way. My journey started when I attended a coding bootcamp to learn web development. The teachers at the bootcamp briefly introduced Git to the students, but given that all our projects were done individually, we didn’t have to use it extensively. After the coding bootcamp, I got a job as a junior frontend developer working on a website in a big company. My real Git learning journey began on the first day of my new job. In those first months, working as part of a team in a large company, I realized I was terrified of it. Any time I had to do something that seemed remotely complicated using Git, I thought I was going to destroy the repository or seriously mess something up. To be able to work properly with my coworkers, I decided to teach myself the ins and outs of Git. But as I read through various online resources, it quickly became clear to me that most of the material out there was not designed for people who were just starting out. Once I understood the basics, an idea started to form in my mind of how I could teach this technology in a simpler way using visuals and colors. I ended up creating an online course that I uploaded to the web. While working on the course, in the back of my mind, I thought to myself that someday I might write a book about it as well. I got a lot of positive feedback about the course, and finally, in the summer of 2021, I decided it was time to get started on that project. The book you’re reading now is the product of that decision, and I hope it helps you on your Git learning journey!
📄 Page
12
Who This Book Is For This book is for anyone who wants to learn the basics of how Git works. It is especially designed for individuals that are just getting started learning technical skills, or that work in nontechnical roles but need to use Git to collaborate with their technical counterparts. Some examples of individuals that may benefit from this book include (but are not limited to) coding bootcamp students, computer science students, technical writers, product managers, designers, junior developers, data scientists, and self-taught programmers. The book is written for people with no experience using Git, as well as those with a bit of experience using Git. If you have no experience with Git, that’s not a problem since this book starts from zero. We’ll begin with installing Git and how to use the command line, and build from there. If you already have some experience using Git or the command line, the first chapter may be a bit of review. However, I encourage you not to skip it because it sets up the Rainbow project that you will be using throughout the rest of the book.
📄 Page
13
Using This Book This book is a hands-on learning experience, where you will be carrying out exercises on your computer while learning the basic concepts of Git. Throughout the book, you will come across two projects: the Rainbow project and the Book project. The Rainbow project is a hands-on project that you will work on by going through the exercises in the book. It is a simplified project that is intended only for learning purposes. The Book project is an imaginary project that I’ll use to demonstrate how certain features of Git might be used for a more realistic project. Let’s take a closer look at each of these, and at the way the book is structured. [ NOTE ] Don’t worry if this preface contains terminology that you are not yet familiar with, like repository and commit. I’ll explain all of these concepts in the chapters to come. THE RAINBOW PROJECT To learn the basics of Git, throughout this book you are going to be working on the Rainbow project. To follow along, you should read the book from Chapter 1 to Chapter 12 in a linear fashion, and you should complete each and every exercise on your computer. For example, exercises in Chapter 4 will assume that you’ve already completed the exercises in Chapters 1, 2, and 3. Repositories On a basic level, a repository is a copy of a Git project. At first, you will create one local repository called rainbow to work on the Rainbow project. Subsequently (in Chapter 7), you will create a remote repository called rainbow-remote. And finally, in Chapter 8, you will simulate that you are collaborating with a friend on the Rainbow project, and you will create a second local repository called friend-rainbow. From Chapter 8 onward, whenever a reference is made to your “friend” doing something, you will have to carry out the action in the friend-rainbow repository. [ NOTE ] When I refer to the Rainbow project with a capital R, I’m referring to the entire project that starts off with one
📄 Page
14
repository and in the end contains three repositories. When I refer to the rainbow project directory or the rainbow repository (with a lowercase r), I’m referring to the specific local repository that is part of the Rainbow project. Commits In the Rainbow project, you are going to create and edit files to list the colors of the rainbow, and some colors that are not part of the rainbow. This is not meant to be a realistic example of a project version controlled by Git. It’s a simplified project that enables you to focus on learning instead of building something complicated. Throughout the book, I’ll use diagrams to illustrate what is happening in the Rainbow project. Every time you add a color to the Rainbow project, you are going to make a commit in a repository. A commit basically represents a version of your project. In the diagrams, the commit will be represented by a circle in the color you added, and we will use the name of the color as the name of the commit as well. For example, the first color that you will add to your Rainbow project is red, so the circle that will represent that commit will be colored red, and we will refer to it as the red commit. To make the book accessible to readers with color vision deficiency, I’ll include the name of the commit (or an abbreviation of the name) in the diagrams. See Figure P-1 for an example of the red commit. Table P-1 contains a list of all the commits you will make in the Rainbow project in this book, showing their full names and their abbreviations. TA B L E P - 1 . Full list of commits made in the Rainbow project COMMIT FULL NAME COMMIT ABBREVIATION F I G U R E P - 1 An example of a commit with its full name
📄 Page
15
red R orange O yellow Y green G blue B brown Br merge commit 1 M1 indigo I violet V merge commit 2 M2 gray Gr black Bl rainbow Ra pink P merge commit 3 M3 Figure P-2 is a diagram of all the commits. APPENDIXES While the book is designed to be read from Chapter 1 to Chapter 12 in a linear way, there may be some situations in which you want or need to start off from a specific chapter. For example: • You have gone through the exercises in the entire book once and you want to review from a F I G U R E P - 2 The 15 commits you will make in the Rainbow project by the end of this book
📄 Page
16
specific chapter onward. • Something went wrong in the Rainbow project in a previous chapter that you were not able to troubleshoot, and you want to continue from a new chapter afresh. In this case, you can use the instructions in Appendix A to re-create what the Rainbow project should look like at the start of the chapter you want to begin from. Appendix B contains a quick-reference guide to the commands introduced in each chapter. Appendix C is a guide to the visual language used in the diagrams in the book.
📄 Page
17
THE BOOK PROJECT The Book project is an imaginary project that I will use to demonstrate how Git can be used for more realistic projects. For this project, we’ll pretend that I’m writing a book and I want to use Git to version control the files. The book will consist of 10 chapters represented by 10 text files, one for each chapter: chapter_one.txt, chapter_two.txt, and so on. At times, I will also simulate what it would be like to work on the Book project with a coauthor and/or an editor. These discussions will take place in Example Book Project sections. You will not actively work on or build the Book project. It will only be used to provide further examples and descriptions of how certain features of Git are used. Apart from the Example Book Project sections in the book, you will also come across some other sections. We’ll look at those next. SECTIONS IN THE BOOK Here’s a quick guide to the different types of sections you will encounter in this book: Example Book Project As mentioned previously, Example Book Project sections provide additional context and examples about using Git features and commands, based on the Book project. Follow Along Follow Along sections present numbered lists of steps that you should carry out on your computer. If a step includes a command in bold, then you must enter and execute that command in the command line. Sample output is provided for all commands that produce output. This output is based on the Rainbow project that I worked on while creating this book; it was generated by the macOS operating system, but the output of Git commands should be the same on Microsoft Windows. When there are significant differences between the output of a command on Microsoft Windows and macOS, this is stated in the text. Save the Command Save the Command sections introduce useful commands, some of which you will use in the Follow Along sections. A full list of all important commands grouped by chapter is also available as a reference in Appendix B. Visualize It Visualize It sections show diagrams of what is happening in the Rainbow project. Two
📄 Page
18
important diagrams that are used in these sections (as well as Figures) are the Git Diagram and the Repository Diagram. I’ll introduce the Git Diagram in Chapter 2, and we will start using the Repository Diagram in Chapter 4. Every diagram in the book is built step by step, with explanations in the text. A summary of the visual language used throughout this book is also available as a reference in Appendix C. Note Note sections provide useful information related to the material in the text. THE LEARNING GIT REPOSITORY While I aim to provide most of the information that you need in order to go through the Learning Git experience within the book itself, there are some technologies and processes that change too often to document in the book. I’ve created the public Learning Git repository (https://github.com/gitlearningjourney/learning-git) on GitHub to provide up-to-date information about these technologies and processes. Among other things, the repository contains: • Information about downloading Git • Links to resources related to working with hosting services • Information about setting up HTTPS or SSH access to remote repositories Throughout the book, I will mention when you may need to consult the Learning Git repository for more guidance.
📄 Page
19
WHAT THIS BOOK ISN’T This is not a reference book. I won’t cover every single Git command (trust me, there are a lot!). It also isn’t an advanced guide to Git. There are a lot of features of Git that won’t be discussed in this book because they are not necessary to carry out the basic actions I want to teach you to perform. I have been very selective in what I have included in the book. My aim is to give you a clear mental model of the basics of Git so that you can go on to learn about any additional features you need with a solid foundation in place. This book also won’t tell you what your Git workflow should be or how you should use the features of Git. As much as possible, I try to stay away from giving any opinions on these matters, and instead I focus on teaching you how the tool works. Depending on your individual context and preferences, the tool can be used in different ways. Where possible, this book aims not to be prescriptive. For example, you are allowed to use whichever text editor or hosting service you prefer to carry out the exercises in the book.
📄 Page
20
CHAPTER SUMMARY The book is split into two logical parts. In the first part of the book (Chapters 1 through 5), you will learn about working with local repositories on your computer. In Chapters 6 through 12, you will additionally learn about working with hosting services and remote repositories. Following is a brief summary of what we’ll cover in each chapter: • In Chapter 1, you’ll get ready to work on a project using Git by installing it, learning some command line basics, preparing Git settings, making one of the project directories you will use throughout the rest of the book, and preparing a text editor. • In Chapter 2, you’ll turn your project directory into a Git repository. I’ll introduce the Git Diagram that represents the different areas of Git, including the working directory, staging area, commit history, and local repository. At the end of the chapter you’ll create the first file in your project directory. • In Chapter 3, you’ll learn about and carry out the two main steps to make the first commit in your local repository. • In Chapter 4, you’ll learn about branches: what they are, how to make them, how to switch branches, and how to identify which branch you’re on. • In Chapter 5, you’ll learn about the two types of merges and carry out a fast-forward merge. • In Chapter 6, you’ll prepare to work with remote repositories by choosing a hosting service and setting up authentication details to connect to these repositories over either HTTPS or SSH. • In Chapter 7, we’ll discuss the different ways to work with local and remote repositories. You’ll learn how to create a remote repository and upload data to it. • In Chapter 8, we’ll start simulating what it’s like to work with others on a Git project. In order to do so, you’ll create a second local repository; you’ll pretend that it belongs to a friend of yours who will be helping you on the Rainbow project and is on their computer. In the process, you’ll learn about cloning remote repositories and fetching data. • In Chapter 9, you’ll carry out a three-way merge and learn about the difference between fetching data and pulling data. • In Chapter 10, we’ll go over an example of resolving merge conflicts during a three-way merge. • In Chapter 11, you’ll learn about rebasing. This is an alternative way of incorporating changes from one branch to another, as opposed to merging. • In Chapter 12, you’ll learn about pull requests (also known as merge requests) and how they
The above is a preview of the first 20 pages. Register to read the complete e-book.