Using Git involves a series of commands that help you manage your code and track changes over time. Here’s a concise guide to get you started with the basics of Git:
1. Install Git
Before you can use Git, ensure it’s installed on your system. You can check by running:
git --version
If it’s not installed, follow the installation instructions for your operating system from the official Git website.
2. Set Up Your Identity
Configure your Git username and email, which will be associated with your commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
3. Create a New Repository
To start a new project, create a directory and initialize a Git repository:
mkdir my-project
cd my-project
git init
4. Add Files
Create or add files to your project. To track these files, use the git add command:
git add filename.txt
To add all files in the directory, use:
git add .
5. Commit Changes
Once you’ve added files, commit your changes with a message describing what you did:
git commit -m "Initial commit"
6. Check Status
To see the status of your repository, including staged and unstaged changes, use:
git status
7. View Commit History
To view the history of your commits, use:
git log
8. Branching
To create a new branch for features or experiments, use:
git branch new-feature
git checkout new-feature
Or, you can create and switch to a new branch in one command:
git checkout -b new-feature
9. Merging Changes
Once you’re done with your feature, switch back to the main branch and merge your changes:
git checkout master
git merge new-feature
10. Push to Remote Repository
If you’re using a remote repository (like GitHub), you can push your changes:
git remote add origin https://github.com/username/repo.git
git push -u origin master
Further Learning
To enhance your Git skills, consider exploring these resources:
- Git Documentation: The official Git documentation provides in-depth information.
- Interactive Git Tutorials: Websites like Learn Git Branching offer interactive tutorials.
Feel free to ask if you have any specific questions or need further clarification on any Git commands!
