Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge with a single dash

Tags:

git

git-merge

I know that the command git checkout - checks you out to the previous active branch or detached HEAD.

What I would like to know is if the command git merge - merges the previous active branch or detached HEAD to my currently checked out branch.

like image 387
vchan Avatar asked Jul 27 '21 08:07

vchan


People also ask

What does single dash mean in git?

The single dash here means the previous active branch or detached HEAD.

How do I merge in git?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

Does git merge overwrite?

Usually git does not overwrite anything during merge.

What is merging in Git and how to do it?

Merging in git is the process of connecting forked history. That facilitates the joining of two branch histories into one main. When you finish working with all the relevant commits for your active repository, git allows you to merge all into the main branch. Git merge is simply the act of combining branches.

What is the fast-forward merging in Git?

The above illustration is what is known as the fast-forward merging in git. We can also merge feat-branch into the main branch which will alter the current history as shown in the following diagram. From the diagram, feat-branch is the active branch and it is pointing to the last commit E while the main-branch still points to commit D.

How do I merge multiple commits in one branch?

Squash and Merge Commits in Git Run the following Git commands to squash all commits in a branch into one and merge this branch into master with a single commit message: $ git checkout master $ git merge --squash <branch_name> $ git commit If you are working with a remote Git repository, don’t forget to push your changes:

How to merge only one file from a git branch (patching)?

Merging only one file from a git branch (patching) The Solution. To start we begin by creating a new branch. This is good practice as it reduces the likeliness of... Using the Editor. By default, the editor that will be opened is vim. Here we get the same segment of code displayed, but... Saving and ...


2 Answers

Your assumption is correct and git merge - merges the branch/commit checked out before the current one into your currently checked out branch.

The release notes for Git 1.7.6 explicitly state this:

git merge learned - as a short-hand for the previous branch, just like the way git checkout - works.

In commands that allow this, - is a shorthand for @{-1} which is specified in gitrevisions:

@{-<n>}, e.g. @{-1}
The construct @{-<n>} means the <n>th branch/commit checked out before the current one.


By looking through the release notes and the source code, I was able to identify the following commands that support a lone - as a short hand for @{-1}:

  • git-checkout (since Git 1.6.2)
  • git-merge (since Git 1.7.6)
  • git-rebase (since Git 2.0.0)
  • git-switch
  • git-worktree
  • git-revert
  • git-cherry-pick
like image 153
Matt Avatar answered Oct 28 '22 13:10

Matt


The documentation for git-checkout describes how you can describe the branch you want using the @{-N} syntax:

You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using "git checkout" operation. You may also specify - which is synonymous to @{-1}.

So far I haven't found any other documentation that says - is synonymous with @{-1}, but this notation is documented in git-revisions which means that it's more general than just for use with git checkout. It seems reasonable to think you can use it whereever a branch or commit is required.

I habitually use both git checkout - and git merge -. They are very useful, and I've never had a problem with either of them. So to answer your question directly, yes, git merge interprets - just like git checkout. That said, it's a shame the documentation is so arcane.

like image 32
Dominic Cronin Avatar answered Oct 28 '22 13:10

Dominic Cronin