o 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 copy the hash of your previous commit from here and paste in the command prompt. It will look like this.
Now we will run the following command.
git reset --hard "commit hash"
This forces our remote repository projects files to be the state they were in, when we made that particular commit, regardless of what we have now.
When we do so, we’ll receive the message that we are one commit behind. Meaning git counted this action as another commit and now you have some changes in your remote repository.
Let’s now fetch these changes without merging them into the directory and selectively doing the merge by ourselves. To do so, we’ll run the git fetch command as follows:
git fetch --all
This will fetch all the commits that we don’t currently have, but without merging them or affecting our workflow. So let’s actually add a few words in our text file, I’ll add the words ‘we made’ after the word ‘project’ on the third line.
I’ll then add and commit these changes. Now our local repo has a commit that our remote doesn’t have, and our remote has a commit that our local doesn’t have. What to do then?
Well, let’s try to use the
command and see what happens. We get the message that we have a “Conflict”, the automatic merge didn’t work and we need to fix these conflicts by ourselves.
On my VS code, I can clearly see these conflicts and the actions I can take to resolve them. If you’re not using VS code, do not worry, most editors support conflict management.
I have 4 options, ‘Accept Current Change’, ‘Accept Incoming Changes’, ‘Accept Both Changes’ and ‘Compare Changes’.
I will choose to accept both changes to showcase the different options, but of course, in this situation, you should choose the most appropriate. Now, this is how the text file looks like:
At this stage, what you ideally do is click on either “Accept Current Change” or “Accept Incoming Change”, depending on what changes you want to keep and what you want to discard.
Once done, we’ll finalize the merge by committing our changes.
We’ve mentioned branches a lot so far, 