Git Tips for Testers | Bondar Academy
Course: Git and GitHub Fundamentals
Module: Working with Git and GitHub
Instructor: Artem Bondar
Lesson Summary
This final lesson provides several useful Git tips to enhance your workflow. Below are key scenarios and commands discussed: Scenario 1: Committing to Master Branch Imagine you accidentally commit changes to the master branch instead of your own branch. To revert this: Use the command: git reset origin master to synchronize your local branch with the origin master branch. This will unstaged your changes. Create a new branch with: git checkout -b my-new-branch . Now, you can safely commit your changes on the new branch. Scenario 2: Switching Branches with Uncommitted Changes If you need to switch branches but have uncommitted changes, you cannot do so directly. Instead: Stash your changes using: git stash save "some work in progress" . Switch to the desired branch with: git checkout first-code . To recover your stashed changes, use: git stash list to view stashes, then git stash apply to apply the changes. Deleting Unused Branches After merging branches, you may want to delete local branches: To delete a local branch, use: git branch -d first-code . To clean up remote branches that no longer exist, execute: git fetch --prune . These tips help streamline your Git usage, allowing for better management of branches and changes.
Video Transcript
All right, this quick course is coming to the end. And in this final lesson, I want to share with you a few more useful Git tips that is useful to know, all right, let's jump into it. So imagine situation number one, you open test project, you clone latest master branch, and you started working, forgetting switching the branch from master to your own branch. And you start contribution, and for example, this is a next line of code. So you created some code, you created a commit, my commit, submitted the commit. And then you realize that you forgot to create your own branch, and you accidentally submitted the commit to the master branch. And of course, you will not be allowed to push to the master branch. For example, git push, and git will reject pushing the master branch, because master branch is usually protected to pushing into this directly. So how to revert the change? One way of doing this is to reset git head, how we learned in the previous lesson. But more easiest approach is to reset to origin master branch, executing command, git reset, and provide origin master. So with this command, you synchronize your local branch with origin master branch, and all changes that you have done will be unstaged. So right now, at this point, you can create a new branch. Create new branch, my new branch. And now on new branch, you can create this commit, my commit. So that's it, so simple how you can switch to a new branch, if you accidentally perform commit into the master branch. So the example of situation number two. Let's say you are working on some code, for example, deleted something, and then you added something. And then you decide to see to other branch, for example, to see what your peers are working on, or some other code that is located on the other branch. But you cannot simply check out the branch while your changes are not safe. For example, we want to switch to first code branch. If I type git checkout first code and hit Enter, okay, git checkout. And we have the error that local changes will be overwritten by checkout. So we need to commit those changes or stash those changes. So if you are working at the phase that you are not ready to create a commit yet, the alternative is to create a stash. So you can do something like this, git stash save and provide the save message, some work in progress, for example, and hit Enter. And now the changes that you have performed, they are not committed, but they are kind of saved on the git branch. So you can revert back to those changes when you will need it. Now you can check out first code branch, and you can successfully switch to this branch, okay? So switching back to our branch that we were working on. And if you want to recover the changes that you saved in your stash, first we need to write command git stash list to view all the changes that you have. So right now we have two changes, one on the master branch and one on our branch. We want to recover this one, and we need this number. And I type git stash apply and provide the stash number that changes we were working on on this branch. Hit Enter, and here we go. The changes are reviewed back on this branch, and they are on stage. And we can continue working on our code on this branch. And the last final example of useful tips. So we discard those changes. So when we push the changes to remote repository and merge them as a result of the pull request, after that we delete this branch on the GitHub. But those branches, we still have the list of the branches in Visual Studio Code. So we have a local branches, and we have a reference to remote branches. So how to delete branches that we don't need anymore? So there are two ways. The way number one, in order to delete the local branch, for example, we have first code branch that we already merged, and we don't need it anymore. So you type command git branch-D, capital D, and type name of the branch, first-code, and hit Enter. And that's it. If I click, first code branch disappeared from this list. It does not exist anymore. And also we have a list of the origin branches. All those branches were already merged with the pull request. But Visual Studio Code still remember the reference to all those remote branches. And we can simply clean the list of the branches that don't exist anymore. We can type the command, git fetch dash dash prune, and hit Enter. And all branches that are not available on the GitHub server will be deleted from the local computer as well. All right, that's it, guys. It was a useful tips how to work with the git, how to reset to origin master branch, how to stash your changes in order to switch to other working branch, and how to delete local branches. All right, that's it, guys, and see you in the next lesson.