Pushing changes to GitHub Repositories

So far, we have learned how to create a repository in GitHub and clone it.
In this tutorial, we will be learning how to add a file to the repository and publish it to the GitHub.
We’ll also be learning how to carry these tasks with sourcetree and GitHub Desktop.

For better understanding, both sourcetree and GitHub Desktop are pointing to the same location in the local machine which is

/Users/amarkumar/Documents/GitHub/simple-web-app

Any changes to the project folder will also get reflected both the app.

Adding a new file to the repository
In order, to add a new file, create a new file in the cloned repository folder.
We’ll be adding a README.md, which acts as a guide to what the repository is all about.
(Please go through the tutorial on how to write a README.md using Markdown script.)

  • Creating a file
    Create a new file “README.md” using vim editor, or alternatively, you can create a new file using your favorite text editor and place it in the repository folder.As you can see, we have already created a READM.md file which has a plain text.
  • Adding a File
    Though we created a new file, it’s still unlinked to the repository.
    We need to manually add the file to be linked with the current repository by specifying an individual file name.
    If we have created so many files, we can use the “.” To denote all files.To see what has been added or deleted from the repository folder. We type in the below command

    $ git status

When running the command, it lists README.md as an untracked file.
To add it, we run

$ git add README.md

OR simply

$ git add .

to add it to the tracked files.

If we have defined a .gitignore file, while we run the git add . command, files matching the pattern will not be added.

  • Committing a file – Once, it is added, we need to commit these changes with a suitable commit message.Commit is nothing but a checkpoint to maintain what has been modified.
    We can commit this change by typing.

    $ git commit -m “Added README.md”

    When the files are committed, we are ready to push it to the remote repository at GitHub Server.
    We can do this by simply typing

    $ git push origin master

    This will scan the origin of the remote repository, the origin is nothing a URL from where the project has been cloned.
    All the changes that we have done, we want to push it to the main branch called master branch right now.
    We’ll learn more about branching in coming tutorials.


    Publishing changes using sourcetree

    In sourcetree,
    We can select files, that we want to commit, add a Commit message in the dialogue box below and click on commit.
    If we want to push the changes immediately to the branch.
    We can tick on “Push changes immediately to origin/master”.
    Since we are working on the master branch.

    Publishing changes using GitHub Desktop.

    In GitHub Desktop, any new addition of file or deletion will be reflected in the GUI, to add those files, click on the checkbox to add it to the tracker.

    If the changes are correct add a commit message and click on “Commit to master”.
    After which you can push the changes to the remote master branch.
    By clicking on the “Push Origin” button.


    Once we push the changes, following any of the above operations.
    We can see the file has been published to the GitHub Repo.

Conclusion
In this above tutorial, we have learned how to add a file to our cloned local repository, adding those files to the tracker, committing those changes and pushing those changes to a remote repository. One should carefully go through this tutorial because this is the most common task a programmer performs when they use Git.

Translate »