remote git repo cannot be auto checkout after a merge hook - linux

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

Related

"error: pathspec 'master' did not match any file(s) known to git" when trying to switch to master branch

I'm getting the following error message when trying to switch to the master branch.
[root#testlab testing_repo]# git checkout master
error: pathspec 'master' did not match any file(s) known to git
Here are the steps I followed:
I created a master branch with name "testing_repo"
and converted this directory to repository with git init testing_repo.
I created 3 files using touch file1 file2 file3
in testing_repo.
[root#testlab testing_repo]# ls
file1 file2 file3
these 3 files I have staged.
I created a new branch with git checkout -b feat/add_git_tutorial.
I added a new file git_tut.txt which i
staged and commited using git add and git commit -m "message".
[root#testlab testing_repo]# ls
file1 file2 file3 git_tut.txt
I tried to switch to master branch using git checkout master
but I get an error:
[root#testlab testing_repo]# git checkout master
error: pathspec 'master' did not match any file(s) known to git
You do not have a branch named "master"
The error
error: pathspec 'master' did not match any file(s) known to git
is exactly the error you get if you try to check out a commit reference that does not exist, be it a branch name, tag name or commit hash. The wording of the message is misleading to people not familiar with the inner workings of git.
To see the list of branches in your repo:
git branch
I'll bet you won't see "master" in the output. There are two possibilities for why you don't have a "master" branch.
Possibility #1: You switched branches before your first commit.
The default branch (usually "master" or "main", see next section) isn't created when you init the repo, but when you make your initial commit. If you switch branches before that initial commit, the default branch ("master") is never created.
In your step 2 you created and staged some files. But then in your step 3 you switched branches before every committing those staged changes. So the default branch (e.g. "master") was not created. If you redo your above steps, but do a git commit before step 3, you should get a message that that files were committed to "master" (assuming that's your default branch, see below).
Possibility #2: Your git is configured to use another default branch name for new repos.
It is possible your git installation is configured to initialize new repos with "main" or some other name instead of "master".
ℹ️ If you want to understand the background behind switching from "master" to "main", read Regarding Git and Branch Naming/
You can check what your installation's "default default" branch with:
git var GIT_DEFAULT_BRANCH
If you want to change the "default default" branch name for new repos, the easiest way is with:
git config --global init.defaultBranch <your_name_pref_here>
If you want to rename the default branch of an existing repo, say from "main" to "master":
git branch -m main master

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.

How to pull from specific branch in git- understanding gap

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>

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