# Using Git

`This is an exciting journey to the world of Version control. Hoping you already opened a GitHub account?`

Git is a widely used **distributed version control.**

Version control systems are a category of software tools that allows you to track changes to your code over time.

#### What's the difference between Git and GitHub?

Git is a widely used **distributed version control** while GitHub is a web-based hosting service that lets you manage Git repositories.

There are many other web hosting platforms that include: GitLab, Bitbucket, OneDev, Gogs, AWS CodeCommit, e.t.c.

Now that we know the difference between the two now lets install git.

## Installation of git

1. Go to [Git-Download](https://www.git-scm.com/downloads)
    
2. On the Downloads select the operating system you are using.
    
3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704778840395/79666ebb-d0a4-4e6e-ad56-bf4fdb67c15e.png align="center")
    

Select click for download or you can install via command prompt.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704779144885/063d146b-e577-41aa-bb88-dc1d1b1781ad.png align="center")

1. Once it's installed, on the terminal/using Git Bash(You'll see it on the start menu comes with installation of Git). Identify your identity. Use the commands below. replace the username with your GitHub username and email address
    
    ```plaintext
    $ git config --global user.name "John Doe"
    $ git config --global user.email johndoe@example.com
    ```
    
    You can also configure your default branch to be main or just leave it as master
    
    ```plaintext
    git config --global init.defaultBranch main
    ```
    
    Confirm if you set the username right by typing
    
    ```plaintext
    git config user.name
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704779691357/ae52d583-54b6-43a8-b349-0518522d8349.png align="center")
    
    If you want to set the whole thing from the GUI(Graphic User Interface) then from your Start menu select GIT GUI
    
    it opens up to this window.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704780297936/58086797-8cf9-4f6a-a6c8-a915e70ce054.png align="center")
    
    You can choose to create a new repository then choose a folder you want your data to be in.
    
    On the edit menu, select option
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704780736368/a3dc4920-1d7e-46a1-a093-dda797ebc0b0.png align="center")

From this you can be able to still setup the configurations

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704780912448/128959da-736d-4b3c-870b-8f40b484d14a.png align="center")

1. ### Working with Git GUI
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704781780168/65a12ed9-f229-4dc9-ac33-1f82caafc387.png align="center")

This will be the main working window from the GUI.

Window 1: Will show the unstaged files. These are files with no commit made. So unless they are staged they wont be pushed to GitHub.

Once you upload the files you intend to use in your current folder, you'll be able to see then in this window. If they do not reflect click on the `Rescan` button.

Window 2: Is diff view, which shows the changes for the currently-selected file.

When you select a file, for example in our case we have the `README.md` you can edit it in your text editor by clicking on the file(it appears at the top of the window when you select a file).

Window 3: Shows the files that are now ready for committing and pushing to GitHub.

For one to be able to send a commit message ensure at least one file is on this window.

Once you highlight the file from Window 1 then click the button `stage changes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704786883952/6578f1f5-8015-48d0-a5f4-c1791d2f970b.png align="center")

NB: You can toggle files between Window 1 and Window 3.

Window 4: Is the message and action area.Here you can write your commit messages.

If you want your local changes now to be in GitHub you need to create a remote.

1. Create a repository in your GitHub. Once you click the +(plus sign) follow the prompts.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788765424/d4c74fae-48a5-474b-aee7-fa2b48a6b6e0.png align="center")
    

Once on the Git GUI click on remote then add

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704789045076/41b7d8ec-aa95-4609-8d8d-b536772e6449.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704793438803/7380e1f2-c416-4bcd-8d4b-d040257c3bc3.png align="center")

Click on the code(green button) drop-down. For now we use. https. copy the link and add it to the GitHub remote location. You can name it origin(not a must) but most commonly used.

Our file is ready as we have already committed.

Then click on the `Push` button.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704793824374/13e55bf9-0f82-4320-bcd9-856025a4b983.png align="center")

Once you click push follow the prompts.

