Welcome to the Git & Github tutorial! In this guide, we'll cover the basics of using Git and Github for version control and collaborative development. Git is a distributed version control system that helps developers track changes to their code over time, while Github is a web-based hosting service for Git repositories, enabling collaboration and sharing of code.
- Getting Started
- Staging and Committing Changes
- Using Git with VS Code
- Working with Remote Repositories
- Pushing Changes to a Remote Repository
- Merging Branches
- Pulling Changes from Remote
- Cloning a Repository
- Working with Branches
- Handling Merge Conflicts
- Forking a Repository
- Making Pull Requests
- Git Reset
- Git Revert
- Git Amend
- Git Stash
- Git Rebase
- Git Squash
Let's begin by initializing a Git repository in your project folder:
git init
Once you've made some changes to your project, you can stage them for commit using the following command:
git add .
The above command stages all the changes in your project for the next commit. However, if you want to unstage some changes, you can use:
git reset .
You can check the status of your repository and see which files are staged or unstaged using:
git status
Once you have staged the changes, you can commit them with a meaningful message:
git commit -m "Your commit message here"
You can view the commit history using:
git log
For a more condensed and graphical representation, you can use:
git log --graph --oneline --decorate
If you're using Visual Studio Code as your code editor, you can take advantage of Git integration using extensions like "Git Lens" and "Git Source Control."
To connect your local repository to a remote repository (like one hosted on Github), use the following command:
git remote add origin <git url>
To push your changes to the remote repository (specifically to the "master" branch), use:
git push origin master -u
The -u flag sets the "origin" as the default upstream remote, simplifying future pushes.
You can merge changes from one branch into another using various merge strategies. For example, to perform a Fast-Forward merge (when there are no conflicting changes):
git fetch
git merge origin/master
To pull changes from the remote repository (fetch + merge):
git pull
Remember to stash or commit local changes before pulling, as it may cause merge conflicts.
To clone an existing repository from a remote source, use:
git clone <repository url>
You can list all branches in your repository using:
git branch
To create a new branch and switch to it:
git checkout -b new-branch
To delete a branch:
git branch -d branch-name
Use -D instead of -d if you want to forcefully delete a branch.
Merge conflicts occur when Git can't automatically merge changes. You'll need to manually resolve conflicts and then commit the changes.
Forking allows you to create your own copy of a repository on your Github account, enabling you to make changes without affecting the original repository.
To contribute to a project, you can follow these steps:
Fork the repository on Github. Clone the forked repository to your local machine. Create a new branch for your feature/fix. Commit and push your changes to your fork. Create a pull request on Github to propose your changes to the original repository.
when working with fork locally we can keep it in sync with the original
git remote add upstream < git url>
git fetch upstream
git rebase upstream/master
Git reset allows you to unstage changes from the staging area.
git reset
You can also reset to a specific commit by using the commit ID:
git reset <commit-id>
Git revert is used to undo a previous commit by creating a new commit with the opposite changes.
Git amend is used to modify the last commit.
git commit --amend "Your updated commit message"
git commit --amend --no-edit # (forgot to add some files to staging before the commit )
Git stash allows you to temporarily store changes that are not ready to be committed.
git stash save "Your stash message"
To apply the changes back:
git stash apply
Git rebase is used to modify the commit history by moving, combining, or deleting commits.
Git squash is used to combine multiple commits into one.
git rebase -i master
Replace "pick" with "squash" for the commits you want to squash.
Feel free to use this tutorial as a starting point and add more details, examples, and styling as needed. Happy coding with Git and Github!