Pushing Code to GitHub and Making Changes

Pushing Code to GitHub and Making Changes

An Easy Guide for GitHub Beginners

ยท

4 min read

In my coding journey, one challenge stood out โ€“ putting my code on GitHub and making updates. Sometimes, I'd figure it out, but the next day, I'd forget how. It was like a puzzle without a solution.

What I needed was a clear, simple guide for the basics. Even though I studied Git and took notes, I still missed the essential steps. But the good news is, I eventually mastered it and became a Git pro.

And you can do it too! By the end of this article, I'll share an easy-to-follow guide to always remember how to push your changes to GitHub. Soon, you'll be one of those folks who say, 'GitHub? No problem ๐Ÿ’ก!

Let's start with how to push your code to a GitHub repository. After that, I'll walk you through the steps for making updates.

Pushing Code to a GitHub repository ๐Ÿš€

After creating your GitHub account at https://github.com/, open GitHub and ensure you're logged in. As shown below, your username will appear on the left, and your profile picture on the right.

  1. Start by creating a new repository.

    Click the plus sign and choose 'New Repository'.

    Name your repository. You can add a description if you want, and for the rest of the settings, leave them as they are for this demo.

    Click the 'Create Repository' button.

    The next setup page contains valuable information on pushing your code to a GitHub repository using the terminal.

  2. Setting up Your Workspace and Creating a Sample File

    Create a new folder and open it with your preferred IDE. In my case, I'm using Visual Studio. Add a file and write your content.

    For example, I created a folder called 'DemoGitHub' and added a file named 'Repo.txt' with a brief description: 'Hello there!

    Open your terminal by clicking 'View' in the top bar and selecting 'Terminal,' or you can simply press Shift+Esc on your keyboard.

  3. Initialize an empty git repository in your folder

    We do this by typing git init and pressing Enter or Return. Don't worry about any yellow writings; they are hints to guide you.

    This Git repository now becomes your new local repository, connecting it to the folder you've created

  4. Add the file to your new local repository.

    We're preparing 'Repo.txt' for GitHub. We do this by staging it, which means getting it ready for the next step.

    Use git add . to stage all the files in the folder. This adds them to our new local repository and gets them ready for the first commit. Think of a commit as a message you attach to your files.

    Confirm this by typing git status and pressing Enter. You'll see the current branch you're on, which might be 'master' or 'main,' and you'll also see the list of files staged for commit. In our case, it's the 'Repo.txt' file.

  5. Commit the staged file to your local repository.

    Use 'git commit -m' followed by a message in double quotation marks, for example, 'first commit'. This message is like a brief note about your changes. The command will be git commit -m "first commit"

  6. Copy the URL of the remote repository.

    To do this, return to the quick setup page of your Git repository and copy the URL just like the one I have highlighted in blue

  7. Add the URL for the remote repository

    Add the URL for the remote repository, this is where your local repository will be published. You can do this by entering 'git remote add origin' and pasting the URL that you just copied. As you can see in the image below.

  8. Push your local repository's code to GitHub.

    To push your code to GitHub, use the command git push origin master or git push origin main, depending on the branch you're using (either 'master' or 'main').

    After pressing Enter, you may be prompted to enter your username and password. If you've already filled them in before, you won't be asked again.

  9. View the file in your repository.

    After completing all the steps, return to GitHub and you'll be able to see your file in your repository. Don't forget to refresh the page.

    And now, We have successfully pushed our code to GitHub! โœ…

Making Updates to Code on a GitHub Repository ๐Ÿ“ค

Now, let's explore a scenario where we need to make changes and push them. For instance, I've added a short sentence to my file, and I want to push this change. What's the next step?

Simple! I'll use the same commands as before, but this time, there are only three:

  1. git add .

  2. git commit -m "description added"

  3. git push origin master (or git push origin main if you're using the main branch).

After refreshing the GitHub page, you'll be able to see our latest commit: 'description added.'

We've successfully Pushed Code to a GitHub repository and introduced the three key commands you should remember for updating your GitHub repository. ๐Ÿš€ Congratulations, you've just rocked that code!

Until next time... Happy coding! ๐Ÿ–ฅ๏ธ๐Ÿ’ก๐ŸŽ‰

ย