$show=/label

Top Git Commands With Examples - Developer Uses Everyday

SHARE:

A quick guide to how to use git commands and what are the top command that used by every developer in their daily life.

1. Introduction


In this tutorial, You'll learn what are the git commands that can be used in everyday life. You'll see the top git commands with examples. You can not imagine a developer's life without using version control tools such as git, bitbucket, or any tool.

Because this makes like simple and easy to maintain the programming files and collaborate with your teammates.

Most of you do not use git commands either you use GUI plugins in IDE eclipse or Intelleji tools

But, if you know all these handy command and you will get confidence in dealing with the conflicts resolving manually from the terminal.

Top Git Commands With Examples



Git Tutorials

2. Handy Git Commands


Git Commands: Let us see what are the commands that are mostly used from the terminal or command prompt such as git bash in windows. At least the following should be known by all developers and most used in the software industry. We'll discuss one by one from scratch.

Top Git Commands With Examples

Git Commands List:

  • git config
  • git init
  • git clone
  • git help
  • git status
  • git add
  • git branch
  • git commit
  • git push
  • git pull
  • git checkout
  • git stash
  • git merge
  • git reset
  • git remote
  • git diff
  • git rm
  • git log
  • git show
  • git tag


3. Git Config Command With Example


This is the most important one but this is used only once when you join the new company or get a new laptop in the office. This is only a one-time job but if you want to change at any time such as name, email address using the "git config" command as below.

Names, email values can be set at the global, system, or user level.

Example Commands to set the user name and email address.

git config --global <property-name> <value>

git config --global user.name "JavaProgramTo.com"

git config --global user.email "admin@javaprogramto.com"

$ git config --global user.name "JavaProgramTo.com"
$ git config --global user.email "admin@javaprogramto.com"
$ git config --global user.email 
admin@javaprogramto.com
$ git config --global user.name
JavaProgramTo.com


To see all the user configurations have been set in your machine use the "git config --list  | grep 'user'"


git config --list  | grep 'user' 
user.email=admin@javaprogramto.com

user.name=JavaProgramTo.com



4. Git Init Command With Example


"git init" is to convert a project into a git project. To see the current folder is under git or not using git status command or if the ".git" folder is present in the root directory it is git project.

If not a git project, just run the "git init" command at the root folder to convert into a git project.

git init



[$ pwd/Users/Documents/workspace/git-demo$ git statusfatal: not a git repository (or any of the parent directories): .git$ ls -a. .. file1.txt$ git initInitialized empty Git repository in /Users/Documents/workspace/git-demo/.git/$ ls -a. .. .git file1.txt$ git statusOn branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)]

5. Git clone Command Example


If you want to clone any repo from GitHub or bitbucket or remote location then use 'git clone "repo location"'

git clone <remote-repo-location>

$ git clone https://github.com/JavaProgramTo/CoreJava.git
Cloning into 'CoreJava'...
remote: Enumerating objects: 97, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 97 (delta 14), reused 95 (delta 12), pack-reused 0
Unpacking objects: 100% (97/97), done.

And also you can clone into a new folder. Here CoreJava git repo will be cloned into a new folder core-java-tutorials

$ git clone https://github.com/JavaProgramTo/CoreJava.git core-java-tutorials
Cloning into 'core-java-tutorials'...
remote: Enumerating objects: 97, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 97 (delta 14), reused 95 (delta 12), pack-reused 0
Unpacking objects: 100% (97/97), done.
$ls
CoreJava core-java-tutorials file1.txt


6. Git Help Command Example


If you don't know much about any command then. use "git help <command-name>" it pulls syntax and all its options.

git help <command-name>

$ git help init

It displays into a new editor as below and press 'q' to come out to the terminal.

Git Help Command Example


7. Git Status Command Example


To see the list of files are modified and added to the stage in the current directory. And also show the current branch name.

git status

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/
file1.txt

nothing added to commit but untracked files present (use "git add" to track)

Git Status Command Example

8. Git Add Command Example


'git add' command to add new files or existing modified files to the git stage. This works from current directory.

After 'git add' command execution, files will be moved from untracked files to "changes to be committed"

git add <file-1-name>

$ git add file1.txt 
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

new file:   file1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/


Git Add Command Example

Even you can add multiple files by delimiting the file names with space or use '*' to add all files to the stage.

git add <file-1-name> <file-2-name>

for

git add *

9. Git Commit Command Example


'git commit' command commit the files in stage area (Changes to be committed section) and creates a new commit id for this commit.

But these files are now not available on the git. At this stage, all files are on in your local machine as now.

git commit -m <commit-name>

$ git commit -m 'first commit'
[master (root-commit) 8dbddf5] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
Venkateshs-MacBook-Pro-2:git-demo venkateshn$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/

nothing added to commit but untracked files present (use "git add" to track)


Git Commit Command Example



10. Git push Command Example


After commit, use the 'git push' command to move your changes to a remote repo that means from where repo is created.

git push 

