Git is a version control system that allows you to track changes in your files and collaborate on projects.
# Set your username and email globally
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
# Check your configuration
git config --list
1. Initialise or Clone a repository
Use this to initialise an existing directory as a Git repository.
git init
Use this to create a local copy of a remote repository.
git clone https://github.com/username/repository.git
2. Create a new branch
Always create a new branch to work on features or bug fixes.
git checkout -b new-branch-name
3. Stage changes
Add modified files to the staging area.
git add file-name
To add all files:
git add .
4. Commit changes
Commit the staged changes with a message.
git commit -m "Commit message"
5. Push changes to GitHub
Push your changes to the remote repository on GitHub.
git push origin branch-name
6. Merge changes
Merge your changes into a specified branch, typically main.
git merge branch-name
GitHub is a hosting platform for Git repositories.
-
Forking: Make a copy of a repository on GitHub under your account.
-
Pull Request (PR): A request to merge changes from your branch into another branch (typically the main branch).
-
Issues: A way to track tasks, bugs, or enhancements in a project.
To contribute to someone else's project, fork it to your account and then clone it.
# Fork the repo from GitHub then clone your fork
git clone https://github.com/your-username/repository.git
After pushing your changes, open a pull request to the original repository for review and merging.
Check Git Status
git status
View Commit History
git log
Show Changes between Commits
git diff
Remove a file from Git
git rm file-name
View Branches
git branch
Switch Branches
git checkout branch-name
Merge Branches
git merge branch-name
Delete a Branch
git branch -d branch-name
Create a New Branch
git checkout -b new-branch-name
Add a Remote
git remote add origin https://github.com/username/repository.git
View Remotes
git remote -v
Fetch Latest Changes from Remote
git fetch origin
Pull Latest Changes from Remote
# Pulls changes from the 'main' branch
git pull origin main
Push to Remote
git push origin branch-name