DEV Community

krantikr
krantikr

Posted on

The most useful git commands that every developer should know

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 init
git status
git fetch
git branch
git checkout
git add
git commit
git push
git pull
git diff
git stash
git merge
git reset
git cherry-pick

#git config

The git config command sets the author's name and email address. Whatever you will set here that will appear with your commits.

git config --global user.name "[name]" // To set author's name
git config --global user.email "[email]" // To set email address
Enter fullscreen mode Exit fullscreen mode

#git init

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.

git init repository_name
Enter fullscreen mode Exit fullscreen mode

#git status

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.

git status
Enter fullscreen mode Exit fullscreen mode

#git fetch

The git fetch command basically refers from a remote repository into your local repository.

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
Enter fullscreen mode Exit fullscreen mode

#git branch

The git branch command basically shows you a list of branches.

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
Enter fullscreen mode Exit fullscreen mode

#git checkout

The git checkout command always allows you to switch into a different branch, create a new branch or reset your file changes.

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 
Enter fullscreen mode Exit fullscreen mode

#git add

The git adds command is basically used to add your local changes.

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
Enter fullscreen mode Exit fullscreen mode

#git commit

The git commit command is basically used to commit our code and log message about it.

git commit -m "comment"
Enter fullscreen mode Exit fullscreen mode

#git push

The git push command is basically used to push our changes to the remote.

git push
Enter fullscreen mode Exit fullscreen mode

#git pull

The git pull command is basically used to pull out all the remote changes to our local.

git pull 
Enter fullscreen mode Exit fullscreen mode

#git diff

The git diff command is basically used to see your local changes line by line.

git diff
Enter fullscreen mode Exit fullscreen mode

#git stash

The git stash command is used to stash all our local changes from the current working directory and adds to the stash list.

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. 
Enter fullscreen mode Exit fullscreen mode

#git merge

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.

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.
Enter fullscreen mode Exit fullscreen mode

#git reset

The git reset command is used to return the entire working directory to the last committed state.

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.
Enter fullscreen mode Exit fullscreen mode

#git cherry-pick

The git cherry-pick command is used to take out some commits from one branch and add it to another branch.

git cherry-pick commit_id 
git cherry-pick commit_id -x // cherry-pick with all the commits message
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
matt profile image
Matt seymour

Newer versions of GIT now include switch it would be good if you add this to the list.

Collapse
 
siatwe profile image
Simon Weis

Hi, nice article.
I found a small error with git config: the global is written with 2 hyphens.