How do I fetch all Git branches?
2022-01-27
Question I cloned a Git repository containing many branches. However, git branch only shows one: $ git branch * master How would I pull all the branches locally so when I do git branch, it shows the following? $ git branch * master * staging * etc... Answer TL;DR answer git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done git fetch --all git pull --all (It seems that pull fetches all branches from all remotes, but I always fetch first just to be sure.…
Pushing to Git returning Error Code 403 fatal: HTTP request failed
2022-01-27
Question I was able to clone a copy of this repo over HTTPS authenticated. I've made some commits and want to push back out to the GitHub server. Using Cygwin on Windows 7 x64. C:\cygwin\home\XPherior\Code\lunch_call>git push Password: error: The requested URL returned error: 403 while accessing https://MichaelDrog alis@github.com/derekerdmann/lunch_call.git/info/refs fatal: HTTP request failed Also set it up with verbose mode. I'm still pretty baffled. C:\cygwin\home\XPherior\Code\lunch_call>set GIT_CURL_VERBOSE=1 C:\cygwin\home\XPherior\Code\lunch_call>git push Password: Couldn’t find host github.…
Git push rejected after feature branch rebase
2022-01-26
Question OK, I thought this was a simple git scenario, what am I missing? I have a master branch and a feature branch. I do some work on master, some on feature, and then some more on master. I end up with something like this (lexicographic order implies the order of commits): A--B--C------F--G (master) \ D--E (feature) I have no problem to git push origin master to keep the remote master updated, nor with git push origin feature (when on feature) to maintain a remote backup for my feature work.…
Resolve Git merge conflicts in favor of their changes during a pull
2022-01-25
Question How do I resolve a git merge conflict in favor of pulled changes? I want to remove all conflicting changes from a working tree without having to go through all of the conflicts with git mergetool, while keeping all conflict-free changes. Preferably, I want to do this while pulling, not afterwards. Answer git pull -s recursive -X theirs <remoterepo or other repo> Or, simply, for the default repository: git pull -X theirs If you're already in conflicted state.…
Given a commit id, how to determine if current branch contains the commit?
2022-01-24
Question What I'm trying to do is a version check. I want to ensure the code stays on top of a minimum version. So I need a way to know if the current branch contains a specified commit. Answer There are multiple ways to achieve this result. First naive option is to use git log and search for a specific commit using grep, but that is not always precise git log | grep <commit_id> You are better off to use git branch directly to find all branches containing given COMMIT_ID using git branch --contains $COMMIT_ID The next step is finding out current branch which can be done since git 1.…
How to resolve git's not something we can merge error
2022-01-24
Question I just encountered a problem when merging a branch into master in git. First, I got the branch name by running git ls-remote. Let's call that branch "branch-name". I then ran git merge branch-name command and got the following result: fatal: branch-name - not something we can merge How do I resolve this error? Answer As shown in How does "not something we can merge" arise?, this error can arise from a typo in the branch name because you are trying to pull a branch that doesn't exist.…
How to un-submodule a Git submodule?
2022-01-24
Question What are the best practices for un-submoduling a Git submodule, bringing all the code back into the core repository? Answer If all you want is to put your submodule code into the main repository, you just need to remove the submodule and re-add the files into the main repo: git rm --cached submodule_path # delete reference to submodule HEAD (no trailing slash) git rm .gitmodules # if you have more than one submodules, # you need to edit this file instead of deleting!…