GitBash cmd line branch delete - git-branch

what does it mean that I see (feature/StoreTextVariable_134) (I assume branch I created earlier in and switched to in VS, and I deleted the local code earlier)?
how can I get rid of it?
I'm a GitBash newbie.

what does it mean that I see (feature/StoreTextVariable_134)
feature/StoreTextVariable_134 is the branch of git that you are currently working on.
Type git branch to check how many branches in your source code.
how can I get rid of it?
Use git checkout to move to the branch you need to work with.
output
Note: If there is a change in source code on the current branch then you need to commit before switching branches.
Please refer more detail here:
Git document

Thanks #Tai I had to use -f with my checkout so that I could discard pending changes I didn't need
git checkout -f branchName
then to delete it I used
git branch -d -f branchName
now I am back on my default branch

Related

GitHub not Recognizing Differences in File Structure [duplicate]

I have a CMS theme installed on my machine. I'm tracking changes to it via git
and decided to back it up on GitHub so I could share those changes.
The theme as provided is also available on GitHub. On my machine I have added
this as a remote upstream. Now I can easily see the changes between my master
and the remote upstream by using the following command:
git diff --color master upstream/number
If I could add the remote upstream on GitHub I could easily share these changes.
Is it possible to set this relationship on GitHub?
I have tried the following:
git push -u origin upstreambranch
which adds an upstreambranch to the master on GitHub. However trying to
compare both branches doesn't work, the result I get on GitHub is that: "There
isn't anything to compare"
Is there an alternative way to compare these?
If the problem is "main and master are entirely different commit histories.", the following will work
git checkout master
git branch main master -f
git checkout main
git push origin main -f
The Short Answer
It looks like GitHub won't let you compare the branches because they don't
actually share any of the same history at all, even though they may share
much of the same files and code.
Here is a screenshot of the temporary fork I made of your repo, where I tried to
compare master with the upstreambranch, like you described. Notice the error
message:
It says:
There isn't anything to compare.
master and upstreambranch are entirely different commit histories.
The Long Answer
You probably downloaded the original source and added it to a completely new
repo instead of cloning the original repo, right? Doing that will make it so
that the history of your repo will be completely different from the
history of the original repo, since your new repo won't have any of the same
commits with the same sha IDs.
You can see that by doing a reverse log of your master branch and the
upstreambranch:
# Your first commit, see commit sha
git log --reverse master
commit c548d7b1b16b0350d7fbdb3ff1cfedcb38051397 # <== HERE
Author: Padraic Stack <padraic.stack#nuim.ie>
Date: Wed Apr 2 15:11:28 2014 +0100
First commit of everything
# First commit sha of the original repo
git log --reverse upstreambranch
commit 105a12817234033c45b4dc7522ff3103f473a862 # <== THERE
Author: Jeremy Boggs <jeremy#clioweb.org>
Date: Mon Feb 22 16:00:53 2010 +0000
Creates repo directories for the Seasons theme.
Solutions
If you redo your commits on top of the original history, you should then be able
to compare the branches. There are several different ways that you can redo your
commits, including
git rebase --onto
and
git cherry-pick
You also can redo each commit manually, if you have to.
I had a similar situation, where my master branch and the develop branch I was trying to merge had different commit histories. None of the above solutions worked for me. What did the trick was:
Starting from master:
git branch new_branch
git checkout new_branch
git merge develop --allow-unrelated-histories
Now in the new_branch, there are all the things from develop and I can easily merge into master, or create a pull request, as they now share the same commit hisotry.
I solve my issue using these commands
git checkout [BRANCH]
git branch master [BRANCH] -f
git checkout master
git push origin master -f
You can force update your master branch as follows:
git checkout upstreambranch
git branch master upstreambranch -f
git checkout master
git push origin master -f
For the ones who have problem to merge into main branch (Which is the new default one in Github) you can use the following:
git checkout master
git branch main master -f
git checkout main
git push origin main -f
The following command will force both branches to have the same history:
git branch [Branch1] [Branch2] -f
From the experiment branch
git rebase master
git push -f origin <experiment-branch>
This creates a common commit history to be able to compare both branches.
This looks like undesirable behavior on github's part, but it's fairly easy to fix. What you want to do is to rebase your branch on a reasonable (any reasonable) commit in the existing history. What you can do is to fetch the github repo and find which tree in its history is most similar to the one you started with. Start this way:
git remote add github u://r/l
git fetch github
myroot=`git rev-list master --max-parents=0`
root_tree=`git rev-parse $myroot^{tree}`
github_base=`git log --pretty=%H\ %T github/master | sed -n "s/$root_tree//p"`
With any luck, that will find you a commit in the github history that has the exact tree you started with. Assuming it does,
git rebase --onto $github_base $myroot master
and you're done.
If that doesn't find a matching tree, you get to find a nearest approximation. Here's one way to get a rough estimate of the differences:
git log --pretty='echo %H $(git diff-tree -p -b -U0 '$myroot:' %T|wc -l)' github/master \
| sh
which will count the lines in a minimized diff between the tree of each commit in the github/master history and your root tree. It seems reasonable to hope for a nice small difference, you could eyeball the actual diffs on it before calling that the github_base commit and doing the rebase above.
Terminology
First, let's get some terminology out of the way...
upstream <= The remote git repo (likely whose master or release branch is in production)
forked-repo <= The remote [experimental git repo] (https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) also known as "origin".
local repo <= The files and directories that you work with on your local workstaion, which you likely got by running a git clone my-forked-repo.git command
local index <= Also known as your local git "stage", i.e., where you stage your files before pushing them to you remote repo.
Github workflow process
Next, let's talk about the process of getting your changes to the upstream repo:
The process is generally to work on a feature branch and then push said branch, and open a Pull Request, either to your forked-repo's master branch or to the upstream's master branch
Create a feature branch by running git checkout -b FEATURE_BRANCH_NAME
Add/delete/modify files project files.
Add files by running git add .
Commit your files to your index by running git commit -m'My commit message'
Push your staged files by running git push origin FEATURE_BRANCH_NAME
Solution for entirely different commit histories
The master and upstreambranch are entirely different commit histories message can occur when you've forked a git repository and have changed your git history.
For example, if you fork a repo and pull your forked repo to work on it locally...
If then you decide to rewrite the entire application and then decide it's a good idea to deleting all existing files, including the forked-repo's .git directory. You add new files and directories to recreate your app and also recreate your .git directory with git init command.
Now, your application works great with your new files and you want to get it merged into the upstream repo. However, when you push your changes you get that "...entirely different commit histories..." error message.
You'll see that your original git commit will be different in your new local directory and if in your remote fork (as well as your upstream). Check this out by running this command in your current directory: git log --reverse master. Then running the following: pushd $(mktemp -d); git clone https://github.com/my-forking-username/my-forked-repo.git; git log --reverse master; popd
You must fix your local .git repo to match your remote my-forked-repo if you want to push your commits and subsequently perform a pull request (in hopes of merging your new updates to the upstream/master branch).
git clone https://github.com/my-forking-username/my-forked-repo.git
cd my-forked-repo
git checkout -b my-new-files-branch-name
# Delete all files and directories except for the .git directory
git add .
git commit -m'Remove old files'
# Copy your new files to this my-forked-repo directory
git add .
git commit -m'Add new files'
git push origin my-new-files-branch-name
Create a PR on GitHub and request to merge your my-new-files-branch-name branch in your my-forked-repo into master.
Note: The "...entirely different commit histories..." error message can also occur in non-forked repos for the same reasons and can be fixed with the same solution above.
A more simple approach where you can't mingle with the master.
Consider i have master and JIRA-1234 branch and when i am trying to merge JIRA-1234 to master i am getting the above issue so please follow below steps:-
From JIRA-1234 cut a branch JIRA-1234-rebase (Its a temp branch and can have any name. I have taken JIRA-1234-rebase to be meaningful.)
git checkout JIRA-1234
git checkout -b JIRA-1234-rebase
The above command will create a new branch JIRA-1234-rebase and will checkout it.
Now we will rebase our master.
git rebase master (This is executed in the same branch JIRA-1234-rebase)
You will see a window showing the commit history from first commit till the last commit on JIRA-1234-rebase. So if we have 98 commits then it will rebase them 1 by 1 and you will see something like 1/98.
Here we just need to pick the commit we want so if you want this commit then don't do anything and just HIT Esc then :q! and HIT ENTER.
There would be some changes in case of conflict and you need to resolve this conflict and then add the files by
git add <FILE_NAME>.
Now do git rebase continue it will take you to rebase 2/98 and similarly you have to go through all the 98 commits and resolve all of them and remeber we need to add the files in each commit.
Finally you can now push these commits and then raise Pull Request by
git push or git push origin JIRA-1234-rebase
This happened for me because I created a repo from GH, but then I also added a README. In doing so I created a commit on my remote.
Then I went and created a new repo locally, made some changes and committed. Then I pushed it to the 👆repo and tried to make a Pull Request.
But my remote's initial commit was different from my local's commit, hence this error message. GitHub itself even warns you against this:
Create a new repository on GitHub.com. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
GitHub Docs
Similarly if you're creating a new repo, GitHub will quietly suggest that you skip initializing the repo. Rather just define the repo.
tldr the very first commit has to be identical, you can't merge 2 commits that don't have an identical initial commit.
If you know from which commit issue started, you can reset your branch to that commit and then merge them.
this is 100% works in any situation :
1)create new folder in your machine
2)clone the remote repository to the new folder
3)delete all files and folders except for the .git folder
4)add your project files that you are working on to this new folder you created
5)open terminal
6)cd new_folder_path (path to the new folder you created)
warning : don't type > git init
7) > git add .
8) > git commit -m "write anything"
9) > git push URL(url of the remote repository)local_branch_name:remote_branch_name
This happened with me yesterday cause I downloaded the code from original repo and try to pushed it on my forked repo, spend so much time on searching for solving "Unable to push error" and pushed it forcefully.
Solution:
Simply Refork the repo by deleting previous one and clone the repo from forked repo to the new folder.
Replace the file with old one in new folder and push it to repo and do a new pull request.
I solved that problem. In my case when i did “git clone” in one directory of my choice without do “git init” inside of that repository. Then I moved in to the cloned repository, where already have a “.git” (is a git repository i.e. do not need a “git init”) and finally I started do my changes or anything.
It probably doesn’t solve the problem but shows you how to avoid it.
The command git clone should be a “cd” command imbued if no submodule exists.
I got this error when initializing a GitHub repository with a README file, and then trying to push my existing local git repository to it. This resulted in a main branch with the README file, which was the Default branch, and a master branch with my code, and they couldn't be merged.
But since I didn't actually have anything important in my main branch (if you need to keep the data from both your branches, check out PaianuVlad23's Answer instead), I managed to solve the problem by changing the Default branch to the master branch, and then delete the main branch, like this:
When in GitHub, click your user icon in the top right of the window.
Choose "Your repositories", and then click your repository name.
Under the repository name, choose the tab "Settings".
From the pane on the left, choose "Branches".
Under the headline "Default", change the default branch from the one you want to delete (in my case main) to the one you want to keep (in my case master).
Now, click the tab "Code" under the repo name.
Under the tab line containing "Code" etc, you'll see a place where it says "2 branches". Click it.
Find the branch you want to delete, and click the trash bin icon on the right on that line.
Now, your repository has only one branch, which is the one you want to push your local changes to! 🙂 Just as if you hadn't initiated your repository before pushing to it, as #mfaani's answer in this thread suggests you do it.
I found that none of the answers provided actually worked for me; what actually worked for me is to do:
git push --set-upstream origin *BRANCHNAME*
After creating a new branch, then it gets tracked properly. (I have Git 2.7.4)
I don't think we have same case here, but still someone else may find it helpful.
When similar error occurred to me, it was going to be the first merge and first commit. There was nothing in on-line repository.
Therefore, there was no code on git-hub, to compare with.
I simply deleted the empty repository and created new one with same name.
And then there was no error.
I got this error message, because I was migrating an application from SVN to GitHub and it's not enough to invoke a git init in the location of the source code checked out from SVN, but you need to invoke a git svn clone in order to have all the commit history.
This way the two source codes on GitHub will have a mutual history and I was able to open pull requests.
I had an issue where I was pushing to my remote repo from a local repo that didn't match up with history of remote. This is what worked for me.
I cloned my repo locally so I knew I was working with fresh copy of repo:
git clone Your_REPO_URL_HERE.git
Switch to the branch you are trying to get into the remote:
git checkout Your_BRANCH_NAME_HERE
Add the remote of the original:
git remote add upstream Your_REMOTE_REPO_URL_HERE.git
Do a git fetch and git pull:
git fetch --all
git pull upstream Your_BRANCH_NAME_HERE
If you have merge conflicts, resolve them with
git mergetool kdiff3
or other merge tool of your choice.
Once conflicts are resolved and saved. Commit and push changes.
Now go to the gitub.com repo of the original and attempt to create a pull request. You should have option to create pull request and not see the "Nothing to compare, branches are entirely different commit histories"
Note: You may need to choose compare across forks for your pull request.
Top guy is probably right that you downloaded instead of cloning the repo at start.
Here is a easy solution without getting too technical.
In a new editor window, clone your repo in another directory.
Make a new branch.
Then copy from your your edited editor window into your new repo by copy paste.
Make sure that all your edits are copied over by looking at your older github branch.
I had mine solved by overriding the branch:
My case: I wanted to override whatever code is in the develop with version_2.
delete the local copy of conflicting branch:
git checkout version_2
git branch -D develop
checkout a fresh branch from the version_2 and force push to git:
git checkout -b `develop`
git push origin `develop`
I didn't need to rebase. But in my case, I didn't need to take code from my old code.
first: pull from remote repo
merge or rebase
finally: push to remote repo
finish
When you are pull/merging feature to main and are in the main branch in the terminal, I successfully used 'git pull origin feature --allow-unrelated-histories'.
Before using this command, I had the same message about completely different commit histories, and I think it's because I accidentally pushed to main after committing to the feature branch. Then I tried some of the solutions offered here like rebase, which allowed me to merge my code, but I still had the compare and pull notifications through git, and it was a one time fix. By one time fix I mean I still got the different commit history message the next time I tried to merge a feature branch's code to main.
Another source from a google search offered the --allow-unrelated-histories fix, and it permanently works exactly how I wanted it to. The branches were merged and now I can merge without error messages and the compare and pull notifications work through git.
I'm sure there are consequences for people who didn't have the same problem as me, but I didn't lose any code and now my repo is clean. Also, I'm also an amateur coder and the question is older so maybe this command wasn't available when the question was asked or I'm not understanding the issue correctly.
I wanted to copy commit history of "master" branch & overwrite the commit history of "main" branch .
The steps are:-
git checkout master
git branch main master -f
git checkout main
git push
To delete master branch:-
a. Locally:-
git checkout main
git branch -d master
b. Globally:-
git push origin --delete master

