Ultimate Git and Github guide for beginners 2024
Introduction
Version control is an essential part of software development, allowing developers to track changes, collaborate, and manage their codebase efficiently. Git is a distributed version control system that has become the industry standard, and GitHub is a web-based platform for hosting and collaborating on Git repositories. In this article, we’ll explore the basics of Git and GitHub, covering everything from installation to pushing your first commit.
Fun Fact: Git was created by Linus Torvalds, the famous developer who also created Linux, in 2005 to help manage the development of the Linux kernel.
Git vs Github
Most common doubt for a beginner is the difference between Git and Github. Below is a quick differentiation between them.
Other platforms similar to GitHub for hosting and managing Git repositories include GitLab, BitBucket, SourceForge, GitKraken etc.
Installing Git
Before we dive into Git, you’ll need to install it on your machine. Head over to the official Git website (https://git-scm.com/downloads) and follow the instructions for your operating system.
Configuring Git
After installation, you’ll need to configure Git with your name and email address. Open your terminal or command prompt and run the following commands, replacing “Your Name” and “your@email.com” with your actual name and email address:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Creating a GitHub Account
Next, you’ll need to create a GitHub account. Visit https://github.com and click on the “Sign up” button. Follow the prompts to create your account.
Fun Task: Try to spell guthib.com instead of github.com :)
Creating a Local Repository
To start using Git, you’ll need a repository. You can either create a new repository or initialize an existing project as a Git repository. Let’s create a new repository:
- Open your terminal or command prompt and navigate to the directory where you want to create your project.
- Run the following command to initialize a new Git repository:
git init
Making Your First Commit
Now that you have a repository, let’s make your first commit:
- Create a new file in your project directory. You can use any text editor or IDE of your choice. Let’s say for example create file named “test.txt”
2. Once you’ve made some changes to the file, run the following commands in your terminal ( also note that when there are changes which are not committed the colour of the branch is orange ) :
git add .
git commit -m "Initial commit"
The git add
command stages your changes, and the git commit
command saves those changes with a descriptive message.
Creating a Remote Repository on GitHub
To collaborate with others or host your project on GitHub, you’ll need to create a remote repository:
- Log in to your GitHub account and navigate to the “Repositories” section.
- Click on the “New” button to create a new repository. You can name it whatever you want and set it public or private.
- Provide a name for your repository and click “Create repository”.
Connecting Your Local Repository to GitHub
Now that you have a remote repository on GitHub, you need to connect your local repository to it:
- In your terminal, run the following command, replacing
YOUR-USERNAME
andYOUR-REPO-NAME
with your GitHub username and repository name, respectively ( when your github repository is created your repository link will be visible along with commands in the command line tutorial section ) :
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO-NAME.git
git branch -M main
#the last command will rename the current branch to main.
#this is done as default naming convention in github's repository
#changed from master to main
For me it will be
git remote add origin https://github.com/Darshan-Padia/DemoRepo.git
git branch -M main
2. To push your local commits to the remote repository, run
git push -u origin main
From your GitHub account, go to Settings → Developer Settings → Personal Access Token → Tokens (classic) → Generate New Token (Give your password) → Fillup the form ( **make sure to tick the repo option** )
→ click Generate token → Copy the generated Token, it will be something like ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta
paste this token in password section and you are good to go.
Conclusion
Congratulations! You’ve successfully created your first Git repository, made your first commit, and pushed it to GitHub. This is just the beginning of your journey with Git and GitHub. There’s so much more to explore, including branching, merging, pull requests, and collaborating with others.
Remember, practice makes perfect. Keep using Git and GitHub for your projects, and you’ll become more comfortable with these powerful tools over time.