Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cof, col, cop and con alias #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

MinsonLee
Copy link

No description provided.

Signed-off-by: MinsonLee <lms.minsonlee@gmail.com>
* `git cof` - switch to a branch's first commit hash
* `git col` - switch to a branch's last commit hash
* `git cop` - switch to the previous (or previous N) commit hash
* `git con` - switch to the next (or next N) commit

Signed-off-by: MinsonLee <lms.minsonlee@gmail.com>
@MinsonLee MinsonLee closed this Jan 18, 2025
@MinsonLee MinsonLee reopened this Jan 18, 2025
@MinsonLee MinsonLee closed this Jan 18, 2025
@MinsonLee MinsonLee changed the title Add cof alias Add cof, col, cop and con alias Jan 18, 2025
@MinsonLee
Copy link
Author

  • git cof - switch to a branch's first commit hash
  • git col - switch to a branch's last commit hash
  • git cop - switch to the previous (or previous N) commit hash
  • git con - switch to the next (or next N) commit

@MinsonLee MinsonLee reopened this Jan 18, 2025
@@ -156,11 +156,109 @@

# checkout - update the working tree to match a branch or paths. [same as "o" for "out"]
co = checkout
cog = checkout -guess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-guess with a single dash?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply.

I saw there is an alias for cog in readme.md, but I didn't find it in gitalias.txt.

I re-read the git documentation about git-checkout and found a sentence :

--guess is the default behavior. Use --no-guess to disable it.

I think it would be better to remove the cog alias description in README.md

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern was a single dash. --guess must be with double dashes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be --guess

# git cof foo
#
cof = !"f() { \
REMOTE=\"$(git remote)\"; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the repository has multiple remotes? I often have 2 or 3 — gh for GitHub, origin for my own clean copy, and web to publish at my own hosting.

#
cof = !"f() { \
REMOTE=\"$(git remote)\"; \
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you get one remote repo git symbolic-ref could fail:

$ git symbolic-ref refs/remotes/gh/HEAD
fatal: ref refs/remotes/gh/HEAD is not a symbolic ref

#
cof = !"f() { \
REMOTE=\"$(git remote)\"; \
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if there is a parameter you can skip these 2 commands thus making these aliases faster.

#
cof = !"f() { \
REMOTE=\"$(git remote)\"; \
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if there are no parameters why work with remotes at all? In detached HEAD state remote HEAD could be completely different from the current HEAD, it's hardly a way to get the current branch. There is no reliable way to get the current branch in detached HEAD state. If my current branch is development and current commit is development~10, but the remote HEAD is refs/remotes/origin/master then what?

IMO the best course of actions would be to get the current branch from the HEAD and in case of detached HEAD fail and force user to provide branch name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or may be guess with git branch --contains HEAD ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git for-each-ref --contains HEAD --count=1 --format="%(refname)" refs/heads/

cong = checkout --no-guess

# create a new branch from the current working tree, and switch to it
cob = checkout -b

# cof - switch to a branch's first commit hash.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps IWBN to explain that "first commit" means the very first, initial commit of the current tree, not the commit from which the current branch was forked.

@phdru
Copy link
Contributor

phdru commented Jan 18, 2025

May be split these aliases into smaller parts? 1. Get the current branch, single alias. 2. Get the next/prev/1st/last commit. 3. Checkout to the next/prev/1st/last commit.

@phdru
Copy link
Contributor

phdru commented Jan 18, 2025

When git con/git col return to the tip commit of the branch should they stay in the detached HEAD state? Or should they checkout the branch?

@joelparkerhenderson
Copy link
Member

Great ideas and PR, thank you. Great comments @phdru.

Here's what I see in this PR... there's an implementation request here that you both are suggesting. How does a user query for a specific commit? This is a big topic that entangles many developers.

What could this commit look like if there's an entirely new top-level one-letter gitalias that queries for a commit hash? Then you could do query by position (first, prev, etc), query by metadata (author, date, etc.), query by content (text, bisect), etc.

@MinsonLee
Copy link
Author

@phdru Thank you for these great suggestions!

I'll try to fix it and resolve it when I have some free time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants