Skip to content
Home » Forum

Forum

A Comprehensive Gui...
 
Notifications
Clear all

[Solved] A Comprehensive Guide to Git Commands

1 Posts
1 Users
0 Reactions
55 Views
Mark Sikaundi
(@emmanuelmark117)
Member Admin
Joined: 2 years ago
Posts: 83
Topic starter  

Understanding Git Basics

Before diving into commands, let's clarify some key Git concepts:

  • Repository: A directory that contains all the files for a project and their revision history.
  • Working Directory: The directory where you edit files.
  • Staging Area: A place to temporarily store changes before committing.
  • Commit: A snapshot of your project at a specific point in time.
  • Branch: An independent line of development.
  • Remote: A copy of your repository on a server (e.g., GitHub, GitLab).

Essential Git Commands

Initializing a Repository

  • git init: Creates an empty Git repository in the current directory.

Working with Files

  • git status: Shows the state of the working directory and staging area.
  • git add <file>: Adds a file to the staging area.
  • git add .: Adds all changed files to the staging area.
  • git commit -m "message": Commits the staged changes with a descriptive message.
  • git diff <file>: Shows the difference between the working directory and the staging area for a specific file.
  • git diff --staged: Shows the difference between the staging area and the last commit.

Inspecting Commit History

  • git log: Shows the commit history.
  • git log --oneline: Shows the commit history in a single line.
  • git show <commit>: Shows details of a specific commit.

Working with Branches

    • git branch: Lists all local branches.
    • git branch <branch-name>: Creates a new branch.
    • git checkout <branch-name>: Switches to a different branch.
  • git merge <branch-name>: Merges changes from one branch into another.
  • git branch -d <branch-name>: Deletes a branch.

Working with Remote Repositories

  • git remote add <alias> <url>: Adds a remote repository.
  • git fetch <remote>: Downloads new data from a remote repository.
  • git pull <remote> <branch>: Fetches and merges changes from a remote branch.
  • git push <remote> <branch>: Pushes changes to a remote branch.

Other Useful Commands

  • git reset --hard HEAD: Unstages all changes.
  • git revert <commit>: Reverses the changes of a specific commit.
  • git stash: Saves the working directory's state temporarily.
  • git stash pop: Restores the most recently stashed changes.

Example Workflow

  1. Create a new repository: git init
  2. Make changes to files: Edit files in your working directory.
  3. Stage changes: git add <file> or git add .
  4. Commit changes: git commit -m "Descriptive message"
  5. Create a new branch for a feature: git branch feature
  6. Switch to the new branch: git checkout feature
  7. Make changes and commit: Repeat steps 2-4.
  8. Merge changes back to main branch: git checkout main and git merge feature
  9. Push changes to remote repository: git push origin main

Additional Tips

  • Use descriptive commit messages.
  • Commit frequently.
  • Create branches for new features.
  • Review changes before merging.
  • Back up your repository regularly.

Remember: This is just a basic overview. Git is a powerful tool with many more features and options. For more in-depth information and advanced usage, refer to the official Git documentation or online tutorials.

Would you like to focus on a specific area or learn about more advanced Git commands?


   
Quote
Share: