CLI AND GIT

Dheeraj Kumar
11 min readMar 7, 2021

CLI and GIT must be known by all developper aspirants.

First lets see 15 important commonly used CLI commands.

1. pwd

Use the pwd command to find out the path of the current working directory (folder) you’re in. The command will return an absolute (full) path, which is basically a path of all the directories that starts with a forward slash (/). An example of an absolute path is /home/username.

2. cd command

To navigate through the Linux files and directories, use the cd command. It requires either the full path or the name of the directory, depending on the current working directory that you’re in.

Let’s say you’re in /home/username/Documents and you want to go to Photos, a subdirectory of Documents. To do so, simply type the following command: cd Photos.

Another scenario is if you want to switch to a completely new directory, for example,/home/username/Movies. In this case, you have to type cd followed by the directory’s absolute path: cd /home/username/Movies.

There are some shortcuts to help you navigate quickly:

  • cd .. (with two dots) to move one directory up
  • cd to go straight to the home folder
  • cd- (with a hyphen) to move to your previous directory

On a side note, Linux’s shell is case sensitive. So, you have to type the name’s directory exactly as it is.

3. ls command

The ls command is used to view the contents of a directory. By default, this command will display the contents of your current working directory.

If you want to see the content of other directories, type ls and then the directory’s path. For example, enter ls /home/username/Documents to view the content of Documents.

There are variations you can use with the ls command:

  • ls -R will list all the files in the sub-directories as well
  • ls -a will show the hidden files
  • ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.

4. cat command

cat (short for concatenate) is one of the most frequently used commands in Linux. It is used to list the contents of a file on the standard output (sdout). To run this command, type cat followed by the file’s name and its extension. For instance: cat file.txt.

Here are other ways to use the cat command:

  • cat > filename creates a new file
  • cat filename1 filename2>filename3 joins two files (1 and 2) and stores the output of them in a new file (3)
  • to convert a file to upper or lower case use, cat filename | tr a-z A-Z >output.txt

5. cp command

Use the cp command to copy files from the current directory to a different directory. For instance, the command cp scenery.jpg /home/username/Pictures would create a copy of scenery.jpg (from your current directory) into the Pictures directory.

6. mv command

The primary use of the mv command is to move files, although it can also be used to rename files.

The arguments in mv are similar to the cp command. You need to type mv, the file’s name, and the destination’s directory. For example: mv file.txt /home/username/Documents.

To rename files, the Linux command is mv oldname.ext newname.ext

7. mkdir command

Use mkdir command to make a new directory — if you type mkdir Music it will create a directory called Music.

There are extra mkdir commands as well:

  • To generate a new directory inside another directory, use this Linux basic command mkdir Music/Newfile
  • use the p (parents) option to create a directory in between two existing directories. For example, mkdir -p Music/2020/Newfile will create the new “2020” file.

8. rmdir command

If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.

9. rm command

The rm command is used to delete directories and the contents within them. If you only want to delete the directory — as an alternative to rmdir — use rm -r.

Note: Be very careful with this command and double-check which directory you are in. This will delete everything and there is no undo.

10. touch command

The touch command allows you to create a blank new file through the Linux command line. As an example, enter touch /home/username/Documents/Web.html to create an HTML file entitled Web under the Documents directory.

11. locate command

You can use this command to locate a file, just like the search command in Windows. What’s more, using the -i argument along with this command will make it case-insensitive, so you can search for a file even if you don’t remember its exact name.

To search for a file that contains two or more words, use an asterisk (*). For example, locate -i school*note command will search for any file that contains the word “school” and “note”, whether it is uppercase or lowercase.

12. find command

Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory.

As an example, find /home/ -name notes.txt command will search for a file called notes.txt within the home directory and its subdirectories.

Other variations when using the find are:

  • To find files in the current directory use, find . -name notes.txt
  • To look for directories use, / -type d -name notes. txt

13. grep command

Another basic Linux command that is undoubtedly helpful for everyday use is grep. It lets you search through all the text in a given file.

To illustrate, grep blue notepad.txt will search for the word blue in the notepad file. Lines that contain the searched word will be displayed fully.

14. chmod command

chmod is another Linux command, used to change the read, write, and execute permissions of files and directories. As this command is rather complicated, you can read the full tutorial in order to execute it properly.

15. chown command

In Linux, all files are owned by a specific user. The chown command enables you to change or transfer the ownership of a file to the specified username. For instance, chown linuxuser2 file.ext will make linuxuser2 as the owner of the file.ext.

Now we know how to use basic commands lets dive into GIT

Git is basically for version control. Imagine, you are a developer and one night on January 1,2020 you got a brilliant idea to give a new feature to your project, and you have been working on it for few days. Finally you got to know that this feature you are developing is banned in your county and you wanna undo your changes, well actually to say, you wanna travel back in time to the night of January 1,2020 and make a different choice . Well, you can do this now using git.

Basically we save our project, this is called as commit. We can make a commit with comment. Each commit has many information as when and who made the commit. So in future you can see the description on commit and go back to that perticular commit.

When we are working with git , there are three areas where out work will flow.Those are working directory,staging area and Repository.

Remember this, repository will store all the commited files, and whenever you commit.

