How to pull from specific branch in git- understanding gap - gitlab

I am using gitlab and really confused in few things. :
When we create new branch by git checkout -b test. Does it create copy of master or it creates copy of branch i am currently in?
For example: I am currently at branch dev, then i write command git checkout -b test. So it will be copy of dev, not masters?
Pull : when we write git pull , it pulls changes of current branch from remote branch of same name. Its used when more people are working on same project.
Example : I am at branch dev, i write git pull, it updates my local as of dev in remote. Now i created a new branch test, checkout test branch and do git pull. It gives me :There is no tracking information for the current branch.
Please specify which branch you want to rebase against.
its because there is no test branch in remote ?
What command to be used if i want to pull from dev branch while my current branch is test? is it git pull --rebase dev test?
When we write git push, it pushes current branch to remote one.
example : i am on branch test, i add, commit and write git push. It simply pushes my branch test to remote with same name as test.
Can we push to specific branch like push test to dev?
What is difference in following considering i am at branch test:
git push
git push origin test
they both push to remote?
My requirement is : there is branch dev which is not master branch, i am supposed to work on this branch as starting and end point. Like, new branch should be copy of this and i am supposed to push to same branch.

Does it create copy of master or it creates copy of branch i am currently in?
It creates a new branch named test based on the current branch, whatever that might be, as a starting point.
What command to be used if i want to pull from dev branch while my current branch is test?
I believe you can pull any branch you wish into your current branch. E.g. if you were on branch test and wanted to pull dev you could just use:
git pull origin dev
Can we push to specific branch like push test to dev?
Yes, can specify both the local and remote branch names when pushing, e.g.
git push origin test:dev

1- When you do git checkout -b test it creates copy of your current branch(in this case 'dev').
2- git pull will only sync your changes between remote and local. If you upload the branch and try to pull, it does not work because your local and remote changes will be synchronized.
3- This could help you : Make an existing Git branch track a remote branch?.
If you want to work in a copy of a branch, you should do this:
git checkout <origin_branch> (master, dev, what u want )
git checkout -b <work_branch> (test, for example. This create a copy of your origin branch)
After this, you have a new branch 'test' in your local repository. If you want to push this branch on the repo:
git add .
git commit -m "Pushing new branch test"
git --set-upstream origin <your_new_branch>

Related

remote: Pushed to branch other than [main, master], skipping build [duplicate]

I have a project hosted on Heroku and it's gotten to the point where I want to make an alternate test server (so I can test Heroku workers without messing up production).
I have already set up my main Heroku remote running my trunk and a Heroku-dev remote on which I wish to run an alternate branch.
My problem is that since my alternate branch isn't master, Heroku won't build it.
$ git push heroku-dev test
counting objects ...
...
Pushed to non-master branch, skipping build.
To git#heroku.com:example-dev.git
* [new branch] test -> test
Switching this build to master is not an option at the moment. Obviously one option is to create a whole new git repo that's a clone of my test branch, but that doesn't sound very ideal.
You can push an alternative branch to Heroku using Git.
git push heroku-dev test:master
This pushes your local test branch to the remote's master branch (on Heroku).
Comment from #Brian Armstrong:
Worth noting also, when you're ready to go back to master you need to do
git push -f heroku master:master
In my case, the default or base branch was develop, so i used:
git push heroku develop:master
In case git push heroku-dev test:master doesn't work for you, try git push heroku test:master.
Remember the "test" in "test:master" is the name of the new branch you are on.
You will need to pull the remote branch first before you can push the non master branch.
Run following command in you local repository
git pull https://heroku:YOUR_HEROKU_API_KEY#git.heroku.com/YOUR_APP_NAME.git

Sourcetree existing repo has no brances and cannot create one

I have an existing repo which I connected to my gitlab via ssh
I am just trying to create my branch in sourcetree so that i push my changes there but in getting an error "not a valid object name master"
is there any part that i skipped because my created repository cant create new branches? thanks for reading any help will be great.
TLDR: created and linked via gitlab repo in sourcetree with an existing one, cant create branch to push
Check first if you are creating your branch from an empty repository: there should be at least one commit.
If the repository is not empty, check if:
your remote GitLab repository expects a default branch 'main'
your local repository uses master as its main branch
If it is the case, rename your local branch
cd /path/to/local/repository
git branch -m master main
And make sure any future local repository starts with main:
git config --global init.defaultbranch main
Try again your branch creation in SourceTree.

