GitHub - How to copy from one branch to another - bash

I git cloned one GitHub repo and checked out one branch.
Then made changes to several files.
How to create another branch and push all changes to a new branch (create new branch and push changes to it using terminal commands)?

Assuming you haven't committed anything yet, you can just use checkout -b to create a new branch, then commit your changes:
$ git clone ...
$ git checkout original-branch
(Make changes)
$ git checkout -b new-branch
$ git commit -a -m "Message for changes"
You talked about "pushing changes" to a new branch - the above would commit the changes in the new branch to the local repo. If you wanted to then push that branch back to GitHub, you'd want something like:
# Here origin is the name of the remote to push to, and new-branch is the branch
$ git push origin new-branch
You'd probably want to make sure you were pushing to your fork rather than to the original repo. You can configure each branch to default to a particular remote and branch, and there are various ways of configuring the default push behavior for new branches too via the push.default configuration option. See the git push docs for more details.

Related

Sync Git Branch to a specific folder

Suppose I have a git repo named waterkingdom It has lot of branches. We will be working with a specific branch called wave-pool.
wave-pool branch has files & folders such as
cost.txt
ride.txt
rules.txt
code/
code/ride.py
code/boom/crash.py
We have another folder which is not a part of the repo named wave-pool-boom
How can I only sync the branch wave-pool from waterkingdom repo to the folder called wave-pool-boom after the commit without knowing the latest commit hash?
Everything is locally on Linux.
How can I only sync the branch wave-pool from waterkingdom repo to the folder called wave-pool-boom after the commit without knowing the latest commit hash??
Everything is locally on LINUX.
Pushing branches from one repo to another is easily done in git, and makes a lot of sense the more you work with git.
Clone waterkingdom into a new directory
git clone --single-branch -b wave-pool /path/to/waterkingdom ~/projects/waterkingdom
cd ~/projects/waterkingdom
Setup a new remote
git remote add r-wave-pool-boom /path/to/wave-pool-boom
Push the branch to the remote (but do not change its tracked remote branch)
git push r-wave-pool-boom wave-pool
Remove the remote (optional)
git remote remove r-wave-pool-boom
Tools to help you further your Git knowledge
git branch -avv
Gives a listing of all branches and what remote branch (if any) they track, what the latest commit hash/message is, and the state (behind, ahead) of each branch.
git remote -v
Give a listing of all the remotes (if any) and their URL's configured for your local repo.
Further comments
Why "without knowing the latest commit hash"? The latest commit hash is always HEAD or <branch-name> or refs/remotes/origin/<branch-name>.

What is the best way to mirror a repo and fetch updates on a daily basis?

