Delete one of multiple Heroku remote branches (Heroku CLI) [duplicate] - django

Failed Attempts to Delete a Remote Branch:
$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.
$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).
$ git push
Everything up-to-date
$ git pull
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.
How do I properly delete the remotes/origin/bugfix branch both locally and remotely?

Executive Summary
git push -d <remote_name> <branchname>
git branch -d <branchname>
Note: In most cases, <remote_name> will be origin.
Delete Local Branch
To delete the local branch use one of the following:
git branch -d <branch_name>
git branch -D <branch_name>
The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
The -D option is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]
As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
You will receive an error if you try to delete the currently selected branch.
Delete Remote Branch
As of Git v1.7.0, you can delete a remote branch using
$ git push <remote_name> --delete <branch_name>
which might be easier to remember than
$ git push <remote_name> :<branch_name>
which was added in Git v1.5.0 "to delete a remote branch or a tag."
Starting with Git v2.8.0, you can also use git push with the -d option as an alias for --delete. Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.
Delete Remote Branch [Original Answer from 5-Jan-2010]
From Chapter 3 of Pro Git by Scott Chacon:
Deleting Remote Branches
Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s main branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:
$ git push origin :serverfix
To git#github.com:schacon/simplegit.git
- [deleted] serverfix
Boom. No more branches on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”
I issued git push origin: bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).
Then you should execute this on other machines
# Fetch changes from all remotes and locally delete
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune
to propagate changes.

Matthew’s answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:
to remove a local branch from your machine: git branch -d {local_branch} (use -D instead to force deleting the branch without checking merged status);
to remove a remote branch from the server: git push origin -d {remote_branch}.
Reference: Git: Delete a branch (local or remote).

The short answers
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch
git push origin --delete <branch> # Git version 1.7.0 or newer
git push origin -d <branch> # Shorter version (Git 1.7.0 or newer)
git push origin :<branch> # Git versions older than 1.7.0
Deleting a local branch
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches
Deleting a local remote-tracking branch
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter
git fetch <remote> --prune # Delete multiple obsolete remote-tracking branches
git fetch <remote> -p # Shorter
The long answer: there are three different branches to delete!
When you're dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:
The local branch X.
The remote origin branch X.
The local remote-tracking branch origin/X that tracks the remote branch X.
The original poster used:
git branch -rd origin/bugfix
Which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.
To delete that actual remote branch, you need
git push origin --delete bugfix
Additional details
The following sections describe additional details to consider when deleting your remote and remote-tracking branches.
Pushing to delete remote branches also removes remote-tracking branches
Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p. However, it wouldn't hurt if you did it anyway.
You can verify that the remote-tracking branch origin/X was also deleted by running the following:
# View just remote-tracking branches
git branch --remotes
git branch -r
# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a
Pruning the obsolete local remote-tracking branch origin/X
If you didn't delete your remote branch X from the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.
A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:
git fetch origin --prune
git fetch origin -p # Shorter
Here is the relevant quote from the 1.6.6 release notes (emphasis mine):
"git fetch" learned --all and --multiple options, to run fetch from
many repositories, and --prune option to remove remote tracking
branches that went stale. These make "git remote update" and "git
remote prune" less necessary (there is no plan to remove "remote
update" nor "remote prune", though).
Alternative to above automatic pruning for obsolete remote-tracking branches
Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p, you can avoid making the extra network operation by just manually removing the branch(es) with the --remotes or -r flags:
git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter
See Also
git-branch(1) Manual Page.
git-fetch(1) Manual Page.
Pro Git § 3.5 Git Branching - Remote Branches.

Steps for deleting a branch:
For deleting the remote branch:
git push origin --delete <your_branch>
For deleting the local branch, you have three ways:
1: git branch -D <branch_name>
2: git branch --delete --force <branch_name> # Same as -D
3: git branch --delete <branch_name> # Error on unmerge
Explain: OK, just explain what's going on here!
Simply do git push origin --delete to delete your remote branch only, add the name of the branch at the end and this will delete and push it to remote at the same time...
Also, git branch -D, which simply delete the local branch only!...
-D stands for --delete --force which will delete the branch even it's not merged (force delete), but you can also use -d which stands for --delete which throw an error respective of the branch merge status...
I also create the image below to show the steps:

You can also use the following to delete the remote branch
git push --delete origin serverfix
Which does the same thing as
git push origin :serverfix
but it may be easier to remember.

