文章目录
  1. 1. Git: Delete a branch (local or remote) - makandra dev
  2. 2. Git: Delete a branch (local or remote)
    1. 2.1. Note
    2. 2.2. Author of this card:
    3. 2.3. Related cards:
    4. 2.4. Git: Remove information on branches that were deleted on origin
    5. 2.5. Git: How to check out branches that exist on multiple remotes
    6. 2.6. Git: List remote branches
    7. 2.7. Git: How to configure git to push only your current branch
    8. 2.8. Check out a remote branch in git
    9. 2.9. How do you make an existing Git branch track a remote branch? - Stack Overflow
    10. 2.10. Getting permanent links to files on Github or Gitlab
    11. 2.11. List your current Git remotes
    12. 2.12. Basic git commands

Git: Delete a branch (local or remote) - makandra dev

git git-branch

« next card
previous card »
back to all cards in this deck

Posted over 7 years ago. Visible to the public.

Git: Delete a branch (local or remote)

To delete a local branch

Copygit branch -d the_local_branch

To remove a remote branch (if you know what you are doing!)

Copygit push origin :the_remote_branch

or simply use the new syntax (v1.7.0)

Copygit push origin --delete the_remote_branch

Note

If you get the errorerror: unable to push to unqualified destination: the_remote_branch The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'

perhaps someone else has already deleted the branch. Try to synchronize your branch list with

Copygit fetch -p 

The git manual says -p, --prune After fetching, remove any remote-tracking branches which no longer exist on the remote.

Protect your Rails app from security breaches
Does your version of Ruby on Rails still receive security updates?

Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Learn more

Author of this card:

-45.png

Tobias Kraze
makandra.de
Say thanks

2171

Last edit:11 months ago
by Daniel Straßner
About this deck:We are makandra and do test-driven, agile Ruby on Rails software development.License for source code

Git: Remove information on branches that were deleted on origin

When branches get deleted on origin, your local repository won’t take notice of that.

You’ll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.

You can clean up that information locally like this:

git remote prune origin

Your local copies of deleted branches are not removed by this.

The same effect is achieved by using (kudos to Julien):

git fetch --prune

You could also set that as a default.

Git: How to check out branches that exist on multiple remotes

So you’re using multiple remotes that offer the same branch?

$ git branch -a | grep my-branch
  remotes/something/my-branch
  remotes/origin/my-branch

And when trying to check out that remote branch, it fails for you with an error like this?

$ git checkout my-branch
error: pathspec 'my-branch' did not match any file(s) known to git.

Git usually guesses the remote branch to check out, but when using more than one remote, it seems like it no longer can do that.

Even if the branch is the same on both remotes, you nee…

-46.png

Git: List remote branches

Sometimes you may need to figure out what branches exist on a remote repository so you can pull them down and check them out, merge them into your local branches, etc. You can see the remote branches by saying

git branch -r

Or, if you want to see both local and remote branches, you can say

git branch -a

%!(EXTRA markdown.ResourceType=, string=, string=)

Git: How to configure git to push only your current branch

You can change which branches will be pushed when saying git push. Our recommendation is to set it to current.

From the git-config documentation:

push.default Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

  • nothing - do not push anything.
  • matching - push all matching branches. All bra…

Check out a remote branch in git

If you would like to checkout the branch groups, you can simply say this in recent versions of Git:

git fetch origin
git checkout groups

This will automatically track origin/groups from a local branch groups.

In older Git versions you need to say this:

git fetch origin
git checkout --track origin/groups

%!(EXTRA markdown.ResourceType=, string=, string=)

How do you make an existing Git branch track a remote branch? - Stack Overflow

%!(EXTRA markdown.ResourceType=, string=, string=)

%!(EXTRA markdown.ResourceType=, string=, string=)

Please don’t simply copy line number links from Github. The URL usually contains a branch name like master:

https://github.com/makandra/upjs/blob/master/lib/assets/javascripts/up/link.js.coffee#L76

If someone now posts an insertion or deletion to that file into master your link points to the wrong line!

A better way is to (Github) press the Y key after clicking on a line number or (Gitlab) clicking the “Permalink” button in the top bar.

This will transform the URL to another URL that points to the particular commit:


h…

## [How to mirror a git repo to a new remote](https://makandracards.com/makandra/48818-how-to-mirror-a-git-repo-to-a-new-remote)

Say you want to move a git repository from one remote (perhaps Github) to another (perhaps Gitlab).

If you have the repo checked out, you still should make sure to mirror all branches of the old remote, not only those you happen to have checked our. Otherwise the target repo will become a copy of your current repo, and not the source repo, potentially missing commits. You can use this:

git rename origin old-origin

git remote add origin git fetch old-origin –prune git push –prune origin +refs/remotes/old-origin/*:refs/hea…

List your current Git remotes

To display a list of your current Git remotes and their endpoints, you can say

git remote -v

The output looks like this:

brady8	https://github.com/brady8/aegis.git (fetch)
brady8	https://github.com/brady8/aegis.git (push)
origin	git@github.com:makandra/aegis.git (fetch)
origin	git@github.com:makandra/aegis.git (push)

Basic git commands

This is for people recovering from Subversion.

Get an existing from the server for the first time git clone git@example.com:repositoryname See what’s changed git status Check in locally git commit -m "good description" Push local commits to the server git push Get and merge updates from the server git pull Stage a file for the next local commit git add file Stage all files for the next local commit git add . Create a new local branch and check it out git checkout -b branchnameSwitch to a local branch…

文章目录
  1. 1. Git: Delete a branch (local or remote) - makandra dev
  2. 2. Git: Delete a branch (local or remote)
    1. 2.1. Note
    2. 2.2. Author of this card:
    3. 2.3. Related cards:
    4. 2.4. Git: Remove information on branches that were deleted on origin
    5. 2.5. Git: How to check out branches that exist on multiple remotes
    6. 2.6. Git: List remote branches
    7. 2.7. Git: How to configure git to push only your current branch
    8. 2.8. Check out a remote branch in git
    9. 2.9. How do you make an existing Git branch track a remote branch? - Stack Overflow
    10. 2.10. Getting permanent links to files on Github or Gitlab
    11. 2.11. List your current Git remotes
    12. 2.12. Basic git commands