The Ultimate Beginner's Guide to Mastering Git and GitHub

Mastering Git doesn't have to be difficult. This beginner's guide provides step-by-step instructions and helpful resources to help you become a Git pro.

By Tim TrottSoftware Engineering • July 15, 2024
1,456 words, estimated reading time 5 minutes.
The Ultimate Beginner's Guide to Mastering Git and GitHub

Git is a distributed version control system (DVCS) commonly used in software development and other collaborative work. Linus Torvalds invented it in 2005, and it has since become the de facto standard for version control in the software development industry. Mastering Git allows developers and teams to manage and track changes to source code and other files.

Git tracks changes to your files and directories over time. This means you can see who made the changes, what they were, and when they were done. It is especially handy for code files, although it may apply to any file.

What Is The Difference Between Git and GitHub?

Software engineer coding in office with dual screens
What Is The Difference Between Git and GitHub?

In version control and collaborative software development, Git and GitHub are related but separate ideas. GitHub is a web-based platform that stores Git repositories and provides collaborative features. Git is the version control system itself. You can use Git without GitHub for version control. However, GitHub improves the collaborative elements of Git and makes it easier for numerous developers to collaborate on the same codebase. GitLab and Bitbucket are two platforms that are comparable to GitHub.

Install Git on Your Computer

Installing Git on your computer is a straightforward process. It's available for Windows, Mac, and Linux, so regardless of your operating system, you're covered. Simply visit the official Git website, download the version that matches your OS, and follow the installation instructions. Once installed, you're ready to manage your code and collaborate with others.

Open a terminal or command prompt and verify the Git installation by running:

powershell
git --version

Before you begin using Git, you must first configure your Git account and email address. This data will be linked to your commits, allowing others to determine who made the changes. Open your command line or terminal and type the following commands to configure your username and email:

powershell
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

"Your Name" should be replaced with your desired username and "[email protected]" with your email address. Use the email address linked with your Git hosting service for authentication. After you run these instructions, your Git login and email address will be created and ready to use.

Create A New Git Repository And Make A First Commit

In your favoured terminal, navigate to the directory where you wish to save your project files to start a new Git repository. Enter the following command into your command line or terminal -

powershell
git init

This operation creates a new Git repository. A notice will appear verifying that the repository has been initialised. After that, you can begin adding files to your repository. To check the status of your changes and see which files are staged for the next commit, use the 'git status' command. To add a file to the staging area, run the following command -

powershell
git add file1.txt file2.html

Replace 'file1.txt file2.html' with the name of the file(s) you want to add. 'git add .' can also be used to add all files in the current directory.

powershell
git add .

When you are ready to commit changes, use this commit command -

powershell
git commit -m "Initial commit"

Cloning A Git Repository

This command will clone the repository located at the specified URL into a directory with the name "repo-name" in your current working directory. Cloning a repository will make a complete copy of the remote repository on your local machine.

Suppose you specify a different local directory name for the clone. In that case, you can provide it as the second argument:

powershell
git clone https://github.com/username/repo-name.git my-local-directory

In this case, the repository will be cloned into the "my-local-directory" directory.

After running the git clone command, you will have a complete copy of the remote repository on your local machine. You can then work with the repository, make changes, and push those changes back to the remote repository when needed. To push your changes to the remote repository, use the 'git push' command.

Working with Branches in Git

Using Git requires you to create and switch between branches. Branches enable you to work on many versions of your project simultaneously without affecting the main branch or other branches. Use the command 'git branch' to create a new branch.

To create a new branch to work on a specific feature or fix:

powershell
git branch feature-branch

Switch to the new branch:

powershell
git checkout feature-branch

Once on a branch, you can make changes and commit them just like on the main branch. This allows you to work on new features or bug fixes without interfering with the main development process. To switch back to the main branch, use the command git checkout main. You can also use the command git branch to see a list of all branches in your repository and identify which branch you are currently on.

Stage and Commiting Changes To Your Repository

It is important to stage and commit changes after adding files to your Git repository. You can choose which modifications to include in your commit using staging. Use the command git add file1.txt to stage changes, replacing 'file1.txt' with the name of the file you want to stage, or use git add . to stage all files in the current directory.

You can commit your modifications to the repository after you have staged them. To make a new commit, use the command -

powershell
git commit -m "Your commit message"

You are replacing "Your commit message" with a summary of your changes. This message will assist you and others in comprehending the significance of the commitment. Staging and committing changes are effectively saving snapshots of your project at different points in time. This enables you to monitor the progress of your project and revert to prior versions if necessary. Remember to stage and commit your changes frequently. At the same time, you work on your project to keep your Git repository up to date-and organised.

Dealing With Merge Conflicts in Git

Dealing with merge conflicts in Git is common daily in collaborative software development. Merge conflicts occur when Git cannot automatically merge changes from separate branches. Follow these steps to resolve merge conflicts.

Pull or Fetch the Latest Changes

Ensure your local repository is up to date by running either git pull (if you're working with a branch that is tracked) or git fetch to get the latest changes from the remote repository. It is important to have the latest changes to minimise potential conflicts.

Identify the Conflicting Files

After pulling or fetching, Git will indicate which files have conflicts. These files will have conflict markers (between <<<<<<< HEAD and ======= and >>>>>>> branch_name) indicating where the conflicts exist.

Open Conflicting Files

Open the conflicting files in a text editor. Conflict markers will surround the conflicting sections. You must decide which changes to keep and which to discard.

Resolve the Conflicts

Edit the conflicting sections in the file to resolve the conflicts. You can choose to keep your changes (between <<<<<<< HEAD and =======) or the incoming changes from the other branch (between ======= and >>>>>>> branch_name). You can also make entirely new changes if needed. Remove the conflict markers once you're done.

For example:

<<<<<<< HEAD
Your changes here
=======
Their changes here
>>>>>>> branch_name

After resolving the conflicts in the file, save it and use git add to stage the resolved files.

Continue the Merge

Complete the merge operation by running git commit with a merge commit message. Suppose you're working on a branch that you will push to the remote repository. In that case, you can push your merged changes after resolving the conflicts.

powershell
git commit -m "Merge branch_name into your_branch_name"
git push

Your branch should now have been successfully merged with the changes from the other branch, and any conflicts should have been resolved.

Remember that merge conflicts are a natural feature of Git collaboration. To avoid conflicts, it is important to communicate with your team and ensure everyone is informed of changes in the repository. Pulling or merge requests can aid in merging by allowing code reviews before changes are merged into the main branch.

Additional Git Commands

Here are some other common Git commands:

  • git status - Check the status of your repository.
  • git log - View the commit history.
  • git diff - See the differences between your working directory and the last commit.
  • git pull - Update your local repository with changes from a remote repository.
  • git push - Push your local changes to a remote repository.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.