Git is a popular version control system used by developers to track changes in their codebase. It provides a way to manage and collaborate on code with a team, making it an essential tool for software development. Git commands are used to interact with the repository, which can be either local or remote. In this article, we will explore how to use Git commands to connect your local repository with the remote repository.

Git Bash

Git is a distributed version control system that allows multiple users to work on the same codebase simultaneously. It helps developers to keep track of changes made to the code and collaborate with others. Git has a command-line interface that is used to interact with the repository. Git Bash is a popular terminal application that provides a Unix-like environment on Windows. It can be used to execute Git commands in a Windows environment. Git Bash can be downloaded and installed here.

Initializing a Git Repository

To start using Git, you need to initialize a Git repository in your project directory. You can do this by running the following command in Git Bash. Go to the folder which contains your project, right click on the folder and open git bash.

$ git init

This command initializes a new Git repository in your current directory.

Adding and Committing Changes

Once you have initialized a Git repository, you can start making changes to your code. To add these changes to the Git repository, you need to use the git add command. For example, if you have made changes to a file named index.html, you can add it to the Git repository by running the following command:

$ git add index.html

To commit the changes you have made to the Git repository, you need to use the git commit command. This command creates a snapshot of the changes you have made to your code. You can add a message to the commit to describe the changes you have made. For example, if you have made changes to the file index.html, you can commit them by running the following command:

$ git commit -m "Updated index.html file"

Pushing Changes to Remote Repository

To push the changes you have made to the local Git repository to a remote repository, you need to use the git push command. This command uploads the changes to the remote repository. Before pushing the changes, you need to configure the remote repository. You can do this by running the following command:

$ git remote add origin <remote-repository-url>

This command sets the remote repository as the origin. You can then push the changes to the remote repository by running the following command:

$ git push -u origin master

This command pushes the changes to the master branch of the remote repository.

Pulling Changes from Remote Repository

To pull changes from the remote repository to the local repository, you need to use the git pull command. This command downloads the changes from the remote repository to the local repository. For example, if your team member has made changes to the file index.html in the remote repository, you can pull the changes by running the following command:

$ git pull origin master

This command downloads the changes from the master branch of the remote repository.

Git GUI: A Graphical Interface for Git

Git GUI is a graphical user interface for Git that provides an easier way to interact with the Git repository. It is a useful tool for beginners who are not comfortable with the command-line interface. Git GUI allows you to perform all the Git operations using

a graphical interface. You can visualize the changes made to the codebase, and commit and push changes to the remote repository using a few clicks.

To launch Git GUI, you need to open Git Bash and run the following command:

$ git gui

This will launch the Git GUI window, where you can perform various Git operations. You can open the local repository in Git GUI by clicking on the “Open Existing Repository” button and navigating to the project directory.

Once you have opened the local repository in Git GUI, you can view the changes made to the code by clicking on the “Commit” button. This will open a new window where you can select the files you want to commit and add a message to the commit.

Git gui

To push the changes to the remote repository, you need to click on the “Push” button. This will upload the changes to the remote repository. You can also pull changes from the remote repository by clicking on the “Fetch” button.

FAQ

Q: How do I clone a repository to my local machine from a remote repository? A: You can open a folder in your pc and type the following command

$ git clone [remote-repository-url]  

Q: How do I create a new branch in Git? A: You can create a new branch in Git using the git branch command. For example, to create a new branch named “feature-branch”, you can run the following command:

$ git branch <feature-branch>

Q: How do I switch to a different branch in Git? A: You can switch to a different branch in Git using the git checkout command. For example, to switch to the “feature-branch” branch, you can run the following command:

$ git checkout <feature-branch>

Q: How do I merge changes from one branch to another in Git? A: You can merge changes from one branch to another in Git using the git merge command. For example, to merge changes from the “feature-branch” branch to the “master” branch, you can run the following command:

$ git checkout master $ git merge <feature-branch>

Example scenario

Suppose you are a member of a development team working on a web application. You have been assigned a feature that involves creating a new login page for the application. You are working on a feature branch called “login-page-feature” that was created from the master branch.

Here’s how you can pull changes from the master branch to your feature branch, make changes in the local repo, push those changes to your remote feature branch, and then merge the changes into the master branch using a pull request.

  1. Switch to your feature branch:

Before making changes to your feature branch, it’s a good idea to ensure that you have the latest changes from the master branch. To do this, you can switch to your feature branch using the following command:

$ git checkout login-page-feature
  1. Pull changes from the master branch:

Now that you are in your feature branch, you can pull the latest changes from the master branch using the following commands:

$ git pull origin master
You can also use the following two commands in order to pull the latest changes remote master branch
$ git fetch $ git merge origin/master

The first command, git fetch, retrieves the latest changes from the remote repository without merging them. The second command, git merge origin/master, merges the changes from the remote master branch into your local feature branch.

  1. Make changes in the local repo:

You can now make the necessary changes to your local repo in the “login-page-feature” branch. For example, you might update the HTML and CSS files for the login page, or modify the code for the authentication process.

  1. Stage and commit changes:

Once you have made the necessary changes, you can stage and commit them using the following commands:

$ git add .
$ git commit -m "Updated login page"
  1. Push changes to your remote feature branch:

You can now push the changes from your local repo to your remote feature branch using the following command:

$ git push origin login-page-feature
  1. Create a pull request:

After pushing the changes to your remote feature branch, you can create a pull request to merge the changes into the master branch. To do this, you can go to the repository on GitHub, select your feature branch, and click on the “New pull request” button.

  1. Review and merge pull request:

Once the pull request has been created, your team lead or manager can review the changes and approve the pull request. After the pull request has been approved, the changes will be merged into the master branch.

Before you start working on new changes, make sure to pull the latest from the master branch to your working feature branch in the local setting.

By following these steps, you can effectively manage and collaborate on your codebase with your team, and ensure that your changes are properly integrated into the master branch.

Conclusion

In this article, we have explored how to use Git commands to connect your local repository with the remote repository. We have learned how to initialize a Git repository, add and commit changes, push changes to the remote repository, and pull changes from the remote repository. We have also seen how Git GUI provides a graphical interface for Git that makes it easier to interact with the repository. By using these Git commands, you can effectively manage and collaborate on your codebase with your team.

Leave a Reply

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