Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Git for Programmers
Git for Programmers

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

eBook
AU$41.99 AU$60.99
Paperback
AU$75.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $24.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Git for Programmers

Creating Your Repository

In this chapter, you will learn how to create an account on GitHub, and how to create and clone your first repository so that you have a link between the repository on your computer and that on GitHub.

This chapter will cover:

  • Creating your repository
  • Git pull
  • Push me, pull you
  • Starting at the command line
  • Commits – best practices

We'll start by creating your GitHub repository.

Creating your repository

There are a number of different ways to create your repository. We'll cover creating a repository on GitHub and cloning it to your disk, as this is the most common way.

Creating your repository on GitHub first

Your first step is to register with GitHub. Go to http://github.com and click Sign Up. Fill in your username (it will tell you if the name is taken) and your email and it may ask you to verify that you are a human. Assuming you are, click Create Account.

Fill out their micro-survey and click Create Account. You will be asked to verify your email, and once you do, you'll see the (one-time) opening page asking what you want to do first. Choose Create a repository:

Figure 2.1: Getting started with GitHub

If you already have an account, sign in and press New Repository. You may not find this at first glance, in which case click the big plus sign in the corner.

Either way, you will be brought to the Create A New Repository page. The first job is to give your new repository a name. I'll use ProGitForProgrammers. Feel free to use any name you want as long as GitHub doesn't complain that the name is taken.

Now it is time to fill in the form:

Figure 2.2: Creating the repository

Start by entering a short description of your project. Next, and very importantly, choose whether you want this repository to be public (anyone can see it) or private (only people you invite can see it).

I strongly recommend checking Add a README file. This will be what is shown to users when they come to your repository. You can fix the file up later using Markdown.

Be sure to add a .gitignore file. This tells Git which files to ignore when checking your files into the repository. This can be very important so that you don't overwrite another programmer's metadata files. Click the dropdown and admire how many languages are supported; for C# I recommend you search for and choose Visual Studio.

If your repository is public, be certain to choose a license for the code. I chose the MIT License. You can learn more about this license at https://opensource.org/licenses/MIT.

That's it! You are ready to click Create repository. When you do, you'll be brought to the home page for your new GitHub repository:

Figure 2.3: Initial view of your repository

Notice that you have the three files you asked for, and that you can see a preview of the README as well as the description you entered.

Right now, this repository exists only on the server. You want to put a copy on your disk so that you can add code and use commands to keep them in sync. Therefore you will "clone" the repository; that is, you'll make an exact copy of the remote repository in your local repository.

How you will do this will depend on whether you are using the command line, Visual Studio, or a GUI.

Cloning to your computer – command line

Cloning to your local repository is easy. Open your terminal (or PowerShell) and change the directory to where you want the repository to go (in my case GitHub/the command line).

Switch back to your GitHub repo on GitHub.com, and see the green button in the upper right-hand corner marked Code. Click that button and a small dialog box will open. Choose HTTPS unless you know you have SSH (as I do). In either case, click on the clipboard icon to copy the address:

Figure 2.4: Copying the address of the repo

Return to the command line, enter git clone, and then paste in the address:

git clone git@github.com:JesseLiberty/ProGitForProgrammers.git

You should see something like this:

Figure 2.5: Cloning at the command line

Change the directory to ProGitForProgrammers and you'll see that the three files that were on the server are now here as well:

Figure 2.6: Files in the directory

Now let's take a look at how to do this in Visual Studio.

Cloning to your computer – visual studio

Go to your directory (in my case GitHub) and make a directory called VisualStudio.

Open Visual Studio with no project. Select File | Clone Repository. Fill in the fields and click Clone:

Figure 2.7: Cloning to your local repository using Visual Studio

A few seconds later you will see the three files, now shown in the Solution Explorer:

Figure 2.8: Cloned files in Visual Studio

There are a number of ways to clone from a GitHub repository to your own. One way is to use a dedicated GUI tool such as GitHub Desktop.

Cloning to your computer – GitHub for Desktop

Once again, return to your root directory (GitHub) and make a new directory. This time call it GitHubDesktop.

Now, return to GitHub and click Code:

