INTRODUCTION
We just cant imagine our life without git
and Github
. Being a developer it is must to understand Git. Git is the most commonly used version control system. Git tracks all the changes you make to your files so that you have record of all the changes that has been done. In this read, you will work on git practically and will gain the basics of Git.
Getting consistent with the work is the only way to achieve the glory and for consistency we need to work, work and work and for that sometimes even we need a therapy and for me, My therapy is listening to her songs
INSTALLING GIT
For MacOS
$ brew install git
For Linux
Debian/Ubuntu
$ apt-get install git
Fedora
$ yum install git
For other Linux distributions, you can click here
HOW GIT WORKS
It start with the Initialization. We need to initialize our working directory as Git Directory with the command git init
. Once we have initialized the git repository we can add our files to staging area. Staging is nothing but tracking files from the working directory. We can add untracked files to staging area with the command git add <filename>
or git add .
to add all the files from that particular directory. We cant commit untracked files from the working directory until and unless we dont stage them. The commit is used to save your changes to local repository. Every time you save that file, all the changes made are tracked. Git commit command takes the snapshot recording the staged changes. We can commit a file by the following command git commit -m "<message>"
, message
should be related to the changes made in that particular file at the time of staging. Once all the files are staged and commited, then we can push the local repository to the code hosting platform like Github for collaborations and to avoid losing all our program files due to any kind of failure of our local machine.
GIT SETUP
So that now we have installed git on our machine, lets get started with git with our identity.
Setup git with the git config
command.
First we will add username by the following command.
$ git config --global user.name "JohnDoe"
Then we will add our email address by the following command.
$ git config --global user.email "johndoe@example.com"
Thats it. We have setup our git. Let work on git practically.
LETS DO IT PRACTICALLY
Open terminal and make a new directory with following command
$ mkdir gitEssentials
now navigate to the directory
$ cd gitEssentials/
Add some files to the directory.
$ touch index.js server.js index.html main.css
now check the created files
$ ls
Initialize the working directory with git init
command.
$ git init
Now add some lines to your index.js file so that we can add them to staging area.
$ echo "console.log('Git Essentials');" >> index.js
git status
command is used to check the current status of our working directory.
As we can see there are no commits yet and we have 4 untracked files.
Now with the command git add
, we will track/stage the files.
$ git add .
Lets check what git status
has to show now
So we have staged all the files. Lets create another file and name it to_Be_Deleted.js
.
$ touch to_Be_Deleted.js
Now check the status with git status
.
This is what we get now. It shows that we have 1 untracked file. Let stage the untracked file using git add
command.
$ git add .
After adding, check the status using git status
command.
We have successfully staged our newly created file to_Be_Deleted.js
.
Now we will learn how to unstage an file.
For unstaging an file we will use git rm --cached <file>
command.
Lets unstage the to_Be_Deleted.js
file.
$ git rm --cached to_Be_Deleted.js
Thats it, we have unstaged the file successfully.
Lets check it using git status
command.
Yepp! We have unstaged the file.
Delete the file to_Be_Deleted.js
$ rm to_Be_Deleted.js
Now we will be working on git commit
command.
Lets commit our staged files.
$ git commit -m "console.log added to index.js file"
Lets check the status of our working tree after committing files.
So as we have committed all the files we get the message nothing to commit, working tree clean
. That's cool.
Lets check the logs using git log
command. This command is super handy.
$ git log
It shows the complete commit history with commit id, author, time and commit message.
Using git show
command we can track the changes made on that particular commit using the commit id.
$ git show 67c737e925fd76918444494f752a65fce5d6a55e
With the help of git show <commitId>
command, we can track all the changes commited with that particular commit.
Now if we edit our files, how do we amend the commit. Lets work on it.
First of all we will be editing our index.js
file.
$ echo "const age = 18;" >> index.js
Now we will check the status of our working tree.
So now we are getting modified message for index.js
file which is unstaged.
Now we will check what changes has been added to the file using git diff
command.
$ git diff
So the changes are shown with +
sign as we can see it over here +const age = 18
.
Let track the file with git add
command and then commit it with different message. For changing the commit message we can use the git commit --amend -m "<message>"
command
$ git commit --amend -m "added const age"
Now if we go for git log
command, we will get the following data.
As you can see the message got changed to added const age
.
Now we will edit some other files.
add the following command to edit your main.css
file.
$ echo "body {}" >> main.css
Now stage the file using git add
command.
$ git add .
Now commit the file.
$ git commit -m "added body {} to main.css"
We have committed the file successfully.
Now check the logs using git log
command.
$ git log
Yep now we are having two commits in our git logs.
GITHUB
Github is a code hosting platform. It is web-based platform used for version-control. It becomes super handy when it comes to collaborating on projects and working with other people.
Lets add new repository on github which is really easy.
Once you create new repository you go to this page.
And as we already have an existing repository we will copy lines from β¦or push an existing repository from the command line
.
$ git remote add origin git@github.com:SwamiSankalp/gitEssentials.git
This gives the origin the path to store our local repository to the github repository.
$ git branch -M main
Branches are really important in Git
, we will talk about it in later part. For now just follow.
$ git push -u origin main
This command pushes our local repository to the github repository.
We have successfully added our local repository to github.
Once done, you can refresh your github page, you will find your local repo over ther -
BRANCHES IN GIT
Branches in git are nothing but an independent line of development, ie; When there are multiple developers working on the same project everyone can have their own branches which will keep the code base safe.Git branches come to the rescue at many different places during the development of a project. There is one default main
branch. You can add your commited files to your own branches and can submit it to your senior dev for review. If it gets approval then your branch is merged with the default main
branch.
$ git branch
As we can see we are having only one branch that is by default main
branch.
git branch <branchName>
command is used for adding a new branch.
$ git branch feature-xyz
Now you can check branches by the git branch
command
We have added a new branch successfully.
You can swithch branches using git checkout <branchName>
command.
For pushing your newly added branch to github you can use git push -u origin <branchName>
command.
As you can see we have successfully pushed the branch to github.
We can merge both the branches using git merge
command.
Now we will be merging our branches.
$ git merge feature-xyz
If some changes are made directly on github repository and if we want that code on our local machine, we can use git pull
command.
Lets change some code on github repo and lets pull it to our local machine.
We will add readme file on github repo. Readme file is used for adding some basic instructions for that particular repository.
- Go to Github and click on
add a README
button. - Edit the file the way you want and click on
commit new file
. - Now go to your terminal and check for git logs. You will find 1 less git commit than your github commits.
- then with the help of
git pull
command you can pull the changes to your local machine.Yepp we have successfully pulled the changes from github repo to our local git repository.
SOME MORE COMMANDS
- Using the
git config --global core.editor vim
command to set the preferred editor when triggering thegit commit -a
command. - Using the
git config --list
to present all defined config lists. - Using the
git remote
command to present defined remote Git server URL lists. - Using the
git remote get-url remote_url_name
command to present current remote name to be the Git server URL.
CONCLUSION
I hope now we are having a good knowledge about git and github and have gained good practical knowledge to use it efficiently.
Let me know in the comment section if you have any doubt or feedback.
πSUPPORT
Thanks for reading, keep learning, Peace and Bubbyeye..!
And Yeah! get intoxicated by listening to her
Top comments (0)