Source Control: Using Git & GitHub

Overview

In the world of software development, managing source code can be a daunting task. With multiple team members working on the same codebase, version control becomes critical to avoid conflicts and ensure a seamless workflow. This is where source control systems come into play, and Git is currently one of the most popular and widely used systems.

Git is a distributed version control system, which means that each developer has a copy of the codebase on their local machine. This allows developers to work on their own copy of the codebase without interfering with other team members’ work. Changes can then be merged together when necessary, and any conflicts can be resolved easily.

In this chapter, we will take a closer look at Git and its architecture, and how it can be used to manage codebase versions efficiently. We will also cover GitHub, one of the most popular Git repository hosting services, and explore how it can be used for collaboration and sharing code.

So, let’s get started with Git!

Next, we can move on to discussing the architecture of Git and how it works, followed by a section on how to use Git. We can then wrap up the chapter with a section on GitHub and how it can be used for collaboration and sharing code.

Introduction to git

Git is a distributed version control system, which means that every developer has a complete copy of the repository on their local machine. This is in contrast to centralized version control systems, where there is a single central repository that everyone pulls from and pushes to.

In Git, each copy of the repository is a full-fledged repository with its own history and branch structure. Developers can make changes to their local copy of the repository and then push those changes to a remote repository to share with others.

To start using Git, you’ll need to install it on your local machine. Git is a command-line tool, so you’ll need to be comfortable working in a terminal or command prompt. Once Git is installed, you can create a new repository by running the git init command in a new directory.

After you’ve created a repository, you can start tracking changes to your files by creating commits. Commits are snapshots of your code at a particular point in time, and they include a commit message that describes the changes you’ve made.

To create a commit, you’ll first need to stage your changes by running the git add command. This tells Git which changes you want to include in your next commit. Once your changes are staged, you can create a new commit by running the git commit command.

Finally, to share your changes with others, you can push your commits to a remote repository, such as GitHub. This makes your changes available to others, who can then pull your changes into their own local copies of the repository.

Introduction to GitHub

GitHub is a web-based platform that provides hosting for Git repositories. It offers features such as bug tracking, task management, and wiki for every project. GitHub also offers social networking features such as followers, activity feeds, and the ability to see the history of other users’ changes to a repository.

To collaborate with others on GitHub, you can invite them to your repository as collaborators. You can also fork other repositories, which creates a copy of the repository in your own account that you can modify and contribute back to the original repository through pull requests.

To start using GitHub, you will first need to create an account. Once you have created an account, you can create a new repository by clicking on the “New” button in your dashboard. You will be asked to name your repository and provide a brief description. After creating the repository, you can clone it to your local machine and start working on it.

GitHub also offers a feature called issues, which can be used for bug tracking, feature requests, or general discussion. Issues can be assigned to collaborators and labeled with tags to help organize them.

Overall, GitHub is a powerful tool for managing your Git repositories and collaborating with others. It’s widely used in the software development community and has become an essential tool for many teams.

The 20 most used Git Commands

Here is an overview of 20 of the most commonly used Git commands with examples:

  1. git init: Initializes a new Git repository in the current directory.
  2. git clone [repository URL]: Clones an existing Git repository to your local machine.
  3. git status: Shows the status of your Git repository, including any files that have been modified or added.
  4. git add [file]: Adds a file to the staging area.
  5. git commit -m "Commit message": Commits changes to the local repository with a descriptive message.
  6. git push: Pushes committed changes to a remote repository.
  7. git pull: Pulls changes from a remote repository to your local repository.
  8. git branch: Lists all branches in the current repository.
  9. git checkout [branch]: Switches to a specified branch.
  10. git merge [branch]: Merges changes from a specified branch into the current branch.
  11. git remote add [name] [repository URL]: Adds a new remote repository to your local repository.
  12. git fetch: Downloads changes from a remote repository to your local repository without merging them.
  13. git log: Displays a log of all commits in the repository.
  14. git diff: Shows the difference between two commits, files or branches.
  15. git stash: Temporarily saves changes that are not ready to be committed.
  16. git reset: Resets the repository to a previous commit.
  17. git rm [file]: Removes a file from the working directory and staging area.
  18. git tag: Creates a tag for a specific commit.
  19. git rebase: Merges changes from one branch into another by reapplying each commit in order.
  20. git cherry-pick: Selectively applies a single commit from one branch to another.

These are just a few of the most commonly used Git commands. There are many more that you can use to manage your repository and collaborate with other developers.

Khalid ElGazzar

I am a Cloud Architect, Software Development Manager, and CTO/CDO, bringing a wealth of experience and expertise to the table. My passion for technology, desire to foster knowledge sharing, and commitment to helping others succeed drive me forward. You can contact me at: Khalid (at) SWAC (dot) blog or atLinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *