it -m “added hello.txt”
In the command line, it says that one file was changed with 3 insertions, which are the three lines we’ve written so far.
Since all the collaborators will be reading your commit messages, it’s important you learn how to write them succinctly: Git Commit Messages
So everything works fine, but now if we want to share the project with a colleague and bring them on board we need to place our project somewhere on the cloud where other teammates can access it. We’re not making it centralized, as everyone is going to have his working copy, but there needs to be a focal point to pipeline the project changes. So let’s go and create a repository on Github.
Give it a description and a name, I’ll call it Hello Git.
When the new repository’s page opens up, you’ll find an option to push an existing repository which is exactly what we want to do.
So we’ll follow the commands and write:
git remote add origin [repository’s link]
git push origin master
The first command links the remote repository we created to our local one under the name of origin, and in fact, you can name this anything you like as long as it’s clear enough. The second command pushes the files we have on the master branch to the origin repository. So by running these two commands, our repository would end up with the text file we created on the repository.
Now that your code is pushed to a remote repository, anyone with access/permissions to your repo can download the code and start editing it.
There are two ways to bring a remote repository to a directory on your local machine. You can either download a zip of the entire project or you can clone(duplicate) the repository to anywhere on your system.
To download the repository with the full history of the project and nearly every change that was ever made, we use the command
Just navigate to the place where you want to clone the repository and run
git clone "link of the remote repository"
Usually, you don’t need to write this entire command on your own. Just find the repository you want to clone (for ex. On Github), click the green “Clone or download” button and you can see the option to download the zip or copy the Url and replace it in the command “
git clone "link of the remote repository"
Now let’s dive into a very critical part of Git. Let’s say you’re working with a colleague on the same project and he makes some changes to the repository on the same file you’re working on.
(To mimic this behavior, I am going to clone the project I pushed in the last step, to another directory of my system using the git clone.
This way we can mimic two people collaborating on the same project.)
I’ll open another instance of VS code and will head to the directory where I cloned the project, there I’m going to change the word ‘first’ to ‘changed’ and then go through the steps we went through earlier to push the project, that is: add, commit, then push directly.
Note: We will not need to run the git remote add command since we cloned the repository and it’s already remotely added.
git merge and git fetch
There are two ways that we can bring this change into our main project and we’ll go through both. The first is through the
command, which is going bring the changes and merge them into our files.
The other way is to fetch the changes first and then merge them into the files we have. So it’s basically running two commands. But what if we only want to fetch the changes then selectively merge them when we want to?
(Before trying this out we need to have some changes in our local files as well as in our remote repository files. To mimic this we are going to perform some additional steps.)
Git allows us to visit the state of our files at the time of a previous commit. If you want to head to a previous commit without making a change you can run the <code>git checkout “commit hash” </code>command and pass the appropriate commit hash. You can view all previous commits with hashes using
You can also find these by pressing on the commits tab on your repository on GitHub. And It will look something like this.
You can cop