An introduction to Git for beginners
What is Git? Are Git and GitHub the same? How do you get started with Git? What are the basic Git commands a developer should know?
If you’re looking for answers to these questions, Kedasha Kerr has your back. A developer advocate at GitHub, Kedasha breaks down the basics of Git in the video below and in her blog post that covers Git for beginners.?
To build your familiarity with Git, we’ll use this issue of Branching Out_ to recap the fundamentals covered by Kedasha and compile resources that are readily accessible to you. You’ll learn how to install and configure GIt as well as the most common Git commands to run while using the most widely used version control system (VCS) in the world.?
1?? Let’s start by answering this popular question: Are Git and GitHub the same? ??
No, they’re not.?
Git is a VCS that tracks file changes. GitHub is a platform where developers can collaborate and store their Git repository in the cloud. The repository acts like a central hub for a project’s files, documentation, and version history. ( ??Friendly reminder that this difference only scratches the surface. Developers can do much more on GitHub, which we’ll cover below.)?
To understand how a VCS like Git works, it might help to understand how it doesn’t work.?
As an example, let’s look at how you save your resume files.
On your computer, you might have separate files labeled resume, resumev2, resumev4, etc. With version control, you wouldn’t need separate files for each change to your resume. You could keep one resume file because Git, the VCS, would track all the changes for you. So, you could have one file where you’d be able to see the latest version of your resume by default but choose to see, in that same file, your resume’s history, previous versions, and all the changes you’ve made over time.
Why, as a software developer, should you use Git as a VCS to track file changes??
?With Git, you can:?
You can see why Git is valuable for developers who want to virtually collaborate on a project. Its benefits apply to open source communities and enterprise teams alike.
?? Fun fact: Git was released in 2005 and still a new open source VCS when GitHub was founded in 2008. In fact, only 2.4% of companies adopted Git according to a 2009 Eclipse community survey. As of 2023, 93% of developers are using Git, according to Stack Overflow.?
? Want to take a walk down memory lane? Check out this old Stack Exchange thread about Git and Subversion or this history of Git.?
Here are seven Git terms to know:
So, what does GitHub have to do with Git?
You can use GitHub to host your Git repositories. But if that’s all you use it for, you’re missing out on everything else the platform has to offer.?
That’s not to say cloud storage isn’t a big deal. It is. By hosting your code on GitHub, you won’t lose your work if something happens to your local machine. All we’re saying is you can do more than that on a platform built specifically for developers.??
In addition to storing your code, here’s everything else you can do on GitHub:
In other words, GitHub is more than just a GUI for Git.?
?? To learn more about how Git and GitHub work together, read and complete:
2?? How do you get started with Git? Now that you know the purpose and value of Git for developers, here’s how you can start using it today.?
(?? For a visual walk-through of the steps below, skip to this section in Kedasha’s video.)
To install Git on MacOS, here are the four steps you need to follow:
?? Note: While macOS comes with a preinstalled version of Git, you’ll still want to download it to ensure you have the most up-to-date version.
To install Git on Windows 11, here are the steps you need to follow:
-If you’re using a Windows machine, click on the Windows icon on the download page. This will give you the most recent version of Git.
-Once you have that file on your machine, double-click it and follow the onscreen wizard prompts:
-Click the “Next” button for the following: accept the terms, the location to save Git, and the default selections.
-Reset the default branch name to “main” as that’s the new convention.
-Click the “Next” button to accept the recommended path, the bundled OpenSSH program, and for all the other options.
-Click the “Install” button.
-Once Git is installed on the machine, click the “Finish” button and open your terminal.
-Run the command git and you should see a list of all the commands available.
领英推荐
To configure Git on your machine, follow these four steps:
You’re all set up! ??????
?If you have questions or feedback, pop it in the GitHub Community thread, and we’ll be sure to respond!
3?? What are some good-to-know Git commands? Now that you have Git configured on your machine, you’re ready to start running commands.?
We have a top 12 list of Git commands every developer must know and a list of basic Git commands. We’ll share a few below.?
Congratulations, you already ran this one when you set up Git on your machine!
This command initializes a new Git repository.?
Before you run this command, you need to create a folder, or directory. Create a new folder with the command mkdir git-practice and then run cd git-practice to go into the folder you just created. Open the folder in your code editor.
From here, you can run the git init command to initiate a new Git repository so that Git tracks all changes made in this directory.
Run this command to see all tracked changes to your directory.?
You can see what files have been added, deleted, or modified. If you run this command in a directory with no files, it should return “nothing to commit.”
For more practice, create a new file in your sample folder by running touch hello.md and then run git status again. Now, you should see that you have an untracked file, hello.md.
An untracked file is one that hasn’t been added to the staging area. To add files to the staging area, you can use the git add command. ??
There are a few ways to use this command. For example, you can use git add . to add all files to the staging area from the working directory or use git add filename to add a specific file to the staging area.
Try creating a new file called learning.py and another file called waiting.py.?
Now, run git status so you can see your changes. Then, run git add learning.py to add this file to the staging area.?
If you run git status again, you’ll see that learning.py is in the staging area and waiting.py is untracked and in the working directory.
If you make additional changes to the tracked files in your directory, you’ll need to run git add again for Git to record those changes.?
To practice tracking new changes, add the code print (“I'm learning git") to learning.py.?
Then, run git status in the terminal. You’ll see that the learning.py file has been modified and has untracked changes.?
Run git add learning.py to keep track of these newer changes.
Currently, learning.py and hello.md are tracked, while waiting.py is untracked.
Run git commit -m "initial commit" and then run git status afterward.
You’ll see that you have “2 files changed, 1 insertion in git” because you just added two new files and one of the files has one line of code. You’lll also see that you have an untracked file—waiting.py—because that file was never added to the staging area.
If you add new changes to the learning.py file, you can add both files to the staging area along with any other changes you make by running a git add. git commit -m "add waiting file and new function" command.
Bookmark the GitHub for Beginners page on the GitHub blog to stay updated on our latest posts and keep it moving on your Git journey! ??
If you want even more hands-on learning and to interact with the GitHub community in person, “git” your boss to send you to Universe this fall. ?? Not sure how? We have you covered. Check out our cheat sheet now!?
More GitHub goodness:?
?? Repost this newsletter to your network.
? This newsletter was written by Nicole Choi and produced by Gwen Davis. ?
--
1 个月Les explication en fran?ais pourra aider des millions d'utilisateur
Full Stack Developer | Javascript, Java, Go, Rust | React.js, Spring Boot | Passionate about building scalable and performant software
3 个月Excellent resource! It is so important to understand the tools that we work with on a daily basis.
Student at Northern Virginia Community College | Student Government Association Secretary | Black Student Union President | Computer Science Student | Curious Learner | Student Ambassador | Agile Learner
5 个月This is a helpful resource as I am a beginner on GitHub. Kudos to you guys for always being awesome.
First Year Computer Science Major at University of Colorado Boulder
5 个月As a beginner this will be very useful towards my goals!
I'm a job seeker who is always on the lookout for my next opportunity. I'm excited to start this new phase in my career!
6 个月Thanks for the info in this post. I'm a newbie, and your explanations and examples are easy to understand.