Git for Programmers

5 (1 reviews total)
By Jesse Liberty
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Creating Your Repository

About this book

Git is the most popular version control system in the world. It allows developers to keep up with frequent code changes in a project, ensures there are no code conflicts between the developers, and reverts to an older version of code when required.

Git for Programmers comprehensively equips you with actionable insights on advanced Git concepts in an engaging and straightforward way. This book will help you gain expertise on Git with many practical use cases as you progress through the chapters.

The book begins with a quick history of Git and instructions on how to get it and install it, after which you’ll dive into the creation and cloning of your repository. As you progress through the book, you'll explore Git places, branching, and GUIs.

Once you understand the fundamentals, you’ll learn how to handle merge conflicts, rebase, amend, interactive rebase, and use the log. You’ll also explore important Git commands for managing your repository. Finally, the book concludes with coverage of bisect, blame, and several other problem handling techniques.

By the end of this book, you’ll be able to use Git with confidence; saving, sharing, and managing files as well as undoing mistakes and rewriting history.

Publication date:
June 2021
Publisher
Packt
Pages
264
ISBN
9781801075732

 

Branching, Places, and GUIs

In this chapter, you will learn about the crucial "five places" for Git: the work area, the index, the local repository, the remote repository, and the stash. You will see how to use each of these and how they work together.

Each concept will be illustrated with both code and screenshots, and most Git actions will be illustrated in the command line, Visual Studio, and a GUI (GitHub Desktop).

You'll also learn about creating branches, committing code, and how to move commits from the work area to the repository, and from the repository to the remote repository.

 

Five places

As a programmer I think of Git as divided into five places:

  1. The work area
  2. The index (staging area)
  3. The local repository
  4. The remote repository
  5. The stash

Let's begin by examining each of these in turn.

The work area

The work area is where your current files are. That is, if you were to open Windows Explorer and navigate to the directory you cloned to, you would see the version of the program you were currently working on. If you were to open Visual Studio 2019, these are the files that would be in the Solution Explorer. Again, the work area is where your current files are; if you open Visual Studio on your project, the files in the work area are what you will see. As you change branches (see below) the work area is updated with the appropriate files. This can be one of the hardest concepts in Git: when you change branches you change the files that are in your work area – that is, the files for that branch...

 

Branches

Using branches is critical to working with Git, not to mention to the success of your project. The idea is this: you have a "main" branch that you'll do your releases from. Each time code is added to the main branch it is checked and reviewed so that the main branch stays as clean as possible.

When you want to work on a bug or a feature, you create a new branch (often called a feature branch). This creates a copy of the code that is currently in the main branch. You can work on your feature branch without affecting the main branch at all. Once you are done, and all is working, you can then "merge" your feature branch into the main branch:

Figure 3.11: First feature branch

Notice that there is a pointer named Head. This points to whatever is in your work area. In this case, we've branched to Feature 1 and Head shows that the code for that feature branch is now in our work area.

That is a pretty good simplification of branching...

 

Summary

In this chapter, you have learned about the crucial "five places" for Git: the work area, the index, the local repository, the remote repository, and the stash. You have seen how to use each of these and how they work together.

Each concept was illustrated with both code and screenshots, and every Git action was illustrated in the command line, Visual Studio, and GitHub Desktop.

Finally, you have learned about creating branches, committing code, and how to move commits from the work area to the repository and from the repository to the remote repository.

 

Challenge

Create a private repository on GitHub named Contacts, and then clone that repository down to a folder on your disk. Using the command line, create a feature branch named Person and in that feature branch, create a person object with their name, age, and social security number. Create commits as you add each attribute. Review the log to see what you've created and then add these commits to the remote repository.

 

Answer

There is no one right way to do this, but we'll walk through a likely answer.

Task #1 – create a private repository on GitHub named Contacts. To do this, open a browser to Github.com and navigate to your repository page. Click on New and fill in the fields as shown here:

Figure 3.32: New repo on server

Notice that I've marked the repository as private. Click the Create Repository button.

Task #2 – clone that repository down to a folder on your disk.

On the same GitHub page, click on Code and click on the clipboard to copy the HTTPS or SSH path (if you have SSH you'll know it, otherwise choose HTTPS):

Figure 3.33: Copy address from server

Open the command line where you want your cloned repo and type:

git clone

and paste in the link you just copied:

git clone [email protected]:JesseLiberty/Contacts.git

You should see something like this:

Figure 3.34: Cloning from server to local repo

...

About the Author

  • Jesse Liberty

    Jesse Liberty is a senior programmer specializing in Xamarin.Forms and related tools, including Git. He has been working with Git for fourteen years and is considered an expert by the developer community.

    Liberty hosts the popular, Yet Another Podcast and he is the author of more than a dozen best-selling programming books. and one not yet published novel. Liberty is a Microsoft MVP and has worked as a Program Manager for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University.

    Browse publications by this author

Latest Reviews

(1 reviews total)
They're perfectly good books

Recommended For You

Git for Programmers
Unlock this book and the full library for FREE
Start free trial