CodeNewbie Community 🌱

Cover image for GIT ~ Learn the basics [Part One]
000
000

Posted on

GIT ~ Learn the basics [Part One]

Everybody needs to learn GIT! Learning GIT is not only a great skill to have but it is also a necessary one. Precisely because software development is characteristically more like a journey you take with your friends than a destination you reach individually. What do I mean by that? It is a process that requires constant refinement & correction as you work in collaboration with other developers. GIT is an application which helps to facilitate this process of refinement & correction with version control.i.e creating, cloning, making & reversing changes and integrating those versions of changes into the code base. The code base or source code is saved on Github so that it can be accessed by developers who want to contribute.

I will try to explain the basics of GIT to consolidate my knowledge & help you break it down with my simple & straight forward explanation.

  • Before we start, follow these instructions on this website here to install GIT if you've not already done so.

  • I am going to assume that you have a Github account and you're familiar with it.

  • In addition I am going to be using the command line on MacBook to create directories/folders to start a project to be used as an example

Okay with the above taken care of, let's start with the basics of GIT using an example.

GIT for starting your own individual project

  1. Create new local repository in a directory of your choice using the command line
    Alt Text

  2. Open that local repository for the new project in Vs code or IDE of your choice. I use Vs code.
    Alt Text

    And then it will open in the Vs Code like this.

    Alt Text

  3. Now let's create a remote repository that will save all the changes made on your local repository.

    Click on new repository on your Github profile.
    Alt Text

    And then create a new repository for your project and name it.
    Alt Text

    And this will be the page you will see after you created the repository.
    Alt Text

  4. Add and connect the remote repository to the local
    repository by following the instruction below.

    • Initialize the repo using πŸ‘‰ git init
    • Create new files or make changes to your code base.
    • And then use πŸ‘‰ git add . to add the changes.
    • Check if the changes have been tracked using πŸ‘‰ git status.
    • If they have been tracked it will let you know as follow.πŸ‘‡Alt TextIf not it will show the files or changes not tracked in red.

    Then commit the changes using πŸ‘‰ git commit -m "changes made description"Alt TextThen add & connect the remote repo to the local repo as follows. πŸ‘‰ git remote add origin i.eAlt TextOnce you have established the connection between the remote & local repo, then you can push the changes made on the local repo to be merged into the master or main remote repo.i.eAlt TextYou can double check if your commit has been pushed to the remote repo you created for your individual project for the first time by refreshing the page. You should be able to see something like this πŸ‘‡Alt Text

  5. To create a new branch you use πŸ‘‰ git branch - branch name.i.e
    Alt TextAnd then to switch to the new branch you created,
    you use πŸ‘‰ git checkout - branch name. i.e Alt TextIt will then show you have switched from the master branch to the new newFeature branch.

    You can accomplish both git command lines above with just one combined git line as follow πŸ‘‰ git checkout -b newFeature

    Here I created and switched to another branch named anotherFeature using the combined git line πŸ‘‰ git checkout -b anotherFeature.Alt Text You can check the number of branches using πŸ‘‰ git branch --list and it will show you list of branches as follows.Alt TextThe one highlighted in green is the branch you're currently on.

  6. You can then work on your feature and commit the changeπŸ‘‡Alt TextAnd push the changes as follows to remote repoπŸ‘‡Alt TextAnd you can check you've done it right by going to the remote master/main repo on Github.Alt TextIf you have've done it right you should see a message similar to the above.

  7. To create a pull request, first switch to master/main branch. Then pull request from the branch you worked on or made changes using - git pull origin branch name as follows πŸ‘‡Alt Text Pull requests can also be done manually on the Github page. That's where I usually do it when I am working in group. Often times it has to be reviewed by another group member before it gets merged into the master/main branch.

The explanation so far is enough to get you going when working by yourself on an individual project. In part two of these series, I will talk about how to clone & start from an already made up code base.

Thank youπŸ™πŸΎπŸ‘¨πŸΎβ€πŸ’» for reading my blog so far. Please follow my blog or follow me on twitter @wolde_ai to get more of these series on GIT. I welcome any kind of criticism as it is part of the learning process.βœŒπŸΎπŸ€“

Top comments (0)