Using Git
Git is a powerful version control system that helps developers track changes, collaborate on projects, and maintain a detailed history of their work. In this tutorial, we will guide you through the basics of using Git, providing you with the foundational knowledge needed to manage your projects effectively.
Setting Up Git
Before you begin, ensure Git is installed on your system. Lab machines have Git installed by default. You can download and install Git on your own device by following the instructions on the Git Downloads page. Once installed, configure your Git environment by setting your username and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@usu.edu"These details will be associated with your commits.
Initializing a Repository
To start using Git, navigate to your project directory (i.e., ece2700) and initialize a repository:
git initThis command creates a .git folder in your project, which Git uses to track changes.
Tracking Changes
To track changes in your project, add files to the staging area using:
git add <file>You can add all changes with:
git add .Once staged, commit your changes with a descriptive message:
git commit -m "Initial commit"Tagging a Commit
Tags in Git are used to mark specific points in your repository’s history, often to denote a release or a significant milestone. To create a tag, use the following command:
git tag -a <tag-name> -m "Tag message"Replace <tag-name> with a meaningful name for the tag (e.g., v1.0) and provide a message describing the tag.
To view all tags in your repository, use:
git tagIf you want to push tags to a remote repository, run:
git push origin <tag-name>To push all tags at once, use:
git push origin --tagsTags are a helpful way to organize and reference important commits in your project.
Working with Branches
Branches allow you to work on different features or fixes independently. Create a new branch with:
git branch <branch-name>Switch to the new branch:
git checkout <branch-name>Merge changes back into the main branch when ready:
git checkout main
git merge <branch-name>Working with GitHub
GitHub is a popular platform for hosting Git repositories. To push your local repository to GitHub, create a repository on GitHub and link it to your local project:
git remote add origin https://github.com/your-username/your-repo.git
git push -u origin mainYou can now collaborate with others by sharing your repository and using pull requests.
Additional Resources
For more detailed information, refer to the Git Documentation and GitHub Docs. These resources provide comprehensive guides and examples to help you deepen your understanding of Git.
By following this tutorial, you’ll gain the skills needed to manage your projects efficiently and collaborate effectively in team environments.