I sometimes check out some previous version of the code to examine or test. I have seen instructions on what to do if I wish to modify previous commits -- but suppose I make no changes. After I've done e.g. git checkout HEAD^
, how do I get back to the tip of the branch?.. git log
no longer shows me the SHA of the latest commit.
This question is related to
git
version-control
If you have a branch different than master, one easy way is to check out that branch, then check out master. Voila, you are back at the tip of master. There's probably smarter ways...
Came across this question just now and have something to add
To go to the most recent commit:
git checkout $(git log --branches -1 --pretty=format:"%H")
Explanation:
git log --branches
shows log of commits from all local branches
-1
limit to one commit → most recent commit
--pretty=format:"%H"
format to only show commit hash
git checkout $(...)
use output of subshell as argument for checkout
Note:
This will result in a detached head though (because we checkout directly to the commit). This can be avoided by extracting the branch name using sed
, explained below.
To go to the branch of the most recent commit:
git checkout $(git log --branches -1 --pretty=format:'%D' | sed 's/.*, //g')
Explanation:
git log --branches
shows log of commits from all local branches
-1
limit to one commit → most recent commit
--pretty=format:"%D"
format to only show ref names
| sed 's/.*, //g'
ignore all but the last of multiple refs (*)
git checkout $(...)
use output of subshell as argument for checkout
*) HEAD and remote branches are listed first, local branches are listed last in alphabetically descending order, so the one remaining will be the alphabetically first branch name
Note:
This will always only use the (alphabetically) first branch name if there are multiple for that commit.
Anyway, I think the best solution would just be to display the ref names for the most recent commit to know where to checkout to:
git log --branches -1 --pretty=format:'%D'
E.g. create the alias git top
for that command.
Have a look at the graphical GUI ... gitk
it shows all commits. Sometimes it is easier to work graphical ... ^^
You can use one of the following git command for this:
git checkout master
git checkout branchname
git checkout master
master is the tip, or the last commit. gitk will only show you up to where you are in the tree at the time. git reflog will show all the commits, but in this case, you just want the tip, so git checkout master.
If your latest commit is on the master branch, you can simply use
git checkout master
git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>
You can simply do git pull origin branchname
. It will fetch the latest commit again.
show all branches and commit
git log --branches --oneline
show last commit
git log --branches -1 --oneline
show before last commit
git log --branches -2 --oneline
Source: Stackoverflow.com