Git, a distributed version control system, has revolutionized the way developers collaborate and manage code. Understanding its commands is paramount for leveraging its full potential. This comprehensive guide delves into 25 commonly used Git commands, providing detailed explanations, code examples, and practical applications.
1. git init
Purpose: Initializes a new Git repository in the current directory.
Syntax:
git init
Example:
$ cd my-project
$ git init
2. git status
Purpose: Displays the status of the working directory and staging area.
Syntax:
git status
Example:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
3. git add
Purpose: Adds files to the staging area, preparing them for commit.
Syntax:
git add <file>...
Example:
$ git add README.md
4. git commit
Purpose: Creates a new commit with the changes in the staging area.
Syntax:
git commit -m "<commit message>"
Example:
$ git commit -m "Added README file"
5. git log
Purpose: Displays a history of commits.
Syntax:
git log
Example:
$ git log
commit 78e978b127365899a488646bca8674066cc66a58 (HEAD -> master)
Author: John Doe <john.doe@example.com>
Date: Thu Aug 18 16:01:00 2023 +0200
Added README file
commit 929787da312633a567646f763a4675667b448248
Author: Jane Doe <jane.doe@example.com>
Date: Wed Aug 17 12:10:00 2023 +0200
Initial commit
6. git branch
Purpose: Lists or creates branches.
Syntax:
git branch [<branch name>]
Example:
$ git branch my-feature
7. git checkout
Purpose: Switches to a different branch or creates a new branch and switches to it.
Syntax:
git checkout <branch name>
Example:
$ git checkout my-feature
8. git merge
Purpose: Merges changes from one branch into another.
Syntax:
git merge <branch name>
Example:
$ git merge my-feature
9. git pull
Purpose: Fetches changes from a remote repository and merges them into the current branch.
Syntax:
git pull <remote> <branch>
Example:
$ git pull origin master
10. git push
Purpose: Pushes local changes to a remote repository.
Syntax:
git push <remote> <branch>
Example:
$ git push origin master
11. git remote
Purpose: Adds or modifies a remote repository.
Syntax:
git remote add <remote name> <url>
Example:
$ git remote add origin https://github.com/my-org/my-repo.git
12. git clone
Purpose: Creates a clone of a remote repository.
Syntax:
git clone <url>
Example:
$ git clone https://github.com/my-org/my-repo.git
13. git diff
Purpose: Shows the differences between two commits, branches, or files.
Syntax:
git diff <commit1> <commit2>
Example:
$ git diff HEAD~1 HEAD
14. git restore
Purpose: Restores files to their committed state.
Syntax:
git restore <file>...
Example:
$ git restore README.md
15. git reset
Purpose: Resets the current branch to a previous commit.
Syntax:
git reset [--hard | --soft] <commit>
Example:
$ git reset --hard HEAD~1
16. git stash
Purpose: Temporarily stores and untracks changes from the working directory.
Syntax:
git stash
Example:
$ git stash
17. git stash pop
Purpose: Restores the most recent stashed changes to the working directory.
Syntax:
git stash pop
Example:
$ git stash pop
18. git tag
Purpose: Creates or lists tags.
Syntax:
git tag <tag name>
Example:
$ git tag v1.0.0
19. git fetch
Purpose: Fetches remote changes without merging them into the current branch.
Syntax:
git fetch <remote>
Example:
$ git fetch origin
20. git rebase
Purpose: Integrates changes from one branch into another by re-creating commits.
Syntax:
git rebase <upstream branch>
Example:
$ git rebase origin/master
21. git cherry-pick
Purpose: Selectively applies a specific commit from one branch to another.
Syntax:
git cherry-pick <commit>
Example:
$ git cherry-pick origin/my-feature
22. git bisect
Purpose: Finds the first bad commit in a series of commits.
Syntax:
git bisect start
git bisect bad
git bisect good
Example:
$ git bisect start
$ git bisect bad HEAD~10
$ git bisect good HEAD
23. git gc
Purpose: Performs garbage collection to reclaim unused disk space.
Syntax:
git gc
Example:
$ git gc
24. git config
Purpose: Sets or gets configuration options.
Syntax:
git config <key> <value>
Example:
$ git config user.name "John Doe"
25. git help
Purpose: Displays help information for a specific command or Git in general.
Syntax:
git help <command>
Example:
$ git help commit
Conclusion
Mastering these 25 Git commands will empower you to navigate your repository effectively, collaborate seamlessly, and manage your codebase with confidence. By leveraging these commands, you can streamline your development workflow, resolve conflicts efficiently, and maintain a clean and organized Git history.
Comments
Post a Comment
Oof!