Like we said earlier Git is the tool that tracks the changes in your code over time while Github is the website where you host all your Git repositories.
The first thing you have to do is install Git. To install git follow the instructions here to install git (if it's not already installed).
Making use of git commands can be so confusing if you don't know what each command stand for and what they do specifically. I have put together basic git commands, what they mean, and how to use them so you can see how easy it is making use of git.
This is actually in two phases.
By local repositories, I mean that folder in your Computer which you want to move to GitHub. To properly explain the whole process, I will attach pictures that explain the whole process.
I would be making use of the terminal in VScode, you can make use of any command-line interface.
The image above is a folder(GIT-DEMO) with just two files. I would be making use of git to push this folder to Github now.
To do that we would be making use of the VScode terminal (
Note: You can make use of any CLI as long as you are in the folder directory.
The first command we would be using is
- git init This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible. In other words, you are initializing a git repository in the root of the folder.
If you check your folder you would see a .git folder
This command is used to add files to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). You can use
git addto stage a whole folder, all unstaged files, or a specific file.
To add all files not staged:
$ git add .
To stage a specific file:
$ git add index.html
To stage an entire directory:
$ git add directoryName
- git status This is a command you will find yourself using all this time. As its name implies, it helps you check/know the status of your files if it has been staged, committed, e.t.c.
The image above tells us that we are on branch master. Not commits made yet and also lists files that should be committed.
- git commit This records the changes made to the files to a local repository. For easy reference, each commit has a unique ID. It’s best practice to include a message with each commit properly describing the changes made in a commit.
$ git commit -m "Commit message in quotes"
You can still make use of
git status to check the current state of the repository.
At this point, the next thing might be to push all changes made and files to Github.
The first thing you have to do is go to Github and create a new repository if you haven't created any before. This would give us rights to a particular repo.
- git remote This is used to connect a local repository with a remote repository.
Add remote repository
$ git remote <command> <remote_name> <remote_URL>
You can make use of
git remote -v to check/list remote repository
The final thing is to push to your repository.
- git push This is used to sends local commits to the remote repository. It requires two parameters: the remote repository and the branch that the push is for.
$ git push <remote_URL/remote_name> <branch>
That's all about working with local repositories, although there are some basic commands we haven't explained which I will explain in the next phase on working with remote repositories.
To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer. Git will create a directory locally with all files.
$ git clone <remote_URL>
To get the remote URL of whatever repo you want to clone, just go to Github and copy the URL.
Once that is done, you can now open the cloned folder, make your changes, and update the repository. Once you are done making local changes on your computer, use the following commands to push changes to your Github repository.
You can read about git branching here if you want to learn how to push to a new branch so as to avoid conflicts when pushing your codes back to the repository.
Make use of
git addas I explained earlier to stage your files. Then you can proceed to check the status of your repository by using
- git commit The next stage would be to commit all your changes as I explained earlier. Add a message to your commit as it's a best practice.
Once that is done the next thing would be to push your changes/files to Github.
To know which branch exists on your repo, make use of the
git branchcommand so you know which branch to push your changes to. You can also use
git remote -vto see the exact repo you are about to push to.
Then Finally, push your code.
I strongly recommend you check the article on Git branching so you see the importance of pushing your codes to a new branch to avoid conflicts.
As always, any questions or suggestions, please feel free to leave a response or tweet me 🤭! Be sure to connect with me on socials! 😎