delete branch and messed up backup..need to recover or jump off bridge [duplicate]

I just deleted the wrong branch with some experimental changes I need with git branch -D branchName.
How do I recover the branch?
You can use git reflog to find the SHA1 of the last commit of the branch. From that point, you can recreate a branch using
git branch branchName <sha1>
Edit: As #seagullJS says, the branch -D command tells you the sha1, so if you haven't closed the terminal yet it becomes real easy. For example this deletes and then immediately restores a branch named master2:
user#MY-PC /C/MyRepo (master)
$ git branch -D master2
Deleted branch master2 (was 130d7ba). <-- This is the SHA1 we need to restore it!
user#MY-PC /C/MyRepo (master)
$ git branch master2 130d7ba
If you know the last SHA1 of the branch, you can try
git branch branchName <SHA1>
You can find the SHA1 using git reflog, described in the solution --defect link--.
If you just deleted the branch, you will see something like this in your terminal:
Deleted branch branch_name(was e562d13)
where e562d13 is a unique ID (a.k.a. the "SHA" or "hash"), with this you can restore the deleted branch.
To restore the branch, use:
git checkout -b <branch_name> <sha>
for example:
git checkout -b branch_name e562d13
If you haven't push the deletion yet, you can simply do :
$ git checkout deletedBranchName
Follow these Steps:
1: Enter:
git reflog show
This will display all the Commit history, you need to select the sha-1 that has the last commit that you want to get back
2: create a branch name with the Sha-1 ID you selected eg: 8c87714
git branch your-branch-name 8c87714
First: back up your entire directory, including the .git directory.
Second: You can use git fsck --lost-found to obtain the ID of the lost commits.
Third: rebase or merge onto the lost commit.
Fourth: Always think twice before using -D or --force with git :)
You could also read this good discussion of how to recover from this kind of error.
EDIT: By the way, don't run git gc (or allow it to run by itself - i.e. don't run git fetch or anything similar) or you may lose your commits for ever.
If you deleted a branch via Source Tree, you could easily find the SHA1 of the deleted branch by going to View -> Show Command History.
It should have the next format:
Deleting branch ...
...
Deleted branch %NAME% (was %SHA1%)
...
Then just follow the original answer.
git branch branchName <sha1>
Thanks, this worked.
git branch new_branch_name sha1
git checkout new_branch_name
//can see my old checked in files in my old branch
This worked for me:
git fsck --full --no-reflogs --unreachable --lost-found
git show d6e883ff45be514397dcb641c5a914f40b938c86
git branch helpme 15e521b0f716269718bb4e4edc81442a6c11c139
if you deleted a branch using GUI of a Jetbrains IDE(Goland, phpstorm etc)
go to
git windows(left-down corner of IDE) -> console tab -> now you can see log of executed commands by IDE and find the branch name and SHA1 from this log
If you are using IntelliJ IDEA, in Event Log you'll see something like that:
And may simply restore your branch.
First of all, don't get panic. You are in the right place.
Go on champ, we all make mistakes! That's how we learn!
I wish you health, happiness and success!
Oh for the answer! I think you already figured out!
If not! here is the answer.
use git reflog
git checkout branch branch_name commitsha
For more clarification, in the second command branch_name is the name you want to give your branch. commitsha is sha number you want to check out. which you will get from git reflog command.
Once again Happy coding!

