Understanding essential git commands

git init, status, add, commit -m, push (fork, pull, branch, merge), branch, revert, log, log –pretty, merge, diff

Using git init

The git init command sets up all the components which GIT needs to begin tracking changes to the project (ie. it will initialize your working directory).

With git status command you will check the status of the changes in your working directory. When you run this command and see untracked files, it means that GIT sees that files in your working directory, but has not started tracking changes yet.

Using git add

With git add command you will add the files to the staging area. This means that GIT starts to track the changes for those files. You may use

git add <file_name>

command to start track individual file, or use

git add .

command to start tracking all changes within your working directory. By using the git command it is said that you are moving your changes within staged area.

Using git commit

With the git commit command (followed by a message, -m flag) you tell GIT to permanently store the changes from the staging area inside your local repository. You can specify the -m parameter followed by a message.

Commits are stored chronologically in the repository and can be viewed with the git log command.

Using git log

Running git log command will show a list of the recent commits with information about them. Each commit contains the following information:

  • an ID which uniquely identifies the commit. It is a 40 characters code, called SHA-1 that uniquely identifies the commit. Usually it appears in orange text;
  • an author;
  • a date. This is the date and time of the commit;
  • and a message associated with the commit.

You can use –stat (or stats) option to.

You can use git log –pretty=oneline to output each GIT commit in a single line.

To stop viewing git log output, press q (which stands for quit).

Using git diff

Running git diff command followed by two commit IDs will compare those two commits (ie. the two versions of the code). Therefore git diff command operates on two commits, and it form is like:

git diff commitID_1 commitID_2

Using git diff with no arguments compares working directory with the staged area. Using git diff –staged compares the staged area to the most recent commit on repository

Using git checkout

Running git checkout followed by a commit ID will reset of your file to have the state before that commit was done.

Using git status

The git status command shows what files were changed since the last commit. In other words it shows untracked files and file changes staged for the commit.

Using git add

The git add is the command to add files to the staging area. You can specify -m flag to add a message associated to your commit.

Using git reset

The git reset command is used to remove files from the staging area. For example, lets say you accidentally add lesson.txt file to the staging area, you remove it using git reset lesson.txt command. The file will be removed from the staging area, but it will still be in your working directory.

You may also use the command with –soft parameter, as follow:

git reset –soft <commit_id>

Using git revert

The git revert command with only one commit at a time. You have to specify the commit hash.

Using git branch

Branches are like labels for the commits. Running git branch will show you the branches and with a *, the branch to which you are connected.

You can create a new branch by running git branch dummy-branch command. This will create a new branch having the name dummy-branch. You can checkout the newly created branch by running git checkout dummy-branch command.

You may also run the git brach -av command to list all the branches in your local repository.

Using git remote

With git remote command you can see a list of existing remote repositories. Type git remote -v

List of all GIT commands:

git init

git add

git commit

git log

for a complete list see Screen Shot 2018-05-17 at 15.36.40.png

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply