git restore .- restores the files to the previous commit/ undos all the local changes that haven't been commited.
git restore index.html- restores only that particular file to the recent commit/ undos all the local/uncommited changes for that file.
git reset --hard <hash code of the commit>- removes commits and goes back to the commit for that hash code
git reset --source <hash code> index.html>- removes commits and goes back to the commit for that hash code only for that particular file.
git commit --amend -m 'Your message'- helps re-write messages
git revert <hash code>- helps to roll back to a previous commit by creating a new commit for it. Doesn't removes those commits from the
git reflog- this can be useful to bring back deleted commits/files/changes. Use
git reset <hash code of lost commit from reflog>to bring back rolled changes.
git reset HEAD~2- Helps roll back by 2 commits and unstage all the changes in those 2 removed commits.
git reset HEAD~2 --hard
git rebase(most useful command)- Reapply commits on top of another base tip. ex.
git rebase mastersets the branch at the tip of master branch
- - Use
git checkout -b new-feature
- - Then roll back commits on master using
git reset HEAD~1 --hard: (this command will roll back 1 commit)
2. Stashing (example scenario: you need to work/switch on a new branch without making a commit on the current branch)
git stashwhen you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
The modifications stashed away by this command can be listed with
git stash list, inspected with
git stash show, and restored (potentially on top of a different commit) with
git stash apply. Calling
git stashwithout any arguments is equivalent to
git stash push.
- stashes/ saves the changes in the back of the project/ another directory of the project and the control moves back to the last working copy of the last commit.
- saves the changes as a draft and moves back to code of the last commit.
git stash push -m "Message"- Adds a message for the stash to the stash list
git stash list- lists all the draft changes in the back of the project
Tip- The stash list stores all the stashes and each stashed feature/code has a unique index number to it. The last added stash always appears at the top with index 0.
git stash apply- applies the last stashed draft to our current working directory
git stash apply <index number>- applies the particular indexed stash to our current working directory
git stash drop <index number>- drops the stash out of the stash list with the particular index
git stash pop- pops the last draft changes back into the working directory/ on the working branch and that draft is then removed from the stash list
git stash pop <index number>- pops the draft change with the particular index back into the working directory/ on the working branch and that draft is then removed from the stash list
git stash clear- clears/ deletes all the draft changes stored
3. Moving committed changes to an already existing branch using cherry-pick(scenario: you accidently worked on master when there is already a dedicated branch for the feature):
git checkout feature-branch
git cherry-pick <hash code of that commit on master>
git checkout master
git reset HEAD~1 --hard(rolls back 1 commit)
4. Squashing commits (scenario: you made a bunch of extra commits because of errors and you wan to combine commits into one with a new commit message)-
git rebase -i <hash code of the commit above which all the commits need to be squashed>
- i stands for interactive squash
- opens up squashing in vim editor where you can pick or squash and update commit messages.
5. Bisect (scenario: there is a bug introduced in the code but you don't know which commit introduced the bug)-
- Uses binary search to find the commit that introduced a bug.
git bisect start- starts the searching procedure to find the bad commit.
(Optional) Use the command
git bisect good <hash-code of the commit that you are sure doesn't have the bug>- usually this is the first or the second commit and tells git to start from the middle.
git will start checking out commits on its own, you have to test the commit and let git know if the commit has the bug or not by using commands,
git bisect good- if the commit is fine
git bisect bad- if the commit has the bug.
After giving feedbacks, git has a feedback tree that it uses to return the commit that introduced the bug/ the first bad commit, you can copy its hash code. The reference
refs/bisect/badwill be left pointing at that commit.
After a bisect session, to clean up the bisection state and return to the original HEAD, issue the following command:
git bisect reset- By default, this will return your tree to the commit that was checked out before git bisect start.
git bisect reset <commit>- For example,
git bisect reset bisect/badwill check out the first bad revision, while
git bisect reset HEADwill leave you on the current bisection commit and avoid switching commits at all.
git revert <hash-code of the bad commit>- used to revert the changes done in the bad commit that introduced the bug in the code.
6. Filter branch (scenario: You want to remove a particular file, whether a large file or any api key accidently uploaded, from you previous commits)-
git filter-branch --index-filter 'git rm --cached --ignore-unmatch filepath' HEAD
- Rewrites whichever commit has the particular file present in it while removing it from the commit.
- Starts searching from
--ignore-unmatchhelps avoiding errors if file is not found.
- Commit time and other properties remain same, only SHA/ Hash of the commit may update if the particular file is found and removed from that commit.
git archive --format=zip --output project.zip master
This command will output a zip file for master branch and will not include files mentioned in