Yes!
To discard all local changes in a file and revert it to the last committed state, use:
git restore file.txtThis command is the modern equivalent of using git checkout -- file.txt.
To restore a deleted file from the last commit, use:
git restore --source=HEAD~1 file.txtThis command restores file.txt from the commit before the last one. You can replace HEAD~1 with any commit reference.
To discard specific chunks or lines in a file, use:
git restore -p file.txtThis command starts an interactive session where you can choose which changes to keep or discard.
To discard all local changes in your working directory, use:
git restore .This command reverts all changes in the working directory to the last committed state.
To discard all local changes and commits and revert to the last commit on the current branch, use:
git reset --hard HEADThis command resets the current branch to the last commit, discarding all changes and commits.
To discard the last commit and keep the changes in your working directory, use:
git reset --soft HEAD~1This command resets the current branch to the commit before the last one, keeping the changes in your working directory.
To discard the last commit and all changes in your working directory, use:
git reset --hard HEAD~1This command resets the current branch to the commit before the last one, discarding all changes in your working directory.
To undo a commit after pushing it to a remote repository, use:
git revert HEAD
git push origin <branch>This command creates a new commit that undoes the changes introduced by the last commit. You can then push this new commit to the remote repository.
To undo a commit and all changes after pushing it to a remote repository, use:
git reset --hard HEAD~1
git push --forceThis command resets the current branch to the commit before the last one, discarding all changes in your working directory. The --force flag is required to update the remote repository with the rewritten history.
To discard a file from the staging area while keeping the changes in your working directory, use:
git reset HEAD <file>This command unstages the specified file, but keeps the changes in your working directory.
To discard all files from the staging area while keeping the changes in your working directory, use:
git resetThis command unstages all files, but keeps the changes in your working directory.
To discard changes in a specific file and revert it to the last committed state, use:
git checkout -- file.txtThis command reverts file.txt to the last committed state, discarding any changes made to it.
To discard all changes in a specific file and revert it to the last committed state, use:
git checkout HEAD -- file.txtThis command reverts file.txt to the last committed state, discarding any changes made to it.
To discard all changes in all files and revert them to the last committed state, use:
git checkout -- .This command reverts all files in the working directory to the last committed state, discarding any changes made to them.
To fix the comment of the last commit, use:
git commit --amendThis command opens an editor to modify the comment of the last commit. Save and close the editor to update the commit message.
To fix the author of the last commit, use:
git commit --amend --author="New Name"This command updates the author of the last commit. Replace New Name with the desired author name.
To add a file to the last commit that you have forgotten, use:
git add forgotten-file.txt
git commit --amendThis command stages the forgotten file and opens an editor to modify the last commit. Save and close the editor to add the file to the last commit.
To add changes to the last commit without modifying the commit message, use:
git add .
git commit --amend --no-editThis command stages all changes and adds them to the last commit without changing the commit message.
To revert a commit in the middle of the history, use:
git revert <commit-hash>This command creates a new commit that undoes the changes introduced by the specified commit. You can then push this new commit to the remote repository.
To delete a commit in the middle of the history and all changes introduced by it, use:
git rebase -i <commit-hash>^This command opens an interactive rebase session where you can delete the specified commit. Save and close the editor to rewrite the history.
To squash multiple commits into one, use:
git rebase -i HEAD~<number-of-commits>This command opens an interactive rebase session where you can squash the specified number of commits into one. Save and close the editor to rewrite the history.
To get the changes from a specific commit without creating a new commit, use:
git cherry-pick <commit-hash>This command applies the changes introduced by the specified commit to the current branch.
To move a commit to another branch, use:
git cherry-pick <commit-hash>
git checkout <destination-branch>
git cherry-pick <commit-hash>
git checkout <source-branch>
git reset --hard HEAD~1This sequence of commands cherry-picks the specified commit to the destination branch, resets the source branch to the commit before the last one, and discards the commit from the source branch.
To check the current branch name, use:
git branch --show-currentor simply
git branchor
git statusThese commands display the current branch name in the output.
To check the remote URL of a repository, use:
git remote get-url originThis command displays the URL of the remote repository named origin.
To check the remote branch associated with the current branch, use:
git branch -vvTo delete a branch, use:
git branch -d <branch-name>To delete a remote branch, use:
git push origin --delete <branch-name>To rename a branch, use:
git branch -m <new-branch-name>This command renames the current branch to new-branch-name.
To delete branches that are not on the remote, use:
git fetch --pruneThis command fetches the latest changes from the remote repository and prunes (deletes) local branches that no longer exist on the remote.
To list all branches, use:
git branch -aThis command displays both local and remote branches.
To list all remote branches, use:
git branch -rThis command displays only remote branches.
To list all branches with the last commit message, use:
git branch -vThis command displays the last commit message for each branch.
To display a graph of all commits and branches, use:
git log --oneline --graph --all