Git 0.2. Basic errors, and other useful commands
Cloning a repository, forcing a repository and updating a repository. Because 0.1 was not bad enough!
Okay, if you've already created your first repository, made your first commit, and survived the introduction "congratulations," you were faster than me. If not, then it's probably my fault, hahaha! But there's nothing that can't be learned ;).
So, let us assume you already made your first repository and now you want to make several things.
1.- Erase files from your repository.
2.- Update your repository.
3.- Update your repository but you got an error like, “Updates were rejected because the remote contains work that you do not have locally.
“.
4.- Clone a repository from someone online to run the code in your computer (UNIX/Linux/OSX).
If that is your case, then you are lucky (or you are out of luck, it depends). Because now I will explain how to do it. Let me remind you that I haven't delved into the intricacies of Git because I haven't had the time yet. I am assuming in the next steps that you already created your own repository, you have your GitHub account and you already have the mail, username and keys. If not check the last post: Git 0.1. I am also assuming the repository we are working on is the same as the one your created, if not you need to initialise your repository and create it (go back to the Git0.1 link if that is not the case). And I am also assuming you are in your terminal and you navigated to the folder where the files for your repository are.
First, lets us assume you want to erase files from your repository, for example I uploaded the next files to my demo repository and I want to erase squares.c
.
In this case the only files left will be squaref2.c squaref3.c
and squaref3.c
. In this case we will need to do the next:
1.1- Use the command rm and the name of the file to remove, in this case you would need to type the next:
git rm --cached “file_name”
In my case the filename would be squaref.c
.
1.2- Now you will need to commit the changes as when you upload files:
git commit -m "remove file"
You have to remember that the message “remove file“ its just a commentary that basically explains what are you doing and appears as part of the commit.
1.3- Now you will need to push the changes as when you load files using:
git push origin “branch name“
If you followed the tutorial in git 0.1 your branch name is the standard main, so your command would be:
git push origin main
If I look at the files I have one files less, VICTORYYYY!!!!.
2.1- Let us say that you want to update your repository which includes not only erase but also upload new files. In that case you will need to go to your directory where your files are stored using your terminal. Do the changes which can be erase files, move files in or create new files in (You can use your terminal or the file browser).
Then you will need to simply use the next commands:
git add .
git commit -m "text message"
git push origin "branch name"
2.2- That will upload all the files in the folder to your GitHub account. But let us say you only want to move one file. In the case you want to add one file you must use:
git add "file name.extension"
git commit -m "text message"
git push origin "branch name"
2.3- If you want to add several files you will need to simply list them all together.
git add "file name1.extension" "file name1.extension" ...
git commit -m "text message"
git push origin "branch name"
But something can happen, like an error! (everyone hates errors!).
3.1- A very common error is one where you want to update your git copying the files from your folder to your GitHub account and there is on a file at your account that doesn’t exist in your folder. The error will produce the next message: Updates were rejected because the remote contains work that you do not have locally.
If this happens, you can push the changes by force using the next command:
git push -f origin "branch name"
And finally, what if you don’t have any code in your folder? what if you don’t even have a folder? Well in that case, when the code is not yours but it is a public repository, you can clone the repository to a local folder.
4.1 To clone a repository you need to use the next command:
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
This will copy all files to the folder where you are located. If you are a developer for sure you have used this command a lot!. Hopefully this will help you ore in your road to learning Git and remember yes its freaking hard D:<, bring coffee, tea or chocolate!