Figure 2.9: Cloning directly through GitHub Desktop

Notice that one of the choices is Open with GitHub Desktop. Click on that. A dialog will open. The only field you need to fill in is the local path. Click Clone:

Figure 2.10: Cloning to GitHub Desktop using HTTP

Notice that GitHub Desktop wants the https URL for your repository.

You now have three copies of your original repository, each in its own directory: CommandLine, VisualStudio, and GitHubDesktop. These might represent three programmers working on the same solution, or various ways for one programmer to choose to clone their project.

Creating a project

We need a project. Using Visual Studio (or your favorite editor) create a project called ProGitForProgrammers in the CommandLine directory. When you are done, you should have the three original files and a folder for your program. In that folder will be the .sln file as well as a folder for the code.

Open the command line and navigate to the same directory. When you get there your command line should look something like this:

Figure 2.11: The command-line prompt

Look at the yellow, where you see +1 ~0 -0. The +1 means you've added a file or a directory; the ~0 indicates that no files have been modified; the -0 indicates that no files have been deleted. Let's see what was added. Enter:

git status

You should see something like this:

Figure 2.12: Untracked files

Git is telling you that you are on the branch main (the only branch for now) and that you have "untracked files" – that is, files that are in the directory but that are not being tracked by Git. If they are untracked, Git can't store them; in fact, Git knows nothing about them. Let's fix that. Enter these commands:

git add ProGitForProgrammers/
git commit -m "First commit – from command line"

add tells Git that this is a file it should pay attention to and commit brings it into the local repository.

Every commit must have a message, and if you don't provide one, you'll be prompted by Git to add one. Here I've added it by using the -m flag.

Once again, all this is happening locally and so GitHub doesn't know about it. We can fix that by pushing our commit up to the server:

git push

Now if you go to GitHub and refresh the page your project will be there. You can click your way down through the folders, and even into Program.cs, to see the code:

Figure 2.13: Viewing your code on GitHub

Notice in the upper left that it tells you that you are on the main branch. Next to that is the path to get to Program.cs. Below that is the message you added, and then the file itself.

Git pull

Having pushed your commits to the server, other developers may want to pull them to their own directory, to keep in sync.

Pulling down using GitHub Desktop

Having put the project up on the server, we can simply pull it down into the other locations. For example, open GitHub Desktop. It will tell you that there have been changes in the repository and helpfully offer a button for you to update your local repo.

If you open a file explorer and navigate to the GitHubDesktop directory, you'll see that there is now a replica of the files you pushed from the command line.

Pulling down to Visual Studio

Click on the Git menu and choose Pull. Visual Studio is updated with the code from the server. Now all three repositories are up to date. This is the heart of Git:

  • Save your files to a local repository
  • Push your files to the remote repository
  • Pull down any files that are on the remote repository but not on your local repository
Left arrow icon Right arrow icon

Key benefits

  • 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

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 30, 2021
Length: 264 pages
Edition : 1st
Language : English
ISBN-13 : 9781801075732
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $24.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 30, 2021
Length: 264 pages
Edition : 1st
Language : English
ISBN-13 : 9781801075732
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
AU$249.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
AU$349.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 254.97
C# 10 and .NET 6 – Modern Cross-Platform Development
AU$110.99
Git for Programmers
AU$75.99
Expert Python Programming – Fourth Edition
AU$67.99
Total AU$ 254.97 Stars icon

Table of Contents

15 Chapters
Introduction Chevron down icon Chevron up icon
Creating Your Repository Chevron down icon Chevron up icon
Branching, Places, and GUIs Chevron down icon Chevron up icon
Merging, Pull Requests, and Handling Merge Conflicts Chevron down icon Chevron up icon
Rebasing, Amend, and Cherry-Picking Chevron down icon Chevron up icon
Interactive Rebasing Chevron down icon Chevron up icon
Workflow, Notes, and Tags Chevron down icon Chevron up icon
Aliases Chevron down icon Chevron up icon
Using the Log Chevron down icon Chevron up icon
Important Git Commands and Metadata Chevron down icon Chevron up icon
Finding a Broken Commit: Bisect and Blame Chevron down icon Chevron up icon
Fixing Mistakes Chevron down icon Chevron up icon
Next Steps Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(13 Ratings)
5 star 46.2%
4 star 38.5%
3 star 0%
2 star 7.7%
1 star 7.7%
Filter icon Filter
Top Reviews

