Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control

Here’s a detailed summary of common Git commands, compiled with hands-on examples and descriptions to help you understand and use Git effectively in your development workflow.

Git: Essential Commands and Usage Guide

Git is a powerful ver…


This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE

Here's a detailed summary of common Git commands, compiled with hands-on examples and descriptions to help you understand and use Git effectively in your development workflow.

Git: Essential Commands and Usage Guide

Git is a powerful version control system used by developers to manage and track changes in their codebase. Below is a compilation of common Git commands along with practical examples and step-by-step descriptions.

1. Initializing a Repository

Command:

git init

Description:
Initializes a new Git repository in the current directory. This creates a .git directory where Git stores all its tracking data.

Example:

mkdir my-project
cd my-project
git init

Explanation:

  1. Create a new directory for your project.
  2. Navigate into the directory.
  3. Initialize a Git repository in this directory.

2. Cloning a Repository

Command:

git clone <repository_url>

Description:
Creates a copy of an existing Git repository. This command clones the repository from a remote server to your local machine.

Example:

git clone https://github.com/user/repo.git

Explanation:

  1. Provide the URL of the remote repository.
  2. Git copies all the files and commits from the remote repository to your local machine.

3. Checking Repository Status

Command:

git status

Description:
Displays the state of the working directory and staging area. It shows changes that have been staged, changes that are not staged, and files that are not being tracked.

Example:

git status

Explanation:

  1. Run the command to see which files have been modified, added, or deleted.

4. Adding Changes to Staging Area

Command:

git add <file>

Description:
Adds changes in the specified file to the staging area. This prepares the file for the next commit.

Example:

git add index.php

Explanation:

  1. Use this command to stage changes in index.php for committing.

To add all changes:

git add .

5. Committing Changes

Command:

git commit -m "Commit message"

Description:
Records the changes made to the files in the repository along with a commit message describing the changes.

Example:

git commit -m "Add user login functionality"

Explanation:

  1. Stage the changes first.
  2. Commit the staged changes with a descriptive message.

6. Viewing Commit History

Command:

git log

Description:
Shows the commit history of the repository, including commit IDs, author names, dates, and commit messages.

Example:

git log

Explanation:

  1. Run this command to review the history of commits made to the repository.

7. Creating a New Branch

Command:

git branch <branch_name>

Description:
Creates a new branch in the repository. Branches allow you to work on different features or bug fixes simultaneously.

Example:

git branch feature-branch

Explanation:

  1. Create a new branch called feature-branch.

8. Switching Branches

Command:

git checkout <branch_name>

Description:
Switches to the specified branch. This command updates your working directory to reflect the state of the branch you switch to.

Example:

git checkout feature-branch

Explanation:

  1. Switch to the feature-branch to work on it.

Note: In Git versions 2.23 and later, you can use:

git switch <branch_name>

9. Merging Branches

Command:

git merge <branch_name>

Description:
Merges the specified branch into the current branch. This integrates changes from one branch into another.

Example:

git checkout main
git merge feature-branch

Explanation:

  1. Switch to the branch you want to merge changes into (e.g., main).
  2. Merge the changes from feature-branch into main.

10. Pushing Changes to Remote Repository

Command:

git push origin <branch_name>

Description:
Uploads the local branch and its commits to the remote repository.

Example:

git push origin feature-branch

Explanation:

  1. Push the feature-branch to the remote repository.

11. Pulling Changes from Remote Repository

Command:

git pull

Description:
Fetches and integrates changes from the remote repository into your current branch.

Example:

git pull

Explanation:

  1. Update your local branch with changes from the remote repository.

12. Removing Files from Staging Area

Command:

git reset <file>

Description:
Unstages the specified file, but keeps the changes in the working directory.

Example:

git reset index.php

Explanation:

  1. Unstage index.php if it was added to the staging area but you don't want to commit it yet.

13. Deleting a Branch

Command:

git branch -d <branch_name>

Description:
Deletes the specified branch from the local repository. Use -D to force delete.

Example:

git branch -d feature-branch

Explanation:

  1. Delete feature-branch after it has been merged or is no longer needed.

14. Stashing Changes

Command:

git stash

Description:
Temporarily saves changes in a stash so you can work on something else. Apply the stash later with git stash apply.

Example:

git stash

Explanation:

  1. Save your changes temporarily if you need to switch tasks.

15. Viewing Stash List

Command:

git stash list

Description:
Displays a list of all stashed changes.

Example:

git stash list

Explanation:

  1. Check the list of stashed changes to decide which to apply or drop.

16. Applying Stashed Changes

Command:

git stash apply

Description:
Applies the most recent stash to your working directory.

Example:

git stash apply

Explanation:

  1. Retrieve your stashed changes and apply them back to your working directory.

17. Removing a Stash

Command:

git stash drop

Description:
Removes a specific stash from the list of stashed changes.

Example:

git stash drop stash@{0}

Explanation:

  1. Remove the specified stash from the stash list.

18. Reverting a Commit

Command:

git revert <commit_id>

Description:
Creates a new commit that undoes the changes from a specified commit.

Example:

git revert a1b2c3d

Explanation:

  1. Create a new commit that undoes the changes introduced by a1b2c3d.

Conclusion

Understanding and using these common Git commands will help you manage your codebase effectively and collaborate efficiently with others. This guide provides a solid foundation for working with Git in your development projects. Feel free to adapt and expand these commands based on your specific workflow and requirements.


This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE


Print Share Comment Cite Upload Translate Updates
APA

MD ARIFUL HAQUE | Sciencx (2024-09-13T20:29:58+00:00) Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control. Retrieved from https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/

MLA
" » Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control." MD ARIFUL HAQUE | Sciencx - Friday September 13, 2024, https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/
HARVARD
MD ARIFUL HAQUE | Sciencx Friday September 13, 2024 » Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control., viewed ,<https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/>
VANCOUVER
MD ARIFUL HAQUE | Sciencx - » Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/
CHICAGO
" » Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control." MD ARIFUL HAQUE | Sciencx - Accessed . https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/
IEEE
" » Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control." MD ARIFUL HAQUE | Sciencx [Online]. Available: https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/. [Accessed: ]
rf:citation
» Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control | MD ARIFUL HAQUE | Sciencx | https://www.scien.cx/2024/09/13/mastering-git-a-curated-list-of-must-know-commands-for-efficient-version-control/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.