git push origin <branch-name> -> pusing the changes to a specific branch

git push -all --> pushing all branch changes to remote git.

$ git push

11. Git Pull Command Example


If you have other changes those are committed by your teammate and to get the latest changes to use 'git pull' command.

git pull -> pulls from the current branch

git pull <repo-url> -> pull change from the given repo

$ git pull

12. Git Branch Command Example


To see all branches under this repo, use 'git branch'

git branch

$ git branch
* master
develop

To see, all the remote branches using 'git branch -r'

git branch -r -> to see remote branches

git branch <new-branch> release -> to create a new branch from release branch.

13. Git Checkout Command Example


If you have modified the file in local repo and you want to replace all the changes with the remote file, use 'git checkout <file-name>'

$git checkout file1.txt


14. Git Stash Command Example


If you modified files in your local and do not want to commit immediately then use 'git stash'.

Now, modified files will be shown in the 'git status' command.


$ vi file1.txt 
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   file1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/

no changes added to commit (use "git add" and/or "git commit -a")
$ git stash
Saved working directory and index state WIP on master: 8dbddf5 first commit
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/

nothing added to commit but untracked files present (use "git add" to track)

Added again some files to git stash. 'save' is optional and by defaults it does save.

$ git stash save

To see all the commits in the git stash use 'git stash list'

$ git stash list
stash@{0}: WIP on master: 8dbddf5 first commit
stash@{1}: WIP on master: 8dbddf5 first commit

To get the latest changes from stash to stage, use 'git stash pop'.

$ git stash pop
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   file1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/
file2.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9faef149414d5c6ab7047ed493c77a3b444e04e6)

Now see the list of all commits in the stash with the command 'git stash list'

$ git stash list
stash@{0}: WIP on master: 8dbddf5 first commit


15. Git Branch Command Example


'git branch' command list all branches available in this repo.

This shows all branches untill your last 'git pull'. After, it pulls if any new branches created on the remote branch, and those can not be seen from the local branch.

$ git branch
  develop
* master

To see only remote branches use "git branch -r" and use "git branch -a" to all remote and local branches.

$ git branch -r
  origin/dummy
  origin/master
$ git branch -a
  develop
* master
  remotes/origin/dummy
  remotes/origin/master
$ git branch
  develop
* master


16. Git Merge Command Example


If you want to merge another branch to the current branch then use the 'git merge' command.

Making the changes to develop branch and merging develop branch changes to the master branch.


$ git status
On branch develop
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

modified:   file1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/
file2.txt

Venkateshs-MacBook-Pro-2:git-demo venkateshn$ git commit -m 'added line 2'
[develop 17569dd] added line 2
 1 file changed, 3 insertions(+)


Merging develop branch commits into the master branch.

git merge develop -> merges the changes of develop branch into the current branch.

$ git checkout master
Switched to branch 'master'
$ git merge develop
Updating 8dbddf5..17569dd
Fast-forward
 file1.txt | 3 +++
 1 file changed, 3 insertions(+)


Git Merge Command Example


17. Git Log Command Example


To see all the commits version history in any branch or current branch, use 'git log' command.

$ git log
commit 94c3fcb44b57cc13a3bdadaabe708e1d0e38a16e (HEAD -> master)
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 22:24:55 2020 +0530

    adding file2

commit 17569dd273defd6ef7a4de554f20a966504fa629 (develop)
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 22:17:12 2020 +0530

    added line 2

commit 8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 21:21:42 2020 +0530

    first commit

By default, the 'git log' command shows all the commits for the current branch but it provides different flags to do specific set of operations.

To see the file commits for a specific file then use 'git log --follow <file-name>'

To see the commits for file1.txt

$ git log --follow  file1.txt 
commit 17569dd273defd6ef7a4de554f20a966504fa629 (develop)
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 22:17:12 2020 +0530

    added line 2

commit 8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 21:21:42 2020 +0530

    first commit

To see all the commits for file2.txt

$ git log --follow  file2.txt 
commit 94c3fcb44b57cc13a3bdadaabe708e1d0e38a16e (HEAD -> master)
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 22:24:55 2020 +0530

    adding file2


git log to see the commits for a specific file


18. Git Diff Command Example


To see the difference among the modified files with remote files using 'git diff' command.

$ git diff
diff --git a/pom.xml b/pom.xml
index f47e51e..8702bb4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,7 +19,7 @@
        </properties>

        <dependencie
-
+               <!-- spring boot dev tools-->
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>

Now, add the modified file to git.

$ git add pom.xml 
$ git diff
$

Now, git diff does not show the differences but still, you can see the diff wit flag '--staged'.

git diff --staged

$ git diff --staged
diff --git a/pom.xml b/pom.xml
index f47e51e..8702bb4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,7 +19,7 @@
        </properties>

        <dependencie
-
+               <!-- spring boot dev tools-->
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>

Another interesting way to see the differences between the two branches.

$ git diff master develop


19. Git Reset Command Example


To remove the file from stage and file should be in the local system.

