CodeNewbie Community

Cover image for Git Essentials
Sankalp Swami
Sankalp Swami

Posted on

Git Essentials

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
LANADELREY

INSTALLING GIT

For MacOS

$ brew install git
Enter fullscreen mode Exit fullscreen mode

For Linux
Debian/Ubuntu

$ apt-get install git
Enter fullscreen mode Exit fullscreen mode

Fedora

$ yum install git
Enter fullscreen mode Exit fullscreen mode

For other Linux distributions, you can click here

HOW GIT WORKS

Working of Git
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"
Enter fullscreen mode Exit fullscreen mode

Then we will add our email address by the following command.

$ git config --global user.email "johndoe@example.com"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

now navigate to the directory

$ cd gitEssentials/
Enter fullscreen mode Exit fullscreen mode

Add some files to the directory.

$ touch index.js server.js index.html main.css
Enter fullscreen mode Exit fullscreen mode

now check the created files

$ ls
Enter fullscreen mode Exit fullscreen mode

ls
Initialize the working directory with git init command.

$ git init
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

git status command is used to check the current status of our working directory.
git status
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 .
Enter fullscreen mode Exit fullscreen mode

Lets check what git status has to show now
git status
So we have staged all the files. Lets create another file and name it to_Be_Deleted.js.

$ touch to_Be_Deleted.js
Enter fullscreen mode Exit fullscreen mode

Now check the status with git status.
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 .
Enter fullscreen mode Exit fullscreen mode

After adding, check the status using git status command.
Git status
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
Enter fullscreen mode Exit fullscreen mode

Thats it, we have unstaged the file successfully.
Lets check it using git status command.
git status
Yepp! We have unstaged the file.
Delete the file to_Be_Deleted.js

$ rm to_Be_Deleted.js
Enter fullscreen mode Exit fullscreen mode

Now we will be working on git commit command.
Lets commit our staged files.

$ git commit -m "console.log added to index.js file"
Enter fullscreen mode Exit fullscreen mode

commit
Lets check the status of our working tree after committing files.
git status
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

git show
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
Enter fullscreen mode Exit fullscreen mode

Now we will check the status of our working tree.
git status
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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

git commit
Now if we go for git log command, we will get the following data.
git commit
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
Enter fullscreen mode Exit fullscreen mode

Now stage the file using git add command.

$ git add .
Enter fullscreen mode Exit fullscreen mode

Now commit the file.

$ git commit -m "added body {} to main.css"
Enter fullscreen mode Exit fullscreen mode

git commit
We have committed the file successfully.
Now check the logs using git log command.

$ git log
Enter fullscreen mode Exit fullscreen mode

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.
new repo
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
Enter fullscreen mode Exit fullscreen mode

This gives the origin the path to store our local repository to the github repository.

$ git branch -M main
Enter fullscreen mode Exit fullscreen mode

Branches are really important in Git, we will talk about it in later part. For now just follow.

$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

This command pushes our local repository to the github repository.
git push
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 -
github repo

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now you can check branches by the git branch command
git branch
We have added a new branch successfully.
You can swithch branches using git checkout <branchName> command.
switch
For pushing your newly added branch to github you can use git push -u origin <branchName> command.
git push
branches
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
Enter fullscreen mode Exit fullscreen mode

git merge
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.

  1. Go to Github and click on add a README button. readme
  2. Edit the file the way you want and click on commit new file. commit
  3. Now go to your terminal and check for git logs. You will find 1 less git commit than your github commits.
  4. then with the help of git pull command you can pull the changes to your local machine. git pull 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 the git 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

Buy Me A Coffee

Thanks for reading, keep learning, Peace and Bubbyeye..!
And Yeah! get intoxicated by listening to her
LANADELREY

Discussion (0)