But if a file has to be commited(sent to Repository), it has to be worthy. In Technical terms being worthy means it has to be present in staging area.

So get the idea? Lemme re-explain it in a line.

If we have to save(Sent to repository) it has to be worthy(first it should be in staging area).

Now you must be thinking, “OK ,but before its worthy(before added to staging area) where it is?” . Well they are in working directory.

Yes, so whenever you create files or do some changes, it will be in working directory. Then we have to bring them to staging area by git add ,once they are in staging area, we have to put them to repository by git commit.

1. Git clone

Git clone is a command for downloading existing source code from a remote repository (like Github, for example). In other words, Git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer.

There are a couple of ways to download the source code, but mostly I prefer the clone with https way:

git clone <https://name-of-the-repository-link>

For example, if we want to download a project from Github, all we need to do is click on the green button (clone or download), copy the URL in the box and paste it after the git clone command that I’ve shown right above.

This will make a copy of the project to your local workspace so you can start working with it.

2. Git branch

Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing and deleting branches.

Creating a new branch:

git branch <branch-name>

This command will create a branch locally. To push the new branch into the remote repository, you need to use the following command:

git push -u <remote> <branch-name>

Viewing branches:

git branch or git branch --list

Deleting a branch:

git branch -d <branch-name>

3. Git checkout

This is also one of the most used Git commands. To work in a branch, first you need to switch to it. We use git checkout mostly for switching from one branch to another. We can also use it for checking out files and commits.

git checkout <name-of-your-branch>

There are some steps you need to follow for successfully switching between branches:

  • The changes in your current branch must be committed or stashed before you switch
  • The branch you want to check out should exist in your local

There is also a shortcut command that allows you to create and switch to a branch at the same time:

git checkout -b <name-of-your-branch>

This command creates a new branch in your local (-b stands for branch) and checks the branch out to new right after it has been created.

4. Git status

The Git status command gives us all the necessary information about the current branch.

git status

We can gather information like:

  • Whether the current branch is up to date
  • Whether there is anything to commit, push or pull
  • Whether there are files staged, unstaged or untracked
  • Whether there are files created, modified or deleted

5. Git add

When we create, modify or delete a file, these changes will happen in our local and won’t be included in the next commit (unless we change the configurations).

We need to use the git add command to include the changes of a file(s) into our next commit.

To add a single file:

git add <file>

To add everything at once:

git add -A

When you visit the screenshot above in the 4th section, you will see that there are file names that are red — this means that they’re unstaged files. The unstaged files won’t be included in your commits.

To include them, we need to use git add:

Important: The git add command doesn’t change the repository and the changes are not saved until we use git commit.

6. Git commit

This is maybe the most-used command of Git. Once we reach a certain point in development, we want to save our changes (maybe after a specific task or issue).

Git commit is like setting a checkpoint in the development process which you can go back to later if needed.

We also need to write a short message to explain what we have developed or changed in the source code.

git commit -m "commit message"

Important: Git commit saves your changes only locally.

7. Git push

After committing your changes, the next thing you want to do is send your changes to the remote server. Git push uploads your commits to the remote repository.

git push <remote> <branch-name>

However, if your branch is newly created, then you also need to upload the branch with the following command:

git push --set-upstream <remote> <name-of-your-branch>

or

git push -u origin <branch_name>

Important: Git push only uploads changes that are committed.

8. Git pull

The git pull command is used to get updates from the remote repo. This command is a combination of git fetch and git merge which means that, when we use git pull, it gets the updates from remote repository (git fetch) and immediately applies the latest changes in your local (git merge).

git pull <remote>

This operation may cause conflicts that you need to solve manually.

9. Git revert

Sometimes we need to undo the changes that we’ve made. There are various ways to undo our changes locally or remotely (depends on what we need), but we must carefully use these commands to avoid unwanted deletions.

A safer way that we can undo our commits is by using git revert. To see our commit history, first we need to use git log — oneline:

Then we just need to specify the hash code next to our commit that we would like to undo:

git revert 3321844

After this, you will see a screen like below — just press shift + q to exit:

The Git revert command will undo the given commit, but will create a new commit without deleting the older one:

The advantage of using git revert is that it doesn’t touch the commit history. This means that you can still see all of the commits in your history, even the reverted ones.

Another safety measure here is that everything happens in our local system unless we push them to the remote repo. That’s why git revert is safer to use and is the preferred way to undo our commits.

10. Git merge

When you’ve completed development in your branch and everything works fine, the final step is merging the branch with the parent branch (dev or master). This is done with the git merge command.

Git merge basically integrates your feature branch with all of its commits back to the dev (or master) branch. It’s important to remember that you first need to be on the specific branch that you want to merge with your feature branch.

For example, when you want to merge your feature branch into the dev branch:

First you should switch to the dev branch:

git checkout dev

Before merging, you should update your local dev branch:

git fetch

Finally, you can merge your feature branch into dev:

git merge <branch-name>

Hint: Make sure your dev branch has the latest version before you merge your branches, otherwise you may face conflicts or other unwanted problems.

So these are my 10 most-used git commands that I come across in my daily programming. There are many more things to learn about Git and I will explain them later in separate articles.

--

--