8

For a few days I was re-writing install.sh file for Scrollback project and since I was the only one working on this and doing it locally, I kept commit ammending the same commit, pushing once in a while to my fork's master. (please ignore best practises here, I was working alone).

In between I remember emailing someone showing my half done work, the URL https://github.com/sindhus/scrollback/blob/8d8f7f414383499c2ab6fec586b4c9665a41c7aa/install.sh

Now by some confusion I lost out on my work locally (think rm -rf), I remember pushing prior to this. So github at some point did see my rebased commit ID of install.sh.

As you can see the above URL lets me access this blob by a commit ID. However I can't access it locally because that same repo was force pushed.

My question how do I get github to show me all commit IDs for a file EVER? All IDs it possibly knows of for that file regardless of path. If I have to use their API I don't mind but I'd like some ideas to dig deep into this.

Thanks!

Sindhu S
  • 942
  • 1
  • 10
  • 23

4 Answers4

21

My question how do I get github to show me all commit IDs for a file EVER

If you forced push (git push --force) your revised commit once in a while, that commit 8d8f7 has been replaced by a more recent commit with a different SHA.

That means 8d8f7 is now only reference in the reflog of the GitHub repo, which only GitHub support can give you access to.
Cloning the repo would not include 8d8f7 in the local history of that cloned repo.


GitHub "reflog": push events from GitHub Events API

Actually the OP sindhus points out in the comments to "Recovering a commit from Github’s Reflog" by John Engelman:

The GitHub Events API allows to browse through the last events:

curl https://api.github.com/repos/<user>/<repo>/events

The "pushEvent" is the one to look for.

Then one can directly create a branch on GitHub in order to make that commit visible again (because not dangling anymore, but reference by an actual object like a branch):

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"ref":"refs/heads/D-commit", "sha":"384f275933d5b762cdb27175aeff1263a8a7b7f7"}' https://api.github.com/repos/<user>/<repo>/git/refs

# JSON request
{
  "ref": "refs/heads/D-commit",
  "sha": "384f275933d5b762cdb27175aeff1263a8a7b7f7"
}

You might need to use a token for the authentication.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

i didn't really get the question so there is some solution:

To see all commit of a specific file: git log --follow filename.

To checkout to old version, find answer here

Community
  • 1
  • 1
LolWalid
  • 515
  • 5
  • 15
0

Clone your repo locally then try this on your file:

git log --follow install.sh

It should show you IDs that you can use on github.

PJ Bergeron
  • 2,788
  • 4
  • 25
  • 42
0

I'm not sure if there's a single way you can get all versions of a file across amended commits. However, reflog will contain information on the earlier ones and you can extract them manually. An example follows.

This is my first commit

echo "a" > a.txt && git add a.txt && git commit -m "Version 0"

After that, a few more amends.

% echo "aa" > a.txt && git add a.txt && git commit --amend -m "Version 1"
% echo "aaa" > a.txt && git add a.txt && git commit --amend -m "Version 2"
% echo "aaaa" > a.txt && git add a.txt && git commit --amend -m "Version 3"
% echo "aaaaa" > a.txt && git add a.txt && git commit --amend -m "Version 4"

While my log just one entry

% git log --oneline a8d6c39 Version 4

My reflog has everything

% git reflog master
a8d6c39 master@{0}: commit (amend): Version 4
cf87b8f master@{1}: commit (amend): Version 3
c45a91e master@{2}: commit (amend): Version 2
63c7f5a master@{3}: commit (amend): Version 1
f2b3336 master@{4}: commit (initial): Version 0

So, if you want to see what your file looked like at Version 4, Version3 etc., you can do this

% git show a8d6c39:a.txt
aaaaa
% git show cf87b8f:a.txt
aaaa
% git show c45a91e:a.txt
aaa
% git show 63c7f5a:a.txt
aa
% git show f2b3336:a.txt
a

In general though, the "process" of continuously amending is bad even if you're the only developer. It's a one off thing you should do to fix mistakes the last commit.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169