GitHub: Essential Git Commands Every Developer Should Master

Picture of 724 Admin

724 Admin

Understanding Git commands is crucial for developers, as it allows them to efficiently manage and track changes in their codebase. Here’s a concise guide to the top 12 Git commands essential for every developer:

1. Git Config

Command:

git config --global user.name "username"
git config --global user.email "[email protected]"

Purpose:
Sets up user information for all repositories, ensuring commits are attributed to the correct developer.

2. Git Init

Command:

git init

Purpose:
Initializes a new Git repository in the current directory, enabling version control.

3. Git Status

Command:

git status

Purpose:
Displays the state of the working directory and staging area, showing changes that are staged, unstaged, and untracked.

4. Git Add

Command:

git add <file>
git add .

Purpose:
Stages changes for the next commit. git add . stages all changes in the working directory.

5. Git Commit

Command:

git commit -m "commit message"

Purpose:
Records the staged changes in the local repository with a meaningful commit message.

6. Git Clone

Command:

git clone <url>

Purpose:
Creates a local copy of a remote repository, enabling collaboration on the same codebase.

7. Git Checkout

Command:

git checkout -b <branch-name>

Purpose:
Creates and switches to a new branch, useful for feature development or bug fixes.

8. Git Branch

Command:

git branch

Purpose:
Lists all branches in the repository. It can also be used to create or delete branches.

9. Git Switch

Command:

git switch <branch-name>

Purpose:
Switches between branches, allowing movement between different lines of development.

10. Git Push

Command:

git push origin <branch-name>

Purpose:
Uploads local commits to a remote repository, updating the remote branch with local changes.

11. Git Pull

Command:

git pull

Purpose:
Fetches and integrates changes from the remote repository into the current branch, ensuring the local branch is up-to-date.

12. Git Show

Command:

git show <commit>

Purpose:
Displays detailed information about a specific commit, including changes made, commit message, and metadata.


Practical Example

Scenario:
You’ve just created a new project and want to set up a Git repository, create a feature branch, and push changes to a remote repository.

  1. Initialize the Repository:
   mkdir project1
   cd project1
   git init
  1. Configure User Information:
   git config --global user.name "yourname"
   git config --global user.email "[email protected]"
  1. Create a File and Check Status:
   touch hello.md
   git status
  1. Stage and Commit the File:
   git add hello.md
   git commit -m "Initial commit"
  1. Create and Switch to a New Branch:
   git checkout -b feature-branch
  1. Make Changes, Add, and Commit:
   echo "Hello, World!" > hello.md
   git add hello.md
   git commit -m "Add greeting to hello.md"
  1. Push Changes to Remote Repository:
   git remote add origin <remote-repository-URL>
   git push origin feature-branch
  1. Pull Latest Changes (if any):
   git pull origin main
  1. View Commit Details:
   git show

Mastering these Git commands can significantly enhance your efficiency in managing code changes, collaborating with others, and maintaining the integrity of your project’s history.

Leave a comment

Leave a Reply

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