It's very simple:
To delete the remote branch
git push -d origin <branch-name>
Or
git push origin :<branch-name>
-- You can also delete tags with this syntax
To forcefully delete local branch
git branch -D <branch-name>
Note: do a git fetch --all --prune on other machines after deleting remote branch, to remove obsolete tracking branches.
Example
to remove local branch
git branch -D my-local-branch
to remove remote branch
git push origin :my-remote-branch
With the new version of git, its also possible to remove branch with
git push origin --delete <branch_name>
TIP:
if you want to see all available branches you can use git branch -a,
and to see just remote branches, you can use git branch -r

Tip: When you delete branches using
git branch -d <branchname> # Deletes local branch
or
git push origin :<branchname> # Deletes remote branch
only the references are deleted. Even though the branch is actually removed on the remote, the references to it still exists in the local repositories of your team members. This means that for other team members the deleted branches are still visible when they do a git branch -a.
To solve this, your team members can prune the deleted branches with
git remote prune <repository>
This is typically git remote prune origin.

If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
git checkout other_than_branch_to_be_deleted
Deleting the local branch:
git branch -D branch_to_be_deleted
Deleting the remote branch:
git push origin --delete branch_to_be_deleted

git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>

This is simple: Just run the following command:
To delete a Git branch both locally and remotely, first delete the local branch using this command:
git branch -d example
(Here example is the branch name.)
And after that, delete the remote branch using this command:
git push origin :example

Another approach is:
git push --prune origin
WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,
git push --mirror
will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).

I use the following in my Bash settings:
alias git-shoot="git push origin --delete"
Then you can call:
git-shoot branchname

Delete locally:
To delete a local branch, you can use:
git branch -d <branch_name>
To delete a branch forcibly, use -D instead of -d.
git branch -D <branch_name>
Delete remotely:
There are two options:
git push origin :branchname
git push origin --delete branchname
I would suggest you use the second way as it is more intuitive.

