Git is a version control system that helps you track changes in code or files over time. Here’s a brief overview:
Key Concepts:
- Repository (Repo): Central location for your project’s files and history.
- Commits: Snapshots of changes made to your files.
- Branches: Independent lines of development (e.g., feature, fix, or experimental branches).
- Remote: A shared repository on a server (e.g., GitHub, GitLab).
Git helps you:
- Track changes and collaborate with others.
- Manage different versions of your code.
- Experiment with new features without affecting the main codebase.
Basic Commands
git init– Initialize a new Git repository.git clone <repo-url>– Clone a repository from a URL.git add <file>– Stage a file for the next commit.git add .– Stage all changes in the current directory.git commit -m "<message>"– Commit changes with a meaningful message.git log– Display a log of all commits.git status– Show the status of the repository.
Branching
git branch <branch-name>: Create a new branch.git checkout <branch-name>: Switch to a different branch.git checkout -b <branch-name>: Create and switch to a new branch.git branch -d <branch-name>: Delete a branch.git branch -D <branch-name>: Force delete a branch.
Remote Commands
git remote add <name> <url>: Add a remote repository.git fetch <remote-name>: Fetch changes from a remote repository.git push <remote-name> <branch-name>: Push changes to a remote repository.git pull <remote-name> <branch-name>: Fetch and merge changes from a remote repository.git remote -v: Display remote repository URLs.git remote set-url origin <new-url>: Update the remote Git repository URL
Undo and Reset
git reset <file>: Unstage a file.git reset --hard: Discard all changes and reset to the last commit.git revert <commit-hash>: Revert a specific commit.git checkout -- <file>: Discard changes in a file.
Miscellaneous
git diff– Show changes between the working directory and the index.git diff --cached– Show changes between the index and the last commit.git tag <tag-name>– Create a new tag.