Git Usage Tutorial
Git is a version control system that allows multiple users to manage and track changes in code. Itβs essential for collaboration in software projects and helps maintain a history of edits and versions.
Table of Contentsβ
- Getting Started with Git
- Basic Git Commands
- Branching and Merging
- Working with Remote Repositories
- Advanced Git Commands
1. Getting Started with Gitβ
Installationβ
To get started with Git, youβll need to install it:
- Linux:
sudo apt-get install git - macOS:
brew install git - Windows: Download Git
Verify the installation by checking the version:
git --version
Configuring Gitβ
Set up your username and email. This information is attached to your commits.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
To confirm your configuration settings:
git config --list
2. Basic Git Commandsβ
Initializing a Repositoryβ
To create a new Git repository in your project directory:
git init
Cloning a Repositoryβ
To clone an existing Git repository from a remote location:
git clone https://github.com/username/repository.git
Staging and Committing Changesβ
-
Stage Files: Select files for your next commit.
git add filename # Adds specific file
git add . # Adds all changes -
Commit Changes: Commit the staged files to the repository with a message.
git commit -m "Commit message describing changes"
Checking the Statusβ
Check the current status of your repository:
git status
Viewing Commit Historyβ
To view the commit history of the repository:
git log
3. Branching and Mergingβ
Creating and Switching Branchesβ
Branches allow for separate feature development without affecting the main code.
-
Create a new branch:
git branch branch-name -
Switch to a branch:
git checkout branch-name
Merging Branchesβ
To merge changes from one branch into the current branch:
git merge branch-name
Deleting Branchesβ
After merging, you may want to delete old branches:
-
Locally:
git branch -d branch-name -
Remotely:
git push origin --delete branch-name
4. Working with Remote Repositoriesβ
Adding a Remoteβ
To link your local repository to a remote one:
git remote add origin https://github.com/username/repository.git
Pushing Changesβ
To upload commits to the remote repository:
git push origin branch-name
Pulling Changesβ
To download updates from the remote repository:
git pull origin branch-name
Fetching Changesβ
Retrieve changes from the remote without merging them automatically:
git fetch origin
5. Advanced Git Commandsβ
Resetting Commitsβ
To undo commits:
-
Soft Reset: Keeps changes staged
git reset --soft HEAD~1 -
Hard Reset: Deletes all changes
git reset --hard HEAD~1
Stashing Changesβ
To save uncommitted changes temporarily:
git stash
To retrieve stashed changes:
git stash apply
Reverting a Commitβ
Undo changes by creating a new commit that reverses a specific commit:
git revert commit-hash
Conclusionβ
This guide provides an overview of Git's fundamental commands for working with repositories. For more detailed information, refer to the official Git documentation. Happy coding!
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.