Important git commands with description

Important Git commands

Maximize your productivity with important Git commands. Learn push, pull, commit and more with examples and clear explanations. Git like a pro.

  1. Configuring Git

setting global user name and email, these are identification of who is working with the project(repository)

$ git config –global user.name “My Name”

$ git config –global user.email myEmail@example.com

Creating a new repository –git init command

initializing git local repository in your computer:

$  git init

Checking the status –git status

$ git status

Adding to the Staging area –git add

suppose hello.txt is our file and we have made some changes to it. Now, we need to add in the staging area before committing :

adding a specific file

$ git add <file-name>

Example:

$ git add hello.txt

adding all files:

$ git add –A     or  
$ git add  . 

Note: You can undo or un-stage a file before committing using the:

git reset <file>

this will remove it from the current index (the “about to be committed” list) without changing anything else.

You can also use:

git reset

This will unstage all due changes .

Committing–git commit command

git commit -m  “<message>”

Example:

$ git commit -m “Initial commit.”

Work with the remote repository: 

  1. Connecting to a remote repository –git remote add command

$ git remote add origin<remote repository url>

Example:

$ git remote add origin https://github.com/Md-MamunAbdulKayum/practice.git

2. Cloning a repository –git clone command

getting remote repository to local

$ git clone https://github.com/Md-MamunAbdulKayum/practice.git

    3.  Upload local change  to a server –git push command

git push origin <branch-name>

Example:

$ git push origin master

    4. Get changes from a server to local –git pull command

$ git pull 

or

git pull origin <branch-name>

Example:

$ git pull origin master

Working with Branch: 

  1. Creating new branch – git branch command

git branch <branch_name>

Example:

$ git branch dev

2. Switching to  a branch –git checkout command

$ git checkout <branch-name>

Example:

$ git checkout dev

3. Merging branches –git merge<branch_name>

Suppose I am in the master branch, marge with the dev  branch

$git merge dev

Delete a Local and remote Git branch

To delete a local branch use the following command. Syntax:

$ git branch -d <branch_name>
#or 
$ git branch -D <branch_name>

Here -d is an alias for –delete and -D is an alias for --delete --force. -D is used to delete a branch irrespective to the merge condition (i.g.: un-merged or merge conflict).

To delete a remote branch we use the following command:

$ git push origin --delete <branch_name>

After deleting the local and remote branches, other machines will still see the branch reference.

$ git branch -a

To get rid of this we need to do:

$ git fetch --all --prune

Delete Local Commit(s)

Delete the most recent commit without losing the work:

$ git reset --soft HEAD~1

Delete the most recent commit losing the work:

$ git reset --hard HEAD~1

Delete permanently all local commits and get the latest remote commit:

$ git reset --hard origin/<branch_name>

#Example;
#$ git reset --hard origin/master

See code examples in our GitHub repository.

Follow us on social media

Author