Filter reviews by




Matthew Emerick Jul 13, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The media could not be loaded. This is a great book for any developer. It's especially important for university students before they begin their career or those who have recently graduated. It covers everything from setting up Git to more advanced topics. A more detailed review can be found in the video.
Amazon Verified review Amazon
Ricardo Jorge Melo Jóia Oct 07, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Either you're a seasoned developer or just starting, git is already or will become one of the most used tools in your toolbox. Jesse's book is a cookbook with the most used commands that you might need and the "infamous" git rebase.The examples are clear and relatable to real world problems and they are shown using both the terminal and git integrations such as git for visual studio and github desktop! Either you use Windows, Linux or Mac, Jesse has your back! Examples on how to get it installed and running are provided.This book guides you through creating a repo, branching strategies, merging, pull requests and how to handle merge conflicts, how to amend commits, rebasing mentioned above and cherry-picking which you might and probably will need at some point.Jesse provides in his book plenty of other tips and tricks to make you a git ninja, deffo a book to have in your collection.
Amazon Verified review Amazon
dr t Aug 08, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Git is the most popular version control system in the world but, as the book says, it is complex.This book aims to demystify the workings of Git for programmers of all skill levels. As such, chapter one serves as an on-point, excellent, introduction that does a fantastic job of enthusing the reader for the journey ahead.One of the many good things about the book is that it very quickly gets the reader going with hands-on using Git; creating repos, pulling, pushing etc. Another good thing is that the book also shows how to perform Git tasks using the command line, GitHub Desktop and Visual Studio.The book is well-written, flows well and covers the basic aspects of Git but also goes into advanced topics such as blame, stash and rebase. Unlike other books this book doesn’t go on and on for the sake of improving the page count, it is only 265 pages long, but this is good because the authors concentrate on disseminating the important elements of Git to the reader as quickly as possible. There are are also challenges at the end of each chapter which allows readers to practice what they learnt. The final chapter “Fixing Mistakes” is especially useful and I can imagine will be welcomed by many.In summary, even if you already work with Git, I would still say get this book! Highly, highly recommended and a must-have for programmers of all skill levels.
Amazon Verified review Amazon
Justin Horner Aug 05, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As a fan of works by Jesse Liberty, I was stoked to find out about this new book. Git is essential to our practice as software engineers, and I believe everyone can learn something from this book, regardless of how much or little you've used Git prior.You'll go from creating a repository and cloning it from GitHub to branching, merging, stashing changes, rebasing, cherry-picking, and so much more. You'll also learn to add notes, tags, and various ways to view commits via log.Typically, you would expect Git topics to be taught solely from the command-line, but Jesse went the extra mile to include how to work with Git via command-line, Visual Studio 2019, and GitHub Desktop! The book ends with a large section dedicated to fixing common mistakes, which I'm sure I will keep handy for if (I mean when) these mistakes occur.Here are a few chapter highlights.- Rebasing, Amend, and Cherry-Picking- Workflow, Notes, and Tags- Using the Log- Finding a Broken Commit: Bisect and Blame- Fixing Mistakes
Amazon Verified review Amazon
hawkinflight Jul 15, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The media could not be loaded. I have used git in a team environment for about a year. I found this book very helpful. I am glad I discovered it. The topics covered are great, and are presented very well. The book is not very long, which means it focuses on the basics, and doesn't include a lot of distracting fluff. The author presents the material as though he is a very helpful, friendly, humble, collaborative co-worker. There are Challenges at the end of each chapter, with answers, which provide hands-on practice. Near the end of the book, there is a short, effective chapter on How To Fix Errors, such as, "committed to the wrong branch". I like the chapter on Workflow, and the inclusion in the book of Best Practices for Making Commits and How to Avoid Merge Conflicts. The author is an experienced git user and knows what the reader needs to learn. I like the book a lot and highly recommend it.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.