Module 4
4.5: Git & Github
Module 4
4.5: Git & Github
Sometimes, you might be working on something but need to switch to a different branch before finishing your current task. You don't want to commit your work just yet, but you also don't want to lose it. This is where git stash comes in. It "freezes" your work so you can safely switch to another branch without worrying about your changes.
How to use git stash:
Open Git Bash or your terminal
Navigate to your repository.
Run the following command to stash your changes:
git stash
Explanation:
git stash: Saves your current changes (both staged and unstaged) to a stash and restores your working directory to the state of the last commit.
Now, you can safely switch to another branch or perform other tasks without worrying about losing your changes.
Example:
git stash
After running this command, your working directory is clean, and you can now switch branches:
git checkout another-branch
When you're ready to continue working on the changes you stashed earlier, you can "unfreeze" them using git stash pop. This restores your changes and removes them from the stash.
How to use git stash pop:
Open Git Bash.
Navigate to your repository.
Run the following command to restore your changes:
git stash pop
Explanation:
git stash pop: Restores the most recent stashed changes back into your working directory and removes it from the stash list.
Example:
git stash pop
Your changes will reappear in your working directory, and you can continue where you left off.
Sometimes, you may need to undo a specific commit and return your project to the state it was in before that commit. You can do this using git revert. It creates a new commit that undoes the changes of a previous commit.
How to use git revert <commit-id>:
First, identify the commit ID (hash) of the commit you want to revert to. You can find this by running:
git log
Once you have the commit ID, run the following command to revert to it:
git revert <commit-id>
Explanation:
git revert: This command creates a new commit that undoes the changes made in a specific commit.
<commit-id>: Replace this with the ID of the commit you want to revert.
Example:
git revert abc123def456
This will create a new commit that reverts the changes introduced by commit abc123def456.
If you want to compare the differences between two specific commits, you can use the git diff command. This shows you what changed between the two commits.
How to use git diff <commit-id-1> <commit-id-2>:
Run the following command:
git diff <commit-id-1> <commit-id-2>
Explanation:
git diff: This shows the differences between two commits.
<commit-id-1> and <commit-id-2>: Replace these with the IDs of the commits you want to compare.
Example:
git diff abc123def456 def789gh012
This will show the differences between commit abc123def456 and commit def789gh012.
If you've made some changes but haven’t committed them yet, and you want to see the differences between your current changes and the last commit, you can use the git diff HEAD . command.
How to use git diff HEAD .:
Run the following command:
git diff HEAD .
Explanation:
git diff HEAD: This compares the current state of your working directory (uncommitted changes) with the most recent commit.
.: Refers to the current directory (all files and changes within it).
Example:
git diff HEAD .
This will show the differences between your current changes and the last commit. It’s useful to see what modifications have been made but not yet committed.