How can I efficiently navigate a Git repo to keep track of project development?

I am trying to understand a relatively new but non-trivial tool that is being developed in a git repository.

There isn't much documentation in the code, but the repository has picked up <100 commits so far and I can see as I understand what's going on better if I do something like:

  • checkout

    the very first commit; look at the code
  • checkout

    say ~ 5-6 commits later; see how this has changed.
  • rinse-and-repeat # 2 until i update

.. instead of just looking at all the code in the HEAD of the master right now, which is much more complicated.

The problem with my idea is that when I am git checkout $commit_1

, I am in a separate head state, so in order to move to any new commit I have to again git checkout master

, and then work my way up to commit, I want. Is there a more convenient way to do this? for example check out an old commit and then ask git to show me what the new commits are and go to one of them.

+3


source to share


2 answers


No need to check again master

all the time or type loads of SHA-1 on a piece of paper. At any stage, you can run

git log --oneline --decorate --graph master

      

For convenience, you can define the following alias, as Git users do:

git config alias.lg "log --oneline --decorate --graph"

      

Then the command above just becomes

git lg master

      



This will result in a log of all ancestors (albeit in condensed form) of your branch master

, no matter where you are "in the commit graph."

This is a great way to understand and determine which commit you want to check, or further check, through the appropriate short SHA-1 hash. Note that using a flag --decorate

shows you where you are ( HEAD

).

Example

Here's an example of how I'm browsing one of my own Git repositories:

$ git branch
* master
$ git tag
v0.1
v0.2
v0.3
$ git checkout v0.3
Note: checking out 'v0.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at cfc71e4... mention compatibility with subset of Octave syntax
$ git log --oneline --decorate --graph master
* 73ec762 (origin/master, origin/HEAD, master) improve brace style
* ffc8d67 remove superfluous word; delete trailing whitespace
* 3f8b8db remove obsolete comment about mcode
* f9f9fd0 remove .DS_Store file that was accidentally added
* cc855ab clarify description of mlonlyheader option
* 7553ccf change contact email
* 4d860e9 correct remarks on shell-escape and backtick
* 9a2ef02 corrected typo in Tips & tricks
* f0badb5 minor improvements in documentation
* cfc71e4 (HEAD, tag: v0.3) mention compatibility with subset of Octave syntax
* 7db2c88 Preventive bugfix: replace \toks@ by \toks@mlpr
* 01fdc43 delete OS-specific files from .gitignore
...

      

+4


source


Decide from git log

which commits is of interest. Write down their SHA and then just check them. No need to stop using the wizard every time.

Example:

$ git checkout 315e25
Note: checking out '315e25'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 315e25a... 85192566 - Use Linker js namespace for verifying link
07:35:55 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker (detached from 315e25a)
$ git checkout d93b9c
Previous HEAD position was 315e25a... 85192566 - Use Linker js namespace for verifying link
HEAD is now at d93b9c7... 85192548 - Use Linker js domain for show group members
07:36:13 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker (detached from d93b9c7)
$ 

      



When you're done checking your commit history and various interests, you can go back to the wizard:

$ git checkout master
Previous HEAD position was d93b9c7... 85192548 - Use Linker js domain for show group members
Switched to branch 'master'
07:37:37 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker master
$ g
On branch master
nothing to commit, working directory clean

      

You can also use relative numbering to navigate through SHA as described in Zeeker's comment.

+1


source







All Articles