Revert everything you did in github

So, here is a situation. I wrote a lot of code and by the end of the day wanted to commit all that.
The branch situation was such:
master
working_branch*
What I did after that was:
git branch 11_11_2018
git add everything
git commit -m "msg"
git push --set-upstream origin/11_11_2018 (sth like that)
git checkout master
git branch -d 11_11_2018
Now what happened is that I deleted everything I worked on. I'm somewhat new to GitHub and am really afraid to lose all those precious code lines. The problem here was that I meant to checkout 11_11_2018 but didn't and now I don't completely understand what happened. Please, explain what I actually did and how to revert it if possible?
Run the following command and you should get back all your code:
git checkout -b 11_11_2018 origin/11_11_2018
What you did was pushed all your code to remote branch, switched to master, deleted local branch.
The above command will switch to your remote branch and fetch all your changes with it

Move a specific branch to new repository

I have a mercurial repository in which I had created a branch 7-8 months back. And now this branch is the one in which I do most of the development and I don't have anything fruitful in default branch and other branches that I have.
I want to create a new repository that represent only this branch. i.e. I want to move this branch to a new repository with history.
I tried to use HG convert tool with following syntax:
hg convert --filemap ~filemap.txt --branchmap branchmap.txt --source-type hg --dest-type hg "E:\MyStuff\Dev\MyOldRepo" "E:\NewRepo"
File map I have defined all my file that I want to include. In branchmap file i had defined
MyOldNamedBranch default
Convert tool do rename MyOldNamedBranch to default but it also brings the changesets from other branch that I don't need.
I also tried to set the following in setting file but no results:
[convert]
hg.usebranchnames=0
hg.startrev=5262
Please suggest how I can move a branch to new repository with history and leaving other branches behind.
I have set the start revision number in command only and it worked.
hg convert --config convert.hg.startrev=5262 --branchmap branchmap.txt "E:\MyStuff\Dev\MyOldRepo" "E:\NewRepo"
And it worked like a charm.
Try this:
Clone only the branch you need:
hg clone E:\MyStuff\Dev\MyOldRepo -b MyOldNamedBranch .\NewRepo
Then inside the NewRepo, convert all the changesets to the draft phase:
hg phase -r 0 -d -f
Then update to the patent of MyOldBranch (I assume, that the parent is in the default branch)
hg update -r "parents(min(branch(MyOldBranch)))"
Then rebase MyOldBranch on the exactly the same changeset.
hg rebase -s "min(branch(MyOldBranch))" -d .
Do exactly the same with the rest of the branches.
To be honest I'm not sure if this is the best method but it worked for me.

