- Beginner guide
- Undo changes
- Push only specific commits
- Copy file from another commit or branch
- Fetch a remote branch
- Reset a branch based on another
- Using the stash
- Search in commit messages
This is for undoing changes on an untracked file.
git checkout -- <file>There is also the new command git restore.
git restore <file>Type man git restore for more about this command, especially the --staged
and --worktree options.
git reset <file>See here for more.
git reset HEAD~git reset is the command responsible for the undo. It will undo your
last commit while leaving your working tree (the state of your files on disk) untouched.
You'll need to add them again before you can commit them again.
More explanations here.
This will destroy any local modifications. Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc3 # Replace the commit hash by the one you want to rollback toAlternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc3 # Replace the commit hash by the one you want to rollback to
git stash popThis saves the modifications, then reapplies that patch after resetting. You could get merge conflicts, if you've modified things which were changed since the commit you reset to.
Source: SO.
git revert --no-commit 0d1d7fc3..HEAD # Replace the commit hash by the one you want to rollback to
git commitThis will revert everything from the HEAD back to the commit hash (excluded),
meaning it will recreate that commit state in the working tree as if every
commit after 0d1d7fc3 had been walked back. You can then commit the current
tree, and it will create a brand new commit essentially equivalent to the
commit you "reverted" to.
The --no-commit flag lets git revert all the commits at once - otherwise
you'll be prompted for a message for each commit in the range, littering your
history with unnecessary new commits.
See this SO answer for detailled explanations.
Note that this only works if there was no merge commit since the commit we want to rollback to. See this answer if merge commits exist.
Sometimes there are a few commits pending to be pushed but you don't want to push all of them for some reason, e.g. partial deployment, and so you want to push them only up to a certain commit.
Then you can do this:
git push <remote> <commit hash>:<branch>For example, you have 5 commits A > B > C > D > E pending (for simplicity, ABCDE are commit hashes), and you want to push up to commit "C". The following will then push A, B, and C to origin/main.
git push origin C:main- https://coderwall.com/p/hexinq/git-push-up-to-a-certain-commit
- https://stackoverflow.com/questions/3230074
There are two options to perform this: using git checkout or git restore.
Source: StackOverflow.
git restore --source otherbranch path/to/myfile.txtThe difference with git checkout (see below) is that git checkout copies the file to the working directory
(your files on disk) but also to the staging area (much like a git add). git checkout by default changes
only the working directory.
Run this from the branch where you want the file to end up:
git checkout otherbranch myfile.txtGeneral formulas:
git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>Some notes (from comments in the SO thread):
- Using the commit hash, you can pull files from any commit.
- This works for files and directories.
- Overwrites the file
myfile.txtandmydir. - Wildcards don't work, but relative paths do.
- Multiple paths can be specified.
If you need to fetch a remote branch that is not in your local repository,
use git switch.
git switch name-of-remote-branchYou will get an output similar to this:
branch 'foo-bar-branch' set up to track 'origin/foo-bar-branch'.
Switched to a new branch 'foo-bar-branch'To reset a branch named my-feature-branch based on the main branch for example:
git switch main && git pull # get the latest changes from remote
git switch my-feature-branch # switch back to the feature branch
git reset --hard mainA simple and concise tutorial is available here.
The following sections are taken from the article.
Git features the stash, which is as much as a good place to store uncommitted changes. When you stash you changes, they will be stored, and your working copy will be reverted to HEAD (the last commit revision) of your code.
When you restore your stash, you changes are reapplied and you continue working on your code.
$ git stash save <optional message for later reference>Example
git stash save 'a dummy stash operation'You will get an output like:
Saved working directory and index state On main: a dummy stash operationIt is possible to have more than one stash. The stash works like a stack. Every time a new stash is saved, it's put on top of the stack.
git stash list
stash@{0}: On main: a dummy stash operationThe stash@{0} is the stash ID, it will be used to restore it later.
The stash ID changes with every stash made. stash@{0} refers to the last stash made.
git stash apply stash@{0}You may notice the stash is still there after you have applied it. You can drop it if you don't need it any more.
git stash drop stash@{0}git stash popgit stash clearIf you need to search for a commit which messages contains a given text, use:
git log --grep='pattern'Note: This will only search in the history of the active branch.