If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your ~/.gitconfig:
[alias]
rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"
Alternatively, you can add this to your global configuration from the command line using
git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'
NOTE: If using -d (lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use -D (uppercase D).

Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.
Relevant blog post: Create and delete branches

To delete your branch locally and remotely
Checkout to master branch - git checkout master
Delete your remote branch - git push origin --delete <branch-name>
Delete your local branch - git branch --delete <branch-name>

You can also do this using git remote prune origin
$ git remote prune origin
Pruning origin
URL: git#example.com/yourrepo.git
* [pruned] origin/some-branchs
It prunes and deletes remote-tracking branches from a git branch -r listing.

In addition to the other answers, I often use the git_remote_branch tool. It's an extra install, but it gets you a convenient way to interact with remote branches. In this case, to delete:
grb delete branch
I find that I also use the publish and track commands quite often.

A one-liner command to delete both local, and remote:
D=branch-name; git branch -D $D; git push origin :$D
Or add the alias below to your ~/.gitconfig. Usage: git kill branch-name
[alias]
kill = "!f(){ git branch -D \"$1\"; git push origin --delete \"$1\"; };f"

Deleting Branches
Let's assume our work on branch "contact-form" is done and we've already integrated it into "master". Since we don't need it anymore, we can delete it (locally):
$ git branch -d contact-form
And for deleting the remote branch:
git push origin --delete contact-form

Delete remote branch
git push origin :<branchname>
Delete local branch
git branch -D <branchname>
Delete local branch steps:
checkout to another branch
delete local branch

Simply say:
git branch -d <branch-name>
git push origin :<branch-name>

To delete locally - (normal)
git branch -d my_branch
If your branch is in a rebasing/merging progress and that was not done properly, it means you will get an error, Rebase/Merge in progress, so in that case, you won't be able to delete your branch.
So either you need to solve the rebasing/merging. Otherwise, you can do force delete by using,
git branch -D my_branch
To delete in remote:
git push --delete origin my_branch
You can do the same using:
git push origin :my_branch # Easy to remember both will do the same.
Graphical representation:

Now you can do it with the GitHub Desktop application.
After launching the application
Click on the project containing the branch
Switch to the branch you would like to delete
From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.
From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (AKA the machine you are currently working on)

git push origin --delete <branch Name>
is easier to remember than
git push origin :branchName

This won't work if you have a tag with the same name as the branch on the remote:
$ git push origin :branch-or-tag-name
error: dst refspec branch-or-tag-name matches more than one.
error: failed to push some refs to 'git#github.com:SomeName/some-repo.git'
In that case you need to specify that you want to delete the branch, not the tag:
git push origin :refs/heads/branch-or-tag-name
Similarly, to delete the tag instead of the branch you would use:
git push origin :refs/tags/branch-or-tag-name

Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need git branch -D branch_to_delete if it's not fully merged into some_other_branch, for example.
git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete
Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up-to-date remotes available on a repository you're tracking. I've observed git fetch will add remotes, not remove them. Here's an example of when git remote prune origin will actually do something:
User A does the steps above. User B would run the following commands to see the most up-to-date remote branches:
git fetch
git remote prune origin
git branch -r

I got sick of googling for this answer, so I took a similar approach to the answer that crizCraig posted earlier.
I added the following to my Bash profile:
function gitdelete(){
git push origin --delete $1
git branch -D $1
}
Then every time I'm done with a branch (merged into master, for example) I run the following in my terminal:
gitdelete my-branch-name
...which then deletes my-branch-name from origin as as well as locally.

According to the latest document using a terminal we can delete in the following way.
Delete in local:
git branch -D usermanagement
Delete in remote location:
git push --delete origin usermanagement

Use:
git push origin :bugfix # Deletes remote branch
git branch -d bugfix # Must delete local branch manually
If you are sure you want to delete it, run
git branch -D bugfix
Now to clean up deleted remote branches run
git remote prune origin

Related

C++ ipch visual studio tracked in git.Cannot push due to large size

I am using visual studio for C++ and I have forked a repo and during building,git tracked this huge file of 101MB which resides in src/.vs/v15/ipch
Now the problem is I cannot push any of my changes to the origin as GitHub's size limit is 100MB.
I committed 4-5 changes after this and cannot push a single of them to the origin.
Following is the error i get in bash:
remote: error :GH001:
I tried doing this :
git --rm cached <file>
This deleted my files from index but I still have to push those changes.
How can I deal with this issue?
Is there a way to go make n commits and undo tracking of .vs code when I changed it?
You can try BFG Repo-Cleaner in order to clean your local repo of that big file, but it applies only on a bare repo.
For you current local (non-bare) repo, you can follow "Removing sensitive data from a repository" and remove that one specific file from all your past commits.
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all
But actually, you should remove the .vs folder itself: see "Remove folder and its contents from git/GitHub's history"
git filter-branch --tree-filter 'rm -rf src/.vs' --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo .vs/ >> .gitignore
git add .gitignore
git commit -m 'Removing src/.vs from git history'
git gc
git push origin master --force

Git pushing codes From repository 1 To repository 2(collaborator repository)

I need some help because I have some codes that I pulled from my repository branch and changed a couple of things in it.
What I need is to push it into another repository that I have been a collaborator in.
Been trying to push the codes, and trying to access it but to no avail.
Can anybody help me with this one? Any help will be greatly appreciated.
You have two different repositories where you manage to code. First one from where you clone/pull code and made some changes. Second one is what, where you want to push your latest code.
If you clone your repository from GIT then It automatically attach its GIT Url into your application. To check run this command
git remote -v
Result:
origin https://username#github.com/project-name.git (fetch)
origin https://username#github.com/project-name.git (push)
This is url from where you clone/pull your latest code. Now we have created alias of URL as ORIGIN.
so when ever you want to push your code, you will do
git push origin master
Now Add another GIT URL(remote) where you want to push your latest code.
git remote add origin_two https://username-two#github.com/project-name.git
So now commit your changes and made a pull request from your secondary GIT URL this way.
git pull origin_two master
If you got any conflicts then make corrections in code and then again add untracked files using
git add file-name
add a commit message
git commit -m "Your message"
and push your code to git
git push origin_two master
If you are working with branches:
You have two repositories now for a single application. So for that each repo has their own branches, to list down branches for each origin just follow
git branch -a
will list all the branches from two remotes. now If you really want to push on any other branch then you should commit all the changes on current branch an then move to your favorable branch using command
git branch branch-name
and do code here whatever code you will change now push to particular branch
git push origin_two branch-name
That's it :) Hope this can help you.

GitHub pages for personal site. How to deploy a folder

If I am using GitHub pages for my personal site, how should manage my source files? I have a simple setup which works.
But I understand I can't using Jekyll plugins with GitHub pages. If I want to use Grunt for example to optimise my images, for a usual app/site, it will produce output in say a dist/public folder, which I will then deploy. But I want to still use GitHub to manage my source files, how should I do it?
Depending of repository kind your using, a User/Organization (UO) site or a Project site (P), sources and pages will be versionned in :
User/Organization - sources : sources, pages: master
Project - sources : master, pages: gh-pages
Note: The pages branch is mandatory, but the sources branch name can be changed.
Setup
Initialize an empty github repository : github.com/userName.github.io for UO or github.com/repositoryName for P
Locally, initialize your local repository :
Go to source folder cd /home/username/www/yoursite or anything you want
git init
git remote add origin git#github.com:userName/userName.github.io.git (UO) or git remote add origin git#github.com:userName/repositoryName.git (P)
git checkout -b sources (UO) or git checkout master (P)
in your .gitignore add your dist/public. As you are currently on master we will ignore it an version it in an other branch
git add -A
git commit -m "base sources". You now have committed your sources.
git push origin sources (UO) or git push origin master (P) push your sources in the appropriate branch
cd dist/public
touch .nojekyll, this file tells gh-pages that there is no need to build a Jekyll site
git init
git remote add origin git#github.com:userName/userName.github.io.git (UO) or git remote add origin git#github.com:userName/repositoryName.git (P)
git checkout master (UO) or git checkout -b gh-pages (P) put this repository on the appropriate branch
your grunt task here
git add -A
git commit -m "first build" commit your site code
git push origin master (UO) or git push origin gh-pages (P)
You now have pushed your code and pages in two different branches. They will now be pushed depending on where you are :
pushing sources
cd yourWorkingDirestory
git add -A
git commit -m "your commit message"
git push origin sources (UO) or git push origin master (P)
pushing pages
cd yourWorkingDirestory/dist/public
git add -A
git commit -m "your commit message"
git push origin master (UO) or git push origin gh-pages (P)

Add an existing origin to a django project and pull the existing repo in it.

I am new to GIT hub version Controlling System.
I am working on a project which i downloaded as zip from GIT hub. It is not a git repository. What i want to do is that I want to make it a git repository and want to pull the existing git repository in it when issuing a git pull command.
I want to create my own Development Branch, develop code on local, push code to github, then do a pull request
Help will be highly appreciated.
Since you don't have any history of your changes to preserve I think your best bet is to clone the repo normally, and then copy your changes into that repository. Your question is a bit sparse on details, but something like the following should work
git clone <git hub project> <new folder on your system>
# maybe you can use a tag here for the SHA
git checkout -b my_branch SHA_THAT_REPRESENTS_YOUR_ZIP_DOWNLOAD
cp -r <your existing directory> <your new git repository>
git status # abort if this step doesn't look right
git add # add all your changed files
git commit # commit your work
git rebase <main dev branch> # catch up

post_receive hook in git: how does it checkouts my non-git folder?

I am using webfaction for my web deployment.
I have a Django app at: webapps/django_app/project_name/
I have a Git repo at: webapps/git_app/repos/my_repo.git
my_repo.git is a bare repository. It is not a working directory.
whenever I push from my local development computer to the remote (webfaction --> my_repo.git), I want my django_app to get the pushed code.
I followed this post which works fine. But no explanation of how this works is given.
I have added these two lines in post_recieve hook in my_repo.git.
#!/bin/sh
GIT_WORK_TREE=/home/username/webapps/django/myproject git checkout -f
GIT_WORK_TREE=/home/username/webapps/django/myproject git reset --hard
what does this two lines actually do?
Moreover, my Djangoapp folder is not a git repo. still whenever push is made to my_repo.git, Djangoapp gets updated. so how does it work?
When you are managing files locally with .git, you typically have two things:
Your git repository, which is contained in the .git directory, and
Your work tree, which is the set of files you are actually editing.
By default, the repository is a subdirectory of the work tree, but this is not a requirement. Setting the GIT_WORK_TREE environment variable directs git to use a different location for your checkout out files.
So the first line...
GIT_WORK_TREE=/home/username/webapps/django/myproject git checkout -f
...is asking git to check out the HEAD of the repository into /home/username/webapps/django/myproject.
The second line...
GIT_WORK_TREE=/home/username/webapps/django/myproject git reset --hard
...makes sure that /home/username/webapps/django/myproject does not have any local changes. reset --hard discards any changes to files that are tracked by git. By "local changes" I mean any changes that you or someone else has made to files in this directory; ideally, there won't be any, but if there were some there, reset -f makes sure that the modified files are overwritten with the version of the file stored in the repository.
For more details on any of the commands listed here, try running git <command> --help for the man page, or see The Git Book.