explanation of various Git commands with examples

Here’s an explanation of various Git commands with examples:

1. `git init`: Initializes a new Git repository in the current directory.


$ git init
Initialized empty Git repository in /path/to/repository/.git/

2. `git clone <repository>`: Copies a remote repository onto your local machine.


$ git clone https://github.com/example/repository.git
Cloning into 'repository'...


3. `git add <file>`: Adds a file or changes to the staging area.


$ git add myfile.txt


4. `git commit -m “<message>”`: Commits the changes in the staging area to the repository.


$ git commit -m "Added a new feature"
[master 0123456] Added a new feature
1 file changed, 1 insertion(+)


5. `git status`: Shows the current status of the repository.


$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)

modified: myfile.txt

no changes added to commit (use "git add" and/or "git commit -a")


6. `git diff`: Shows the differences between the current state of files and the previous commit.


$ git diff myfile.txt
diff --git a/myfile.txt b/myfile.txt
index abcdefg..hijklmn 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1,3 +1,4 @@
This is some content.
+Added new line.
More content.
Final line.


7. `git log`: Displays a chronological list of commits.


$ git log
commit 0123456789abcdef (HEAD -> master)
Author: John Doe <john@example.com>
Date: Mon Jan 1 12:00:00 2023 -0500

Added a new feature

commit 9876543210fedcba
Author: Jane Smith <jane@example.com>
Date: Sun Dec 31 11:00:00 2022 -0500

Fixed a bug


8. `git branch`: Lists all local branches in the repository.


$ git branch
* master


9. `git branch <branch-name>`: Creates a new branch with the specified name.


$ git branch feature


10. `git checkout <branch-name>`: Switches to the specified branch.


$ git checkout feature
Switched to branch 'feature'


11. `git merge <branch-name>`: Merges changes from the specified branch into the current branch.


$ git merge feature
Updating 0123456..abcdefg
myfile.txt | 2 ++
1 file changed, 2 insertions(+)


12. `git remote add <name> <url>`: Adds a remote repository with the given name and URL.


$ git remote add origin https://github.com/example/repository.git


13. `git remote -v`: Lists the configured remote repositories and their URLs.



$ git remote -v
origin https://github.com/example/repository.git (fetch)
origin https://github.com/example/repository.git (push)


14. `git push <remote> <branch>`: Sends the

local commits to the remote repository.


$ git push origin master


15. `git pull <remote> <branch>`: Fetches changes from the remote repository and merges them into the current branch.


$ git pull origin master


16. `git fetch`: Retrieves the latest changes from the remote repository without merging them.


$ git fetch origin


17. `git reset <commit>`: Discards commits, moving the branch pointer to the specified commit.


$ git reset HEAD~1


18. `git revert <commit>`: Creates a new commit that undoes the changes made in the specified commit.


$ git revert abcdefg


19. `git rm <file>`: Removes a file from the working directory and stages the deletion.


$ git rm myfile.txt


20. `git stash`: Temporarily saves changes that are not ready to be committed.


$ git stash


These examples provide a basic understanding of each Git command, but keep in mind that there are more options and use cases for each command. For a more comprehensive understanding, refer to the Git documentation or use `git –help` for detailed information about each command.

Leave a Reply

Your email address will not be published. Required fields are marked *