Reverting to last commit in Xcode 4 using Git

We just moved over to Git from SVN. In trying to clean up some unused files. I saved before deleting one folder that I thought we weren't using. I did not push this to the origin. I realized we are using one of the files in the folder after all, and would like to revert to my last commit. This is on my own branch from the master. I can't find a way to do that in Xcode. Am I missing something? Thanks.
You can see here for rsanchezsaez answer: Xcode 4 git integration
Xcode 4 won't let you to checkout older commits within the user interface, unless you created a new branch for that commit. Nevertheless, you can do it from the command line. For that, use the following command from your project folder
$ git log --format=oneline
to get the hash code of the commit you want to go to, and then use:
$ git checkout desired-hash-code
to checkout that particular version. Once there, you can make tests, changes, and possibly create a new branch. If you do a commit without creating a new branch, you will lose the newer commits in your current branch. If you want to go back to the newest commit after having performed some tests on your older version use:
$ git checkout master
note again that this won't work if you do a new commit from your old code version without creating a new branch, because newer commits in the current branch get dereferenced.
Also, please consider searching SO before asking. Many questions had already been asked and answered.
From the command line, run a program called gitk - this will allow you to visualize the commits you currently have. Find the ID of the commit you want (e.g. the previous commit) and do the following on your branch:
git tag JustInCase
git reset --hard <commit ID>
Refresh gitk, and if you're happy with the results then delete the tag using:
git tag -d JustInCase
If you're not happy with it, just do:
git reset --hard JustInCase
git tag -d JustInCase
To visualize this for you:
1) Start
2) After tagging and resetting your branch to the previous commit.
3) After deleting the tag and doing Reload in gitk.