Learning Git and GitHub: Step-by-Step Guide for Beginners

Git and GitHub are essential tools for software development. In this project, we’ll be creating a simulation of a one-year experience in a company that uses Git and GitHub. We’ll start with a beginner-level understanding of Git and GitHub and gradually work our way to an expert-level understanding.

Step 1: Initializing a Git Repository

  • Begin by creating a directory for the project and navigating into it.
  • Run the following command to initialize a Git repository:
git init

This command creates a new Git repository in the current directory.

Step 2: Creating a GitHub Repository

$ git remote add origin https://github.com/<username>/<repository>.git
$ git push -u origin master

Step 3: Making Changes and Committing

  • Now that the Git repository is set up, make some changes to the project files.
  • Run the following commands to stage and commit the changes:
$ git add .
$ git commit -m "Initial commit"

The git add command stages the changes, and the git commit command creates a new commit with a message.

Step 4: Branching

  • As your project grows, you may want to work on multiple features simultaneously. This is where branching comes in.
  • Run the following command to create a new branch:
$ git checkout -b feature1

Step 5: Merging

  • Once the work on the feature branch is complete, you’ll want to merge it back into the main branch (master).
  • Run the following commands to merge the feature1 branch into master:
$ git checkout master
$ git merge feature1

tep 6: Pull Requests

  • In a company, it’s common for multiple developers to work on a project simultaneously. To keep everyone’s changes organized, pull requests are used.
  • To create a pull request, go to the GitHub repository and click on the Pull Requests tab. Then, click on the New Pull Request button.
  • Fill in the details of the pull request, including a description of the changes and a reference to the branch you want to merge.
  • After the pull request is reviewed and approved, it can be merged into the main branch.

Step 7: Tagging

  • Tagging is used to mark specific points in the project’s history as important releases.
  • Run the following command to create a new tag:
$ git tag v1.0

This creates a new tag named v1.0 at the current commit.

Step 8: Collaborating with Others

  • Collaborating with others on a project is a fundamental part of using Git and GitHub.
  • To collaborate with others, you’ll need to work with forks, clones, and pull requests.
  • To fork a repository, go to the repository on GitHub and click the Fork button. This creates a copy of the repository in your own account.
  • To clone a repository, run the following command:
$ git clone https://github.com/<username>/<repository>.git

This creates a local

Copy of the repository on your computer, allowing you to work on it locally and make changes.

Step 9: Managing Conflicts

Conflicts can arise when multiple people make changes to the same file in a project. Git helps manage these conflicts by marking them in the file and requiring you to resolve them manually.

To resolve a conflict, open the file in question and look for the conflict markers (<<<<<<<, =======, >>>>>>>).
Edit the file to resolve the conflict, then run the following commands to stage and commit the changes:

$ git add <file>
$ git commit -m "Resolved conflict in <file>"

Step 10: Advanced Git Commands

As you become more experienced with Git and GitHub, you may want to learn more advanced Git commands to further improve your workflow. Some examples include:

git stash: Temporarily saves changes that have not yet been staged.
git rebase: Re-applies a series of commits on top of a different base branch.
git cherry-pick: Applies a specific commit from one branch to another.

wa-square-icon