1. ### Using GitHub Desktop
    
    1. Download [GitHub desktop](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjatsmkhNCDAxVGVPEDHZfeCCwQFnoECAYQAQ&url=https%3A%2F%2Fdesktop.github.com%2F&usg=AOvVaw3Q4aArCExy0qKbKQYeMfD4&opi=89978449)
        
    2. Once installed an have connected with your GitHub account.
        
        Click on create a new repository or clone repository(if repository already exists). Give the name and a bit of description.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704794799064/33d4fd4d-e275-4f6e-b1b2-02d543779de3.png align="center")
        
    3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704794850108/eba489df-6588-44dd-9d0b-46b4eab25eb7.png align="center")
        
    
    That's the interface you get after creating. Click on publish repository.
    
    The `show in Explorer` button allows to open a text editor.
    
2. Edit your files in your editor of choice examples: notepad, VS Code. Once you've made the changes click on commit to main.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704795088447/eb07e63d-943d-4e67-aa63-8d461eec8edb.png align="center")
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704795461735/e2e64849-722b-407c-b906-4e1d50aca35e.png align="center")

Click on `push origin` button

1. ### Using GitHub Web
    
    Once you have created your repository, click on the (+) plus sign.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704796290832/47f45af8-c852-4f4c-8b14-e073d0183001.png align="center")
    
    From there one can upload files or create new files.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704796612336/62489519-3f0a-4ae0-addb-0af9b56ad416.png align="center")
    
    Once you click on the commit changes it will appear on your list.
    
    It is an easy process similar to GitHub desktop
    
2. ### Using Command Line.
    
    This can be on VS Code terminal, PowerShell, Git Bash or any terminal.
    
    1. Confirm Git is installed.
        
        ```plaintext
        git --version
        ```
        
        I am currently using PowerShell and my version is 2.43.0
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704797082063/f2062af1-51e1-45de-aa91-75642fc71aa9.png align="center")
        
    
    2\. **If a repository already exists** and you want to clone it to your local machine. using the `git clone` command
    
    One can use the SSH or HTTPS. For now we use HTTPS
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704797617168/de008036-6601-4fb5-9e81-6514f52d45d8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704798158840/c519b28d-27ac-4762-ac46-ec6116e1b1b0.png align="center")

**If the repository doesn't exist** and we want to create a repository from our folder.

* Open the terminal in the directory you’d like to convert
    
* ```plaintext
        git init
    ```
    
    This command initializes an empty repository.
    
* Add the path to your remote repository.(copy as done above in clone process)
    
    ```plaintext
    git remote add origin https://github.com/EstherNjuguna/dataanalytics.git
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704806612084/cb75d9b7-8123-4529-94f5-a1a088a6662d.png align="center")

* Create a branch
    
    a branch is a copy of the files in the repository at the time you create the branch. You can create as many branches as needed. The default is main.
    
* ```plaintext
        git branch -M main
    ```
    
    The `-M` flag is used for branch renaming
    
* Add and commit local changes
    
    `git add` is used. If you want to stage everything `git add .` is used.
    
* ```plaintext
        git add <name of file or folder>
    ```
    
* Confirm the files have been staged using `git status`
    
* Commit the staged changes
    

```plaintext
git commit -m "Your commit message"
```

* Finally push the code to GitHub.
    
    ```plaintext
    git push -u origin main
    ```
    
    After the first push one can use `git push`.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704806698790/80c3db67-ade6-4df5-af27-66a6186329c4.png align="center")

### Conclusion

We have learned 3 different ways of using git with GitHub as our web hosting platform.

You can choose to use the Command Line or GitHub desktop or GitHub on the web or even using Git GUI.

Whichever method is easiest to understand you can use that method.

Feel free to interact with the other learning resources attached here and also free to post any questions.

#### Further Reading

1. [https://www.git-scm.com/book/en/v2](https://www.git-scm.com/book/en/v2)
    
2. [Git GUI Explained](https://www.youtube.com/watch?v=icyOhrMNrOk)
    
3. [How to Use GitHub Desktop](https://www.youtube.com/watch?v=RPagOAUx2SQ)
    
4. [Git and GitHub Tutorial for Beginners](https://www.youtube.com/watch?v=tRZGeaHPoaw)
    
5. [Git Using Command Line](https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html)
    
6. [Geeksforgeeks Git on Command Line](https://www.geeksforgeeks.org/using-git-on-commandline/)