remote git repo cannot be auto checkout after a merge hook

I have a remote non-bare git repo, which is my static blog site.
I push a commit in local master branch to the remote master branch every time, and there is a post-update hook, which is like below:
echo "hook..."
git merge master
echo "after hook..."
In the remote repo, there is a pages branch which is checked out by default, what I want is that pages branch keep update of the master branch and auto checkout to workspace.
However, workspace cannot be checkout automatically, but the two branch has been updated.
In sum, What I want is the workspace can update after the pages branch merged every time. So that my website can update too.
==========detail process===========
==local repo:
*master
==remote repo:
*pages
master
git push local-master ---> remote-master [OK]
trigger hook do : git merge master to pages branch (which is checked out) [OK]
workspace updated (this is what I want) [NOT-OK]
Considering the current working directory of such an hook is the .git/ folder itself (ie, GIT_DIR is set to '.', which in non-bare repo is .git/), try a .git/hooks/post-receive with:
unset GIT_DIR
cd ..
git merge master

Git push to local repo says "everything up to date", it isn't

I have a theme I use for our merchant store. I use git to maintain a repo (local repo 1) of this theme that i clone into a dev folder (local repo 2) and then work on there, when I'm done I want to update (push?) to the original local repo 1. From there I can render zip files or whatever I need for the merchant store.
So i made repo1 by git init and adding the files and committing it. worked fine. Then I cloned the repo to my dev folder and setup my web services there. Worked great. I edited my theme and made commits appropriately. Now that I am ready to put these changes on the live server I want to push to my origin which should be repo 1. However after making commits when I try to push from repo2 by
git checkout master
and then
git merge classes-migration
and then
git push
it says "everything up to date" I've tried specifying specifically the same branch to push, honestly I've tried all kinds of things reading through the different answers here.
git branch [for repo1]
classes-migration
import-classes-migration
initial-commit
* master
git status [for repo1]
On branch master
nothing to commit, working directory clean
git branch [for repo2]
classes-migration
* master
git status [for repo2]
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
git remote show origin [for repo2]
* remote origin
Fetch URL: /home/user/projects/merchant/repos/theme
Push URL: /home/user/projects/merchant/repos/theme
HEAD branch: master
Remote branches:
classes-migration tracked
import-classes-migration new (next fetch will store in remotes/origin)
initial-commit tracked
master tracked
Local branches configured for 'git pull':
classes-migration merges with remote classes-migration
master merges with remote master
Local refs configured for 'git push':
classes-migration pushes to classes-migration (up to date)
master pushes to master (up to date)
So.. yeah.
The push command is used to put in the server what you already has committed.
If you have a git repository configured, clone it in your dev machine, then work in this project. After that you need to commit your changes.
first check the status after the changes:
git status
If you get this message
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: filename.txt
no changes added to commit (use "git add" and/or "git commit -a")
It means you`ve changed the file filename.txt, if you've changed more then one file, every file will be listed here.
The next step is commit the file.
git commit filename.txt -m "commit comments"
Just after that, you push to the server:
git push
After this command, when you clone in another machine or update the repository the user will see the modifications.

how to change branch on --bare remote from local in git

I am trying to make a hook on a remote --bare repository that will copy the source code I send from a local git, in different folders according the branch I'm sending to. I have 3 branches on local: master, development and release so I wish that on the remote would be 3 folders containing the source code of each branch. I found that using:
git rev-parse --abbrev-ref HEAD
in combination with a series of if conditions could do the trick. The only problem is that the HEAD remains the same on remote for whatever branch you send to. Is there any code that could be used in the hook, so it would know that I am sending to a certain branch? Or is there any other method for doing this? Thanks!
Your one bare repo will have all 3 branches on it. You can use git modifiers like this to make 3 separate directories update to what each branch contains:
git --work-tree=/some/project/dir/branch1 --git-dir=/path/to/bare/repo checkout branch1 -- .
To avoid specifying those options, you can set their corresponding environment variables:
GIT_WORK_TREE
GIT_DIR
This way you can keep one bare repo and 3 separate directories that update when branches are pushed. Remember what the arguments are that are provided in your hook. The first is the branch name.

Resources