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.

Example:

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

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

Example:

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

 

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

Example:

$ git add myfile.txt

 

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

Example:

$ 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.

Example:

$ 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.

Example:

$ 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.

Example:

$ 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.

Example:

$ git branch
feature
* master

 

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

Example:

$ git branch feature

 

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

Example:

$ git checkout feature
Switched to branch 'feature'

 

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

Example:

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

 

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

Example:

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

 

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

Example:

 

$ 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.

Example:

$ git push origin master

 

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

Example:

$ git pull origin master

 

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

Example:

$ git fetch origin

 

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

Example:

$ git reset HEAD~1

 

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

Example:

$ git revert abcdefg

 

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

Example:

$ git rm myfile.txt

 

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

Example:

$ 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 *