Look at the in-depth article on "git reset and git rm" commands to unstage a file.

Syntax: git reset <file-name>

$ git reset file1.txt

To unstage and remove all the files from a specific commit. But, all the files from this commit will remain as modified in the local machine.

git reset <commit-id>

$ git reset 6873c7a6c1135b3a977c9d14404e0fd652f566a8
Unstaged changes after reset:
M pom.xml

To discard all the changes from a commit and goes back to the specific commit.

$ git reset --hard 8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10
HEAD is now at 8dbddf5 first commit
$ ls
CoreJava core-java-tutorials file1.txt
$ 

After resetting to the first commit, we are not seeing the file2.txt in the local file system. Because, if you use a 'hard' flag then it removes all history of the files.

You must be careful when running git commands with a 'hard' flag.

20. Git Rm Command Example


This command deletes the file from your current local working directory and from the stage as well. Once you do commit and push then file will be removed from git repo permanently.


If you do not use any flag then 'git rm' command just remove the file.

$ ls
CoreJava core-java-tutorials file1.txt
$ git rm file1.txt 
rm 'file1.txt'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

deleted:    file1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/

$ls
$

'git rm' command does not work with the staged file and works on only committed files.

Created now a new file3.txt and added to git. Then tried to remove using file3.txt file with 'git rm file3.txt'. This result in error saying use '--cached' or '-f' flag.

$ git rm file3.txt 
error: the following file has changes staged in the index:
    file3.txt
(use --cached to keep the file, or -f to force removal)
$

"--cached" flag is used to remove the file from staging and reserves the changes.

$ git rm --cached file3.txt 
rm 'file3.txt'

$ git status
On branch master
Changes to be committed:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
file3.txt


git rm -f file3.txt - Just deleted the file from local machine.

$ git add file3.txt 
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

deleted:    file1.txt
new file:   file3.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

CoreJava/
core-java-tutorials/


$ git rm -f file3.txt 
rm 'file3.txt'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
Untracked files:
  (use "git add <file>..." to include in what will be committed)

$



21. Git Tag Command Example


This command is used to give tags to the specified commit.

git tag <commit-d>

$ git tag 8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10
$ git tag 
8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10

'git tag' command to see all the tags for a branch.


22. Git Show Command Example


"git show" command shows the files metadata sections of contents changed in the commit. By default, it shows the result for the latest commit from the 'git log' command.

$ git show
commit 68c7e111b3995a5104b1651550f29874733f2a96 (HEAD -> master, tag: 8dbddf5a2a5d2c4d14ceec7a4ea74db994b8dd10)
Author: JavaProgramTo.com <admin@javaprogramto.com>
Date:   Sun May 10 22:51:59 2020 +0530

    file one modified

diff --git a/file1.txt b/file1.txt
index e69de29..b806426 100644
--- a/file1.txt
+++ b/file1.txt
@@ -0,0 +1 @@
+adding line 1

Highlighted in yellow color is the contents modified.

To see only file names for a commit.

use flag '--name-only' in 'git show' command.

git show --name-only <commit-id>

$ git show --name-only c97949d3c04593bdc72478038c4ff76b17de9c8a
commit c97949d3c04593bdc72478038c4ff76b17de9c8a
Author: java-w3schools <venkivenki4b6@gmail.com>
Date:   Sat May 9 23:36:22 2020 +0530

    Spring Boot Data MongoDB: Projections and Aggregations Examples

pom.xml
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/MongoDbSpringBootCurdApplication.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/config/MongoDBConfig.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/controller/projection/AggregationController.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/controller/projection/EmployeeMongoRepoController.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/controller/projection/MongoTemplateProjectionController.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/model/AgeCount.java
src/main/java/com/javaprogramto/springboot/MongoDBSpringBootCURD/repository/EmployeeRepository.java
src/main/resources/application.properties
$


23. Conclusion


In conclusion, We've seen most used git commands in the software developer life every day from git bash or terminal.

If you know, all these commands then you can use git commands wisely.

If you have any questions in "Git Commands", please post in the comments section. We will answer soon.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,14,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,80,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,77,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,10,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Top Git Commands With Examples - Developer Uses Everyday
Top Git Commands With Examples - Developer Uses Everyday
A quick guide to how to use git commands and what are the top command that used by every developer in their daily life.
https://1.bp.blogspot.com/-TGvq0t5c794/XrhQdi8ST_I/AAAAAAAACtA/k_0ThqJ7CkA0SXaED3zU0g5UsxSlsCY_ACLcBGAsYHQ/s640/Top%2BGit%2BCommands%2BWith%2BExamples.png
https://1.bp.blogspot.com/-TGvq0t5c794/XrhQdi8ST_I/AAAAAAAACtA/k_0ThqJ7CkA0SXaED3zU0g5UsxSlsCY_ACLcBGAsYHQ/s72-c/Top%2BGit%2BCommands%2BWith%2BExamples.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/05/top-git-commands-examples.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/05/top-git-commands-examples.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content