Table of contents
Having known the meaning of git in this article, right here let us also see some of the commands used by git.
- The command line is the only place you can run all git commands. If you have windows, you can open the terminal in command Prompt or powershell or install some command line tools like git bash. Otherwise if you have MacOs or Linux you can just open your terminal.
- All interactions with git start with the dollar sign and then git
$ git
. Though this is not enough . It will actually just return to you a bunch of options like the ones in the image below incase you only typed that command therefore we need to pass more options to git for it to know what we want it to do.
Since we have successfully installed git on our computers, the next step is to configure git locally on our machines. This covers common configuration settings like email, username, and editor. The following are their commands:
- use
git config --global user.name "EnterYourUserName"
to configure your username globally - use
git config --global user.email “EnterYourEmail”
to configure your email globally - use
git config --global core.editor “code --wait --new-window”
to configure a default editor.
In this case our default editor is visual studio code ( represented by code). The wait and new window are parameters specifying that you always want to use code in a new window. - use
git config --edit --global
to visualize all these global settings you have created.
Git will wait to continue until the opened code window has been closed when we run the command git config --edit --global
(as seen from the hint in the image above)
Below is the opened vs code
Using git in our folder
In order to get started using git inside our folder and file, we need to :
- create a new directory which will be initialized into a git repository .
- Then add a file to our directory .
- Finally we need to commit our changes.
Committing the changes will save changes to the local repository which is our goal at this point and it can't be done alone so we need to do all the above in their order. lets go!!
Inside our terminal, we need to create a new directory (also known as a folder) using the
mkdir
command it stands for Make Directory. for examplemkdir first
creates a new directory called first.Since we want to initialize git inside this new directory, then we have to change the directory we are currently working in by using the
Thecd
command. For examplecd first
and then we use thegit init
command that initializes git inside the directory hence converting the directory into a git directory.git init
command creates an empty repository as well. The net result of executing this command will be that the .git directory will be created in the specified directory ( first ) with a number of sub directories all internal to git.
When git is initialized, a default branch is created this could be main or master and if we want to change the default branch name, use the command
git config --global init.defaultBranch <name>
The .git directory is also a git database, the place that git uses to keep track of the changes made by you or someone else in the remote repository.
To visualize this, you can use the ls command and pass in la i.els -la
- We can now run the
git status
command inorder to ask git the status of our current working directory. And per now it shows us that the branch we are currently working on ( master for my case) , no commits yet , and nothing to commit . - It also tells us that we can create / copy files and use git add to track.
- Let us do that next.
- To create a new file, we will use the
code
command to invoke visual studio code and pass in the name of the file we want to create . The command is
code readme.md
readme.md is the file i want to create, the extension md stands for the markdown format.- This command creates a file with the specified file name and you can type in anything that you want.
The image below shows the file being opened in visual studio code
- If we run the git status command again, we shall have an untracked file which is the readme.md file that is waiting inside of the directory meaning its not yet looked at by git
Note: As we are running these commands, we are still in the same directory ( first ) that we created.
- We are now going to add a file to git and this is done by calling the
git add
command . we also need to specify the filename so we shall writegit add readme.md
. This will then bring the file to git or the staging area . - When we check the status again, git status has gained change . Therefore at this point, git is tracking the file but its not yet part of the commit yet and it also gives us an option to unstage the file .
- Only when the
git commit
command is called, its when a new commit will be created and the new file will be part of the new commit. - The commit is a bundle of all the files that we are going to store in the current state in the git database.
- This is done by using the command
git commit -m " Enter any message"
m - stands for commit message which is specified and it is a piece of text we write in quotes to indicate the changes we have done.
- Now a new commit has been created locally on the machine in which one file has changed and a new file has been inserted which is the readme.md file.
- If we do another git status, we are back on where we started, the branch hasn't changed , nothing is ready to be committed at this point, The working tree is clean.
Sometimes we may have alot of files (morethan one file ) that we would want to stage. we can do this by adding a dot on the git add saying that all filed can just be added. The command is
git add .
What if we want to see our git history? Well, git also has a command for that and that is
git log
. The command shows the commit , the branch, author, date and the file name as shown below.
- That's how we add files to the git database on our computer (we have saved changes to the local repository)
Summary of the common git commands
$ git config
: Allow us to get and set configuration options.$ git init
: Initialize a git local repository.$ git add
: Add a file to the staging area.$ git commit
: Commit a file to the repository.$ git clone
: Download a project from remote . It will clone a repository into a local directory on the machine and will also create a remote tracking branch and checkout an initial branch.
We have reached our goal 😊😊 Happy coding !!!