Certainly! Here’s a detailed explanation of each Git operation and command:
1. Basic Git Operations
Initialization and Configuration
Initialize a New Repository:
git init
- Explanation: Initializes a new Git repository in the current directory, creating a hidden
.git
directory where Git stores configuration and metadata.
- Explanation: Initializes a new Git repository in the current directory, creating a hidden
Clone an Existing Repository:
git clone <repository-url>
- Explanation: Copies an existing Git repository from a remote URL to your local machine, including all its history and branches.
Configure Git:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
- Explanation: Sets up global user information that will be used in commits. The
--global
flag makes these settings apply to all repositories on your machine.
- Explanation: Sets up global user information that will be used in commits. The
Basic Commands
Check Repository Status:
git status
- Explanation: Shows the state of the working directory and the staging area, including which files are modified, staged for commit, or untracked.
View Commit History:
git log
- Explanation: Displays a list of commits in the current branch, including commit messages, author information, and commit hashes.
View a Specific Commit:
git show <commit-hash>
- Explanation: Shows detailed information about a specific commit, including changes made and the commit message.
List Files in the Repository:
git ls-tree --name-only <branch-name>
- Explanation: Lists files in a specific branch. Useful for seeing the contents of a branch without switching to it.
Staging and Committing
Add Files to Staging:
git add <file> git add . # Add all changes in the current directory
- Explanation: Moves changes in specified files (or all files) from the working directory to the staging area, preparing them to be committed.
Remove Files from Staging:
git reset <file>
- Explanation: Unstages files that have been added to the staging area, keeping the changes in the working directory.
Commit Changes:
git commit -m "Commit message"
- Explanation: Saves the changes from the staging area to the repository with a descriptive message.
Amend Last Commit:
git commit --amend
- Explanation: Modifies the most recent commit. You can change the commit message or add new changes to the commit.
Branching and Merging
Create a New Branch:
git branch <branch-name>
- Explanation: Creates a new branch with the specified name. This branch starts from the current commit.
List All Branches:
git branch
- Explanation: Lists all branches in the repository. The current branch is marked with an asterisk.
Switch Branches:
git checkout <branch-name>
- Explanation: Switches to the specified branch, updating the working directory to match the branch’s state.
Create and Switch to a New Branch:
git checkout -b <branch-name>
- Explanation: Creates a new branch and immediately switches to it.
Merge Branches:
git merge <branch-name>
- Explanation: Combines the changes from the specified branch into the current branch. Conflicts may need to be resolved manually.
Delete a Branch:
git branch -d <branch-name> git branch -D <branch-name> # Force delete
- Explanation: Deletes the specified branch. The
-D
flag forcefully deletes the branch even if it has unmerged changes.
- Explanation: Deletes the specified branch. The
Rebase Branch:
git rebase <branch-name>
- Explanation: Re-applies commits from the current branch onto another branch, changing the base of the current branch.
Remote Repositories
Add a Remote Repository:
git remote add origin <repository-url>
- Explanation: Adds a new remote repository with the given URL, typically named
origin
. This allows you to push and pull changes from the remote.
- Explanation: Adds a new remote repository with the given URL, typically named
List Remote Repositories:
git remote -v
- Explanation: Lists the remote repositories associated with your local repository and their URLs.
Remove a Remote Repository:
git remote remove <remote-name>
- Explanation: Removes a remote repository from your local configuration.
Fetch from Remote Repository:
git fetch <remote-name>
- Explanation: Downloads changes from a remote repository without merging them into the local branch.
Pull Changes from Remote Repository:
git pull <remote-name> <branch-name>
- Explanation: Fetches changes from a remote branch and merges them into the current branch.
Push Changes to Remote Repository:
git push <remote-name> <branch-name>
- Explanation: Uploads your local branch commits to the specified remote repository.
Push All Branches to Remote:
git push --all <remote-name>
- Explanation: Pushes all branches to the specified remote repository.
Push Tags to Remote:
git push <remote-name> --tags
- Explanation: Pushes all tags to the specified remote repository.
Tagging
Create a Tag:
git tag <tag-name>
- Explanation: Creates a new tag pointing to the current commit. Tags are often used for marking releases.
List Tags:
git tag
- Explanation: Lists all tags in the repository.
Push a Tag to Remote:
git push <remote-name> <tag-name>
- Explanation: Pushes a specific tag to the remote repository.
Delete a Tag:
git tag -d <tag-name>
- Explanation: Deletes a tag from your local repository.
Undoing Changes
Discard Changes in Working Directory:
git checkout -- <file>
- Explanation: Discards changes in a file in the working directory, reverting it to the last committed state.
Undo Last Commit (Keep Changes):
git reset --soft HEAD~1
- Explanation: Moves the HEAD pointer back one commit, but keeps the changes in the working directory and staging area.
Undo Last Commit (Discard Changes):
git reset --hard HEAD~1
- Explanation: Moves the HEAD pointer back one commit and discards all changes in the working directory and staging area.
Revert a Commit:
git revert <commit-hash>
- Explanation: Creates a new commit that undoes the changes introduced by a specific commit. This is a safe way to undo changes in a public history.
Stashing
Save Changes to Stash:
git stash
- Explanation: Temporarily saves changes in your working directory that are not yet committed. Useful for switching branches without committing changes.
Apply Stashed Changes:
git stash apply
- Explanation: Reapplies stashed changes to your working directory. The stash remains in the stash list.
List Stashes:
git stash list
- Explanation: Shows a list of stashes that you have saved.
Drop a Stash:
git stash drop <stash@{index}>
- Explanation: Deletes a specific stash from the stash list.
Clear All Stashes:
git stash clear
- Explanation: Deletes all stashes from the stash list.
Diffs
Show Changes in Working Directory:
git diff
- Explanation: Displays changes between the working directory and the index (staging area).
Show Changes Between Commits:
git diff <commit-hash1> <commit-hash2>
- Explanation: Shows differences between two specific commits.
Show Changes Between Staging and Last Commit:
git diff --cached
- Explanation: Displays changes between the staging area and the last commit.
Configuration and Settings
View Git Configuration:
git config --list
- Explanation: Lists all Git configuration settings, including user info and repository settings.
Set Git Configuration:
git config <key> <value>
- Explanation: Sets a Git configuration setting. You can set user-specific, repository-specific, or global settings.
4. Advanced Git Operations
Cherry-Picking
- Apply a Commit from Another Branch:
git cherry-pick <commit-hash>
- Explanation: Applies the changes introduced by a specific commit from another branch to the current branch. Useful for backporting fixes.
Reflog
View the Reflog:
git reflog
- Explanation: Shows a log of all reference changes, including commits, checkouts, and merges. Useful for recovering lost commits or understanding recent changes.
Recover Lost Commits:
git checkout <commit-hash>
- Explanation: Allows you to check out a commit directly, which can be useful for recovering lost changes.
Submodules
Add a Submodule:
git submodule add <repository-url> <path>
- Explanation: Adds a new Git repository as a submodule within your repository. This is useful for including external projects.
Update Submodules:
git submodule update --remote
- Explanation: Updates submodules to the latest commit on their respective remote branches.
Initialize Submodules:
git submodule init
- Explanation: Initializes submodules in a repository after cloning. Required for setting up submodules.
This detailed explanation covers the most common and important Git commands and concepts. If you need further details on any specific command or operation, feel free to ask!
No comments:
Post a Comment