The most useful git commands that every developer should know
Tags: git, devops
Published: 2019-11-27 06:56:37

In this developing and fast-growing technology world, it is very important that how you keep all your code and your changes save so to solve this problem Git came to the picture where you can keep your all code changes version wise safe. Basically, with the help of Git, you can track your code changes. In this blog, we are just going to discuss some of the terminal comments for Git that will always help you in day to day life. You can do the same things with Git Tools UI but I prefer to do these things from the command line because doing things in the command line is cool and faster.

#### what is Git? 
Git is a free and open-source distributed version control system. Git’s purpose is to keep track of projects and files as they change over time with manipulations happening from different users. Git stores information about the project’s progress on a repository. A repository has committed to the project or a set of references to the commits called heads. All this information is stored in the same directory as the project in a sub-directory called .git and will mostly be hidden by default in most systems.

#### Table Of Contents
[git config] (#git-config)
[git init] (#git-init)
[git status] (#git-status)
[git fetch] (#git-fetch)
[git branch] (#git-branch)
[git checkout] (#git-checkout)
[git add] (#git-add)
[git commit] (#git-commit)
[git push] (#git-push)
[git pull] (#git-pull)
[git diff] (#git-diff)
[git stash] (#git-stash)
[git merge] (#git-merge)
[git reset] (#git-reset)
[git cherry-pick] (#git-cherry-pick)


####  #git config <a name="git-config"></a>
The git config command sets the author's name and email address. Whatever you will set here that will appear with your commits.

```javascript
git config --global user.name "[name]" // To set author's name
git config --global user.email "[email]" // To set email address
```

####  #git init <a name="git-init"></a>
The git init command creates a new Git repository. You can use this to convert your existing project to a Git repository or initialize a new repository.

```javascript
git init repository_name
```

####  #git status <a name="git-status"></a>
The git status command displays the state of the working directory. You can check your branch, all your files whichever has changed or added and also you can check how many commits you have to push whatever you have committed but not pushed.

```javascript
git status
```

####  #git fetch <a name="git-fetch"></a>
The git fetch command basically refers from a remote repository into your local repository.

```javascript
git fetch remote_name branch_name // will only fetch the changes from that remote branch
git fetch remote_name // will only fetch the changes from remote origin
git fetch --all // will fetch all branches from all remotes
```

####  #git branch <a name="git-branch"></a>
The git branch command basically shows you a list of branches.

```javascript
git branch // list of all your local branch
git branch -r // list of all remote branches
git branch -a // list of all local and remote branches
```


####  #git checkout <a name="git-checkout"></a>
The git checkout command always allows you to switch into a different branch, create a new branch or reset your file changes.

```javascript
git checkout branch_name // checkout into a different branch
git checkout -b new_branch_name // create a new branch and switch into that
git checkout file_path // reset all your change from that file
git checkout . // reset all your file change 
```

####  #git add <a name="git-add"></a>
The git adds command is basically used to add your local changes.

```javascript
git add . // add your all local changes
git add directory_path // add all changes from a particular directory
git add file_path // add your changes from a particular file
```

####  #git commit <a name="git-commit"></a>
The git commit command is basically used to commit our code and log message about it.

```javascript
git commit -m "comment"
```

####  #git push <a name="git-push"></a>
The git push command is basically used to push our changes to the remote.

```javascript
git push
```

####  #git pull <a name="git-pull"></a>
The git pull command is basically used to pull out all the remote changes to our local.

```javascript
git pull 
```

####  #git diff <a name="git-diff"></a>
The git diff command is basically used to see your local changes line by line.

```javascript
git diff
```

####  #git stash <a name="git-stash"></a>
The git stash command is used to stash all our local changes from the current working directory and adds to the stash list.

```javascript
git stash // Stash the changes.
git stash list // list the stash entries that you currently have.
git stash pop // remove a single stashed state from the stash list and apply it on top of the current working directory
git stash apply // like pop, but do not remove the state from the stash list.
git stash clear // remove all the stash entries. 
```

####  #git merge <a name="git-merge"></a>
The git merge command is used to join branch changes into one. For merge first checkout with your branch where you want to take all the other branch changes then apply merge command.

```javascript
git merge --abort // can only be run after the merge results in conflicts. it will abort the merge process and try to reconstruct the pre-merge state. 
git merge --no-ff branch_name // create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.
```

####  #git reset <a name="git-reset"></a>
The git reset command is used to return the entire working directory to the last committed state.

```javascript
git reset --hard // will remove the last commit from the current branch, and your working directory
git reset --soft // will remove the last commit from the current branch, but the file changes will stay in your working directory.
```

####  #git cherry-pick <a name="git-cherry-pick"></a>
The git cherry-pick command is used to take out some commits from one branch and add it to another branch.

```javascript
git cherry-pick commit_id 
git cherry-pick commit_id -x // cherry-pick with all the commits message
```

I have written this blog to just remember all the commend that we are using in our day to day life, if I have missed anything then feel free to add that in the comment. I will appreciate all your comments and suggestions.