Basic git commands (Part 1)

Basic git commands  (Part 1)

Level 1 : Very basic one for git

Basic commandsWhat does it do
mkdirMake new directory
cdopen current directory
cd ..go back
lslists out all the files which are in working directory
ls -alists out all the files along with hidden files in working directory
git ls-fileslists out all the files which are in staged area
cat > file_name.extensioncreates a new file and lets you add content in it ( it is followed by Ctrl + Z to stop taking input)
mv source-files destination-directorymoves specified files which are in current directory to the destination directory

Vim

Just like nano and notepad, we have a text editor vim. On Window, vim is a default text editor in git bash. Let's get started with vim

Before making our first commit, we run:

vim

in the terminal.

It starts in "normal mode". It has very few commands to remember and they are as follows :

Command (Press these keys)Description
iTo insert/ type
EscTo exit the editing mode and get back to "normal mode"
:wTo save our work
:qTo exit vim
:xTo save and exit at a same time

Level 2 : Frequently used between current workspace, staging area, local repository

Git CommandsWhat does it do
git initAdds version control to a particular working directory
git addSending files from working directory to staging area
git commitSending files from staging area to local repository
git statusTo get the status of all the files
git status -sTo get precise status of files
git pushMoving files from local repository to remote repository
git cloneCreate a new local repository from the remote repository
git pullTo get updated file from remote repository to local repository

Extras in the above commands :

  1. git add

Git commands with example filesDescription
git add .Adds all the files in working directory
git add *.txtAdds all txt file
git add a.txt b.txtAdds specific files

  1. git commit

Git commands with example files (a.txt, b.txt)Description
git commit a.txt b.txtCommits the a.txt amd b.txt files. Commit by default is going to consider the files from staging areas. So you don't have to specify the file names.
git commit -m "This is a commit message"While committing it's mandatory to give commit message
git commit -a -m "This is a commit msg"If you want to add and commit a modified file then you can do both by using this command

  1. git config

Git commands with example filesDescription
git config user.name "" git config user.email ""This command will set user-name and email of current directory only
git config --global user.name git config --global user.emailAll the commit changes made to a file in local repository will be stored under this user.name and user.email at global level i.e. this user name and email will be used for all commit changes for all repository
git config --listTo list out all git configurations - Will display configuration details which are at global level - We specially use this to find user name and email

  1. git log

Some specific commands of git logWhat does it do
git logDisplay's history of all the commits of our local repository
git log --onelineDisplay one line commit of all the file history (This option is very helpful when we have a lot of commits and to identify commits based on message)
git log --helpto get specified information
git log file.txtto see log info about a particular file
git log -n 2 / git log -2-n option limits the number of commits to display. Here I have specified 2 so the latest two commits will be displayed over here.
git log -n 2 --onelinedisplay;s one line commit and last 2 commit
git log max-limit ="2"This also does the same thing as git log -n 2
git log --grep="Pattern"grep is used to search based on a given pattern in the commit message. Here the Pattern are those words which are in commit message
git log --decorateTo display meta information like branch name, HEAD, tag's information

To display commits from specific time/date :

  1. --since="YYYY-MM-DD"

  2. --after="YYYY-MM-DD"

  3. --after="3 hours ago"

Here after and since are same

To display all the commits till a specific time/date:

  1. --until

  2. --before

    git log --before="4 days ago" will display all the commits till 2 days ago.

To display commits based on author:

  1. --author =<pattern>

    git log --author="Gautami" will display all the commits made by gautami