Git: click on another remote branch

Problem:

You must redirect the changes from the local git branch to another remote branch of the git repository, and these changes transferred to the branch will be compared with the wizard existing in the remote URL, and the changes will be merged.

Actions

I still created a local git repository,

Initialized a simple local git repository using below, using the commands as shown below

git init 

Existing files are added to the repo and added to the staging area using the following command:

 MacBook-Pro: $ git add *.h MacBook-Pro: $ git add *.m 

Checked status using the command below,

 MacBook-Pro: $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: test.h # new file: test.m # 

Consider

 git commit -m"Added test base files" 

Now created a new branch named issue_fix,

  MacBook-Pro:$ git branch issue_fix 

Work on the branch will begin by checking the branch.

 MacBook-Pro: $ git checkout issue_fix 

A few commits were made to the branch. Everything was OK.

Now I’m in a situation where I need to push the changes that I made to the "issue_fix" branch to the URL of the remote repository, for example this

  https://github.com/myaccountname/project.git 

My changes will be redirected to the branch name indicated to me, or if the branch was not available, I need to create a remote branch and push my changes to the local branch on it.

The most important shifted changes will be compared with the master in the specified repository URL, and if everything goes well, it will be merged with the master. Therefore, I will always redirect my changes only from local to remote.

The problem arose because the Clone URL was not specified when I started from this, only the source was provided, so I created a local git repository and started working on it, now the repository URL was specified and asked to click mine changes to the branch.

I would like to know if this is possible in the first case? If possible, give me the commands I need to give in order to make it work.

+6
source share
3 answers

To fix this fiasco (I had these problems before), they are very unpleasant), you can try to do the following:

Your situation

Primary repository

 ABCDEFGH 

Your repository

  A'-B'-C'-D' 

If the code in G is equal to the code in local A' , but they do not have the same history

Clone the repository you want to push changes to

 git clone https://.... 

This will provide you with a working copy of the code and, more importantly, also with its history.

Clone of the main repository

 ABCDEFGH 

in a different folder than your repo

  A'-B'-C'-D' 

Retrieve your function branch from the damaged repository.

 git fetch ../messed-up-repo 

Where ../messed-up-repo is the path to your second repository.

This will result in the loss of all changes from another repo without merging them. The reason for this is because your branch will not find a common ancestor with the main branch, so it will be difficult or perhaps even impossible to combine them.

The main repository will now look like this:

  ABCDEFGH # master / \ ------------A'-B'-C'-D' # messy 

Create a new branch

 git branch new-featurebranch aabbccdd 

Where aabbccdd is the commit identifier when loading the repository. This will create a new branch on commit, which you downloaded then. The better you can guess this value, the less conflicts you will have later.

  - # new branch / ABCDEFGH # master / \ ------------A'-B'-C'-D' # messy 

Re-format the branch interactively on it

 git checkout messy-featurebranch git rebase -i new-featurebranch 

This will show the editor with a list of all the commits in your branch. The first one will probably be "I downloaded a copy of the code, but did not clone it correctly." REMOVE THAT LINE . Then all other commits should be applied without problems.

  -B'-C'-D' # new branch / ABCDEFGH # master / \ ------------A' # messy 
+4
source

Git is distributed and runs locally for most of its operations; it doesn't matter if remote access exists when you make your branches or comment locally. It just has to exist when you click.

So, you can add a remote repository, and then click your branch on it. The branch will be created remotely if it does not exist.

 git remote add github-repo https://github.com/myaccountname/project.git git push github-repo issue_fix 
+3
source

This seems to be a really small edition, I suggest just cloning the main repo (in a new directory) and then overwriting your files with your own and committing new commits.

Otherwise, there is no “easy way” to do this, but perhaps some tips:

  • clone the main repo into a new directory https://github.com/myaccountname/project.git
  • add your old working copy as remote: git remote add old /path/to/old/repo/
  • git fetch will import all your changes.

Now there are several options, depending on what is best for you: git cherry-pick <1st sha1 you want>..<last commit (probably old/issue_fix)> seems appropriate for your needs, but if it is not, you you can try git rebase --interactive Nils, but you may have a problem if the first commit you made did not have an empty commit.

0
source

Source: https://habr.com/ru/post/949004/


All Articles