0

I'm a web developer currently using FTP to deploy my code and trying to move to git to help speed things up.

My current projects look like:

  • webapp1_live
  • webapp1_dev

  • webapp2_live

  • webapp2_dev

Both live and dev files are stored locally (complete source) because:

  • I need the remote server to test my code (I have an auto upload to FTP when saving local files in Sublime Text)
  • I use a diff tool to figure out which files I have modified on the dev and copy them to the live directory (manually) and then I have to manually FTP those on the live server

I would like to use git to keep track of my changes BUT also to quickly copy my changes from the dev to the live directory (I'll figure out later how to FTP those to my remote server - I've read something about hooks and such... that's a different story, I don't want to get into that now)

I have tried Sublime Merge to do what I want and I set up the dev and live as local repositories but when I try to push my changes from the dev to live, I get this error:

remote: error: refusing to update checked out branch: refs/heads/master        
remote: error: By default, updating the current branch in a non-bare repository        
remote: is denied, because it will make the index and work tree inconsistent        
remote: with what you pushed, and will require 'git reset --hard' to match        
remote: the work tree to HEAD.

I have read about the bare repository but as I understand, that doesn't have a working directory so that means it doesn't have a copy of the files?!

teomor
  • 144
  • 1
  • 8

1 Answers1

0

This could easily be done within git by using different branches.

Your current project would change to only:

  • webapp1
  • webapp2

In fact i'd seperate them into two repositories. Then as to branches, git uses by default the so called master-branch. This branch can be interpreted as the production environment meaning that all code on this branch should be working and be stable (your live folders)

Then i'd create a second branch based from master called develop. This is merely a copy of the master branch and includes every new feature you want to add. You could create feature/hotfix branches which then can be merged into develop which all can be merged after review to the master branch.

As for git there are some commands to help you do this:

  • git init - This will initialize a git repository with a default master branch.
  • git checkout -b develop - This will create a develop branch from the current branch.
  • git checkout master - This will change your local branch to the master branch.
  • git merge develop - While on master will merge all changes from develop into the master branch.

Setup your current project in git

  1. Open up a git terminal and cd into webapp1 directory.
  2. Initialize the git repository with git init .
  3. Run git status to ensure you're on the master branch.
  4. Run git push -u origin master to push your master branch to the remote repository.
  5. Run git checkout -b develop to create a develop branch from the current master branch.
  6. Run git push -u origin develop to push your master branch to the remote repository.

Now on the remote you have 2 branches holding your live environment and your development environment.

to make changes

  1. Switch to develop branch (git checkout develop)
  2. Pull the latest version from remote develop: git pull
  3. Make your changes
  4. Add files and changes to the index: git add -A
  5. Commit your changes to the index: git commit -am "your change message"
  6. Push your changes to the remote develop branch: git push
  7. Once all is alright and ready for the live environment switch to the master branch: git checkout master
  8. Get the latest changes from the remote master: git pull
  9. Merge your develop branch into the master branch: git merge develop
  10. Push your merge to the remote master branch: git push
Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • It looks like I found an easier solution (for now anyway): "Just to make this clearer, in the repo that's the target of the push: git checkout -b tmp. Then in the source repo: git push. Then back in the target (optional): git checkout master; git branch -d tmp" Source: https://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked (in the comments of the 2nd answer). – teomor May 08 '19 at 14:20
  • Actually that doesn't work for me.. I need to delete the branch and re-create it everytime. So I will try your solution and see if the dev branch suits me better. Thanks a lot! – teomor May 08 '19 at 14:30
  • If you use the above solution you can make a script on your server to automaticly pull the latest changes of a specific branch to publish them. IMO it's a bit easier :) – Baklap4 May 09 '19 at 07:02