I don't know if this is the right way to accomplish what I need and maybe has also been asked a lot, although I couldn't find a solution.
I have mirrored a repository into my Gitlab account via
git clone --mirror https://github.com/some/repo
git remote rename origin upstream
git remote add origin git#gitlab.com:user/my-repo.git
git push origin --all
Now I want to frequently get the changes from the upstream repository without using git pull or git merge, because I want the commits to be identical to the upstream branch and not "Merging upstream into origin..." commits. From what I understand, you need to
cd my-repo
git remote add https://github.com/some/repo
git checkout branch_name
git pull --rebase upstream branch_name <-- pulls the changes without merging them
git rebase <-- rebases to the fetched changes so my own changes will stay on top
This works if I do it manually for the branch, but if I want to iterate through all branches with. Please correct me if I am approaching this the wrong way!
for branch in $(git for-each-ref --format='%(refname)' refs/remotes/origin); do branch=${branch##*/}; echo "\nUpdating $branch..."; git checkout $branch; git pull --rebase upstream $branch; git rebase; done
it doesn't work and the local branches stay untouched.
I know that Gitlab provides its own mirroring tool, but it is not activated in our company.
This seems to me to be a misunderstanding of what a branch is and what it's for. You should not have any local branches corresponding to the upstream. Just git fetch and stop; you are now mirrored and up to date.

Edit and create more text files in git with command line [duplicate]

I am working on a git repository with a master branch and another the topic branch. I have switched to topic branch and modified a file. Now, if I switched to the master branch, that same file is shown as modified.
For example:
git status in git-build branch:
# On branch git-build
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: cvsup_current
#
Switch to master branch
[root#redbull builder_scripts (git-build)]# git co master
M builder_scripts/cvsup_current
Switched to branch "master"
git status in master branch
[root#redbull builder_scripts (master)]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: cvsup_current
#
Why is that the file is shown as modified in the master branch even though it was modified in git-build branch?
My understanding was that the branches are independent of each other and when I change from one branch to another the changes do not "spill over" from one branch to another. So I am obviously missing something here.
Has anyone got a clue stick?
Why is that the file is shown as modified in master branch even though it was modified in git-build branch?
The key to remember is that the file was not modified in the git-build branch. It was only modified in your working copy.
Only when you commit are the changes put back into whichever branch you have checked out
If you want to temporarily store your changes to one branch while you go off to do work on another, you can use the git stash command. It's one of the amazing little unsung perks of using git. Example workflow:
git stash #work saved
git checkout master
#edit files
git commit
git checkout git-build
git stash apply #restore earlier work
git stash stores a stack of changes, so you can safely store multiple checkpoints. You can also give them names/descriptions. Full usage info here.
This is the default behaviour of git.
You can use -f flag to checkout to do "clean checkout" if you like.
The modified files are not put in the repository until you add and commit them. If you switch back to your topic branch and commit the file, then it won't appear on the master branch.
It is not like git branches are dependent on each other but also they
do not have a complete code base separately for each branch either.
For each commit, Git stores an object that contains a pointer to the
changes. So each branch points to its own latest commit and HEAD points to the branch currently you are in.
When you switch the branch, the HEAD pointer points to that
particular commit of the branch. So if there are modified files, the
default behavior is to copy over them.
You can do the following things to overcome this issue.
Use -f option to ignore the changes.
If you want to save the changes:
Commit the changes locally in the same branch and then switch the
branch.
Use git stash, switch the branch, do your work, switch back to the
original branch and do git stash apply.
In my experience this "spillover" problem arises because git does not protect "untracked" files from git checkout (only tracked but uncommitted files would be protected, i.e. user would be forced to git commit them before a git checkout is allowed to another branch).
If you switch back to the original branch that created these files (and "untracks" them), these files become "untracked" again and can be git add'ed or git rm'ed.
For me it looks like a bug: even "untracked" files should be protected from git checkout.

Reinitializing a git repository

I have an xcode project on my desktop in a directory that originally had a git repository with a tracking branch that tracked a remote branch on github. The remote branch has some 84 commits and is 2 commits ahead of the master branch of the project I'm adding features to. I changed the name of the folder/directory on my desktop. I'm not 100% sure if this is the reason why but when I go to git status I get: fatal: Not a git repository (or any of the parent directories): .git. My plan is to simply git Init, add the remote branch and create a new tracking branch and than commit locally to that branch and than push to the remote branch. However, I'm a git beginner and I'm not sure if this is the proper way to go about it. I'm very weary of losing any commit history or accidentally breaking something. Is the method I outlined a good way of rectifying this loss of the git repo?
If you have all your code updated in remote repo then your local .git is deleted. You don't need to re-init your local repo. Rather just clone it.
git clone remote_repo
If you don't have any commit in remote repo, Simply follow
git init
git add all_local_files
If you have updated remote repo than some commits made in local but not pushed and you lost .git. Simply clone remote. Add all files in a single commit
git clone
git add all_local_files
Looks like you messed up your git repository, but not the code / contents.
One way to restore and keep local changes ( if any ) would be:
Clone another copy of your repo from github.
Copy all modified files to the new repo, omitting removed ones: rsync -duztv /old/local/repo/ /new/local/repo
git status to see what the situation is.

How to Set Up Xcode with Remote Git Repository

so I just installed a new remote git repository on one of our servers and want to move our old projects there. Our existing projects have local git repositories, and we can add repos from the server, but how do we move our existing projects onto the server?
Any ideas?
You would do these steps:
Make the individual repositories on the server.
git clone --bare nameofrepo
On the actual repo, add the remote to the repository from which you want to send up work:
git remote add origin <url to your remote>
Now push your branch for your work:
git push origin master
Repeat for any other branches you want to have pushed to the central repo.
The URL in the first command can also be a regular file path. Most solutions, however are through an SSH connection.
After creating your Xcode project, cd to that directory
and make sure you are on the master branch
Type:
git remote add origin <URL-of-your-GitHub-repository>
git pull
git branch --set-upstream-to=origin/master master
git merge
git commit -m "Merging with remoteā€
git push
Now your new project has been pushed to the remote GitHub directory.

Resources