DEV Community

GeraldM
GeraldM

Posted on

Git Bash (Pull and Push code, track changes and version control)

Prerequisites

  • A GitHub account
  • Git bash application installed
  • Git bash connected to GitHub
  • An IDE software Eg. Visual Studio Code

What is Git Bash?

Git bash is a command-line application for windows that provides a Unix-like terminal environment specifically designed to work seamlessly with Git and GitHub software in development workflows. It allows developers to run commands to push, pull, commit or clone from or to a remote repositories such as GitHub.
It acts as a local interface where developers manage their code (code in your local machine) with remote repositories hosted on GitHub (code stored on GitHub).

What is Git?

Git is a version control system that is used to track changes in source code.
Git is the underlying technology that runs locally on a developers computer, while GitHub is an online platform that hosts Git repositories and adds collaboration features such as push, pull and commit.
Using this version control system(Git), developers can create repositories, make commits to record code changes, create branches to develop features independently, and merge those changes back into the main code-base. These Git-managed changes can then be pushed to or pulled from GitHub, allowing teams to share code, back it up remotely, and collaborate on projects in an organized, controlled manner.

Why is version control important?

I am sure you've head or experienced incidents where a software is not working and the reason upon discovery was because someone added (pushed) new code to it and it was fixed by reverting back to the old version. Well, here is where version control comes in. With version control, we can be able to track and manage changes made to files. This allows us to track what was changed, when the change was made, who made the change and when needed revert back to an earlier version. Now we are able to achieve safe collaborations among multiple people working on the same project, prevent accidental loss of work and also revert back to previous version if mistakes or bugs are introduced.

How to Push code to GitHub

Create a local repository (code on our local machine)

On your file explorer, create a new folder

Launch you IDE software and navigate to the folder we've just created by clicking on File and selecting the open folder option and you will land in the folder


By hovering above the folder name, you will see a file icon. Click on it to create a file and save it as a .py


Write some content into the file. With that now, we have a python code file on our local machine.

Now we want to push this code to GitHub a remote repository.

Navigate to your GitHub account, Repositories section.


Click the green button named New. It takes you to a creating a new repository, where we give the repository a name and then create it by clicking on the create repository button.


The repository is created and GitHub provides us with commands to use to push our code in the created repository.


Open Git and navigate to the folder we created.


Following the commands we received on git, we start with git init to initialize the git on this local repository. Then we run git add . to all files or git add filename.ext to add a specific file to a staging area. Run git status to check the status of your repository at this stage.


Now run the next command which is git commit -m "add a comment here" to commit your code. Then run the command git branch -M main to set a branch name (by using the name main, we set out branch name to main).
Then we run the command git remote add origin "link from GitHub" to connect our local repository to GitHub


We can then now push our code using the command git push -u origin main. This will push our code to our GitHub repository main Branch


When we now check our repository on GitHub, the file that we just pushed, now appears and is on the branch main.


By running the command git log we can be able to see details about the commit we made. We can see a unique hash assigned to the file we committed, the author of the file, the date the file was committed and the comments provided during this commit.

How to Pull code from GitHub

Now we have pushed code to GitHub. But how can we pull code from GitHub?

Let's use a scenario where you have a friend, and you tell them about this cool project that you are working on and that you would like them to collaborate with you. How will you share the code that you have already written with them?
Since you had already pushed your project to GitHub.
Method 1: Clone


You will navigate to your GitHub account and into the project repository you pushed your code to.


Click the green Code button and a drop down will appear. Here, copy the HTTPS link provided and share it with your friend.

Since this will the first time your friend is working on the project and they do not have any code, they will...


Use the link that you shared with them, and run the command git clone "link". This command will download the code and create a folder named after your repository on your friends local machine and then automatically set the remote origin as the repository on GitHub.
Now your friend will have the same code as you and you can now collaborate on the project.

When your friend makes changes on to the code and you would like to see them in your machine you would do the following:
Method 2: Pull


Using Git, navigate into the repository in your local machine. Then run the git branch command to confirm that you are correct branch (one which changes were made to).


After verifying that you are on the correct branch, run the command git pull to fetch all updates made on the repository and merges them with your local repository. Or run the command git pull origin "branch name" to only pull updates made to a specific branch for example a branch named development.

How to track changes using Git

Now we have you and your friend collaborating on a project. Meaning you will be working on the same code together. How will you now be able to track what changes were made?


Using the command git log on, we can be able to track when commits were made, who made them, when they were made them and why they were made based on the commit message.

Now lets say that after making changes to your code, you realize that the new piece of code you added is causing your software to fail. What do you do?
Checking the log you can see the new commits made. On every commit, there is a number next to it. This number is called a hash and it's what we use to revert back our repository to a previous state before a commit was made.


At the moment, here is the state of our repository. But after the last commit was made, it's when we realized that our software has a bug or issues. Now we want to revert back to the previous commit that we are sure has no bugs or any issues.


Using the command git revert "hash", we revert our repository to the state at which a file we had created was not created yet. We also receive a message on git that informs that the file has been deleted.


Using our code editor, we accept the changes made by deleting the file that did exist at that point in time and then commit the changes.


To update a remote repository on GitHub, you run the command git push -u origin main which will make changes to existing repository so that they can be in sync with your local repository.

Having done this now. You now understand what is git, how it used to push and pull code from remote repositories, how it enables collaboration and how it can be used to do version control.

Top comments (0)