Objective: By the end of this lesson, you will be proficient in using Git commands and workflows, tailored for frontend development, to track changes, collaborate with others, and maintain a robust workflow.
Understanding Git’s workflow is crucial. It involves creating a repository, making changes, staging these changes, and then committing them to your project’s history. Collaborating with others involves pulling changes from a remote repository and pushing your changes to it.
Initializing a Git Repository: Start by creating a new repository. In your project folder, run:
git init
This command creates a new Git repository locally.
Tracking Changes: After making changes in your code, use:
git status
This command shows the status of changes as untracked, modified, or staged.
Staging Changes: To include changes in your next commit, use:
git add [file-name]
Or, to add all changes, use:
git add .
Committing Changes: To save your staged changes along with a descriptive message, use:
git commit -m "Add a descriptive message"
Pushing Changes: To upload your local commits to a remote repository, use:
git push origin master
This pushes the changes to the master branch of the remote repository named ‘origin’.
Pulling Changes: To update your local repository with changes from the remote repository, use:
git pull origin master
Working with branches allows you to develop new features or fix bugs without affecting the main codebase.
Creating a New Branch: Use:
git branch [branch-name]
Switching Branches: To switch to another branch:
git checkout [branch-name]
Merging Changes: To merge changes from one branch to another, first switch to the receiving branch, then use:
git merge [branch-name]
Conflicts can occur during a merge. Git will mark the files that have conflicts. You need to manually resolve these conflicts by editing the files, then stage and commit the resolved files.
Remote repositories are essential for collaboration.
Cloning a Remote Repository: You can clone a remote repository using:
git clone [remote-repo-URL]
Connecting a Local Repository to a Remote: To connect your local repository to a remote one, use:
git remote add origin [remote-repo-URL]