How to step through a git repository?
I am trying to understand a project, it helps to look at its evolution by using gitk. What I do is checkout the first commit, understand code, run tests, go to next commit and repeat. My current workflow is to checkout the commi开发者_运维知识库t through its hash
git checkout 79cd6
But what I would like is another branch where I can perform my own changes, and allows me to merge commits from the master branch, but without the need to find the commit hash. Idealized workflow:
git checkout -b <newbranch> <first commit id of master>
git <command to move head of current branch to next commit of master>
I know this is a bit of an old question, but I wanted to do the same thing and found an answer so I figured I'd share.
for commit in $(git rev-list master --reverse)
do
git checkout $commit
read
done
do this in one window. It will start with your initial commit, and advance once each time you hit enter. Then do your testing etc. in a different shell.
It'll work perfectly with a linear history, but I'm not quite sure how it will handle merges and such. I'm sure it'll be reasonable, but your mileage might vary a bit.
Save this as ~/bin/git-next
or somewhere else in your path:
#!/bin/bash
git co $(git rev-list --children --all | awk "/^$(git rev-parse @\{0})/ { print \$2; }")
This will check out the first child of the current revision.
It will be very slow on a big repo but it will do the job.
You can modify this to handle your branching as needed.
You could write a little shell script. One script would run git log --pretty=oneline | awk '{print $1;}'
. The second script (call it step
or something) would use head
, tail
, wc -l
. Then read the last line of the file with tail
, find out how many lines are in the file, and remove the last line from the file. Ugly, sure, but it'd do the job. :) (If you wanted to be a little less ugly, it could use truncate(1)
to just chop off the last line of the file, rather than always making new temporary files.)
I wanted an easy way to step through commits in a branch for code review/demo. This is the little script I came up with to automate it. Drop the following in a file, say, git_step
and run it like so: git_step d17dacce8b13b2ad454003da7cbd670a06c41437
#!/bin/bash
BASE_REV=${BASE_REV:-$1};
current_branch=$(git branch | grep '^*' | awk '{print $2}');
for rev in $(git rev-list ${BASE_REV}..HEAD | tail -r); do
git checkout $rev;
LESS='RSX' git show -p $rev;
done;
git checkout ${current_branch};
Also available as a gist here.
精彩评论