Git Steps To Follow
Consider Deployment Process Related Paths
Server IP: xxx.xxx.xxx.xxx
Git Origin location: /home/git/git_origin_location_folder_name/sites
Development Server deployed Site location: /usr/share/nginx/html/development_server_deployed_site_location_folder_name/sites
To connect xxx.xxx.xxx.xxx ssh'ly through terminal: ssh git@xxx.xxx.xxx.xxx
Initial One Time Setup :
Step 1: Go to the directory you want to clone git to..
cd /var/www/html
Step 2: Initiallize the git
git init
After Installing GIT and Adding the GIT Public Key to Master Origin up-above in live :
Step 3:
cd /var/www/html
git clone git@xxx.xxx.xxx.xxx:/home/git/git_origin_location_folder_name/sites
Step 4:
cd /var/www/html/development_server_deployed_site_location_folder_name_local
git clone git@xxx.xxx.xxx.xxx:/home/git/git_origin_location_folder_name/sites
----------------------------------------------------------------------------------
| :: Section I : To Take DB Backup on Development Server :: |
----------------------------------------------------------------------------------
Step 1: Login to Development Server (Live) with the .pem key provided
Command:
ssh -i ~ / .ssh / id_rsa_key.pem username@xxx.xxx.xxx.xxx
Step 2: Its possible that the .pem key you have locally is too open and does not have restricted access. In which case, you will get error :
Permissions 0664 for '/ home / sandip / .ssh / id_rsa_key.pem' are too open.
To solve it change the file permissions of .pem file
Command:
sudo chmod 0600 id_rsa_key . pem
Step 3: Now, try logging in again :
Command:
ssh -i ~ / .ssh / id_rsa_key.pem username@xxx.xxx.xxx.xxx
Step 4: Go to the ~/drush-db-backups/ folder to check if todays backup is taken
Command:
cd ~ /drush-db-backups/
ll
Check if DB Backup is taken or not.
Step 5: Change to the folder
Command:
cd /usr/share/nginx/html/development_server_deployed_site_location_folder_name
Step 6: Fire sql-dump Command through drush
Command:
drush sql-dump > ~ /drush-db-backups/database-backup-2017_12_22-1600.sql
Step 7: Now, zip the backed up file
Command:
zip ~ /drush-db-backups/db_2017_12_22-1600.zip ~/drush-db-backups/database-backup-2017_12_22-1600.sql
Step 8: Then, get out of Development Server
Command:
exit
Step 9: Finally, get the zip file copied LOCALLY
Command:
scp -i ~ / .ssh / id_rsa_key.pem username@xxx.xxx.xxx.xxx : ~/drush-db-backups/db_2017_12_22-1600.zip/home/sandip/Desktop
------------------------------------------------------------------------------------------
| :: Section II : To Make Release Branch on Development Server :: |
------------------------------------------------------------------------------------------
Step 1: Go to the Development Server version of my own locally in terminal i.e. clean copy..
Command:
cd /var/www/html/development_server_deployed_site_location_folder_name_local/sites
Step 2: If at all you are in any other branch, maybe yesterday's release branch.. then switch to 'master' branch.
Command:
git checkout master
Step 3: Get all the changes off of master/origin to this local copy
Command:
git pull
Step 4: Locally Create and Switch into the release branch ..
Command:
git checkout -f -b <branch_name_in_format_release_yyyymmdd_hhmm> origin/master
e.g. git checkout -f -b release_20170922_2000 origin/master
HOWEVER, This will create branch forcefully (note, -f option in command)... Since we shouldn't do it.. So,
git checkout -b <branch_name_in_format_release_yyyymmdd_hhmm> origin/master
e.g. git checkout -b release_20170922_2000 origin/master
Step 5: Push the release branch to remote..
Command:
git push origin <branch_name_in_format_release_yyyymmdd_hhmm>:<branch_name_in_format_release_yyyymmdd_hhmm>
e.g. git push origin sandip_release_20170922_2000:sandip_release_20170922_2000
Step 6: Go To Development Server Live and Pull this release branch from origin to Development Server Live..
Command:
git fetch
--------------------------------------------------------------------------------------
| :: Section III : To Get Release Branch in Development Copy :: |
--------------------------------------------------------------------------------------
Step 1: Ensure your folder is fully writable.. In this case, /var/www/html
Command:
sudo chmod -R 0777 /var/www/html
Daily :
Step 2: Move to your sites folder
Command:
cd ~/sites/
& Ensure its fully writable..
sudo chmod -R 0777 ~/sites/
Step 3: Fetch the origin of git from server to your local
Command:
git fetch origin
Step 4: However, its preferred, to pull the changes from release branch as its always one step ahead..
4.1 Check if your intended release branch available on remote origin
Command:
git branch -r|grep release
4.2 If not found the release branch, make sure to fetch the git..
Command:
git fetch
4.3 Then on finding the said release branch fetch that release branch from origin on to your local
Command:
git fetch origin <release_branch_name_todays_date>
e.g. git fetch origin release_20170922_2000
Step 5: Create a branch and switch to that newly created branch..
Make sure to do this once in the morning.. if you are already in that branch, no need to checkout
Command:
git checkout -f -b <branch_name_preferably_todays_date> origin/<release_branch_name_todays_date>
e.g. git checkout -f -b blah_blah_branch_22Sep17 origin/release_20170922_2000
HOWEVER, This will create branch forcefully (note, -f option in command)... Since we shouldn't do it.. So,
Command:
git checkout -b <branch_name_preferably_todays_date_with_task_explanation>
e.g. git checkout -b blah_blah_branch_22Sep17
Please make a note, above command will branch it from local master, which should be ideal case, as we already have pull origin master to our local master so our master is matching to origin master.. so fork it from local instead of origin one.
Do needed changes in code and after code change
Step 1: Check the current status of git
Command:
git status
Step 2: Check the diff of file first... This is to ensure, only your needed changes are getting tracked...
Command:
git diff -b <file path to add without first forwarding slash>
{Make a Note, -b makes sure to eliminate the extra space to compare}
e.g. git diff -b all/modules/custom/custom_module/custom_module.module
Step 3: Now, finally if everything is fine through diff.. and if only expected changes are getting tracked.. then add file..
Command:
git add <file path to add without first forwarding slash>
e.g. git add all/modules/custom/custom_module/custom_module.module
Step 4: Commit the changes now..
Command:
git commit -m "Bug Id: Short Description About Fix"
e.g. git commit -m "123456: Minor Change in Regular Expression"
Step 5: Now, push your changes to origin.. Make sure you are pushing the changes via branch only...
In other words, directly your branch itself is getting pushed..
Command:
git push origin <branch_name_created_in_step4_above_of_section1>:<new_branch_name_on_origin>
e.g. git push origin 22Sep17:abcdefg_Sandip
-----------------------------------------------------------------------------------------------------------
| :: Section IV : To Move the changes done so far to Development Server Live :: |
-----------------------------------------------------------------------------------------------------------
Now in Local Development Server
Step 1: Go to the Development Server version of my own locally in terminal i.e. clean copy..
Command:
cd /var/www/html/development_server_deployed_site_location_folder_name_local/sites
{Ensure you are in release branch}
Step 2: Check for the Branch that you wish to merge present in your local uat clean copy.
Command:
git branch -r|grep <some_starting_characters_of_branch_name>
Step 3: If not present, then fetch the latest branches off of origin that developer pushed, to your local clean copy
Command:
git fetch
Step 4: Now, merge that branch locally into the release branch..
Command:
git merge origin/<branch_name_on origin_that_to_be_merged>
e.g. git merge origin/xyzpqr_Sandip
If At all, you want to revert your this merge or any no. of merges you should use :
git reset --hard OR
git reset --hard HEAD~<no_of_commits_your_local_branch_is_ahead>
Step 5: Now, push the Development Server-local release branch to origin {Make a note, its a push and not Merge}..
Command:
git push origin <branch_name_in_format_release_yyyymmdd_hhmm>
e.g. git push origin release_20170922_2000
Step 6: Now, Login to Development Server of LIVE (not Local) and Pull that release branch you just merged in origin /master onto Development Server
Command(s):
ssh git@xxx.xxx.xxx.xxx {To connect xxx.xxx.xxx.xxx ssh'ly}
cd /usr/share/nginx/html/development_server_deployed_site_location_folder_name/sites {To Go to Development Server Branch}
git checkout master
git fetch
git checkout release_20171120_0900
git pull
Voila ! Now the changes are on Development Server i.e. live
Step 7: Email QA Team to Start Testing..
Step 8: After QA Testing report, Login to Origin and merge the release branch from origin /master into origin /master
Command:
git merge <branch_name_in_format_release_yyyymmdd_hhmm>
e.g. git merge release_20170922_2000
------------------------------------------------------------
| :: To Import Database on Testing Server :: |
------------------------------------------------------------
- Upload zip file on regression (aaa.aaa.aaa.aaa) at : /home /username /drush-db-backups
- Connect to Regression through command prompt
- ssh -i ~/ .ssh / id_rsa_key.pem username@aaa.aaa.aaa.aaa
- cd /home/username/drush-db-backups
- unzip db_2016_02_11_0300.zip
it will unzip it to " /home/username/drush-db-backups/home/username/drush-db-backups"
- Connect to mysql server of regression for Creating New Database Entry
- mysql -h testing_hostname -u root -p
- mysql> show databases;
- mysql> CREATE DATABASE testing_db_name;
- mysql> exit
- Import File into Database
- mysql -h testing_hostname -u root -p testing_db_name < /home/username/drush-db-backups/database-backup-2016_02_17_0300.sql
- Confirm the changes in mysql
- mysql -h testing_hostname -u root -p
- use testing_db_name
- describe tablename;
----------------------------------------------------------------------
| :: To Bring Newly Added Files in Git Versioning :: |
----------------------------------------------------------------------
Before bringing new files in GIT Versioning, make sure you change the file /folder owner to 'git' user
- ssh -i ~ / .ssh / id_rsa_key.pem username@xxx.xxx.xxx.xxx
- cd /usr/share/nginx/html/development_server_deployed_site_location_folder_name
- sudo chown -R git:git sites
- sudo chmod -R 777 sites
- exit
Now, Once that is done,
- ssh git@xxx.xxx.xxx.xxx
- cd /usr/share/nginx/html/development_server_deployed_site_location_folder_name/sites
- Make sure you are in master, if not, make a note of current branch which you are in and then
git checkout master
- git checkout -b newfiles_20170312_1900
- git add 'default/files/242d13102060.png' 'default/files/icons/lmn/pqr/xy/'
- git commit -m "Adding and removing file as observed on 12 Mar 2017 1900 IST"
- git push origin newfiles_20170312_1900
- cd ~ /project_folder_name/sites/
- rm 'default/files/242d13102060.png' 'default/files/icons/lmn/pqr/xy/'
- git merge newfiles_20170312_1900
- If you were not in master at Step 3 then go back to that branch you were in already and fire a command -
git merge origin/newfiles_20170312_1900
--------------------------------------------------------------------
| :: To Deploy the changes to the Testing Server :: |
--------------------------------------------------------------------
Locally to merge branches to testing_master
- be in /var/www/html/local_project_folder_name/sites (testing_master) Locally then
- git fetch
- git checkout <branch_name> {To evaluate or review}
- git log --name-only {For checking the file names that got changed}
- git checkout testing_master
- git merge origin/<branch_name>
- git push origin testing_master
And Then
To make the changes live i.e. to merge the branches on testing server
- Login to Testing Server :
ssh -i ~ / .ssh / id_rsa_key.pem username@aaa.aaa.aaa.aaa
- Go to folder :
cd /usr/share/nginx/html/development_server_deployed_site_location_folder_name/sites
- And then
Command:
git pull - Finally
Command:
drush cc all
------------------------------------------------
: How to Undo a commit and redo :
------------------------------------------------
$ git commit ... (1)
$ git reset --soft HEAD~1 (2)
$ edit (3)
$ git add .... (4)
$ git commit -c ORIG_HEAD (5)This is what you want to undo
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset". (The quotes may or may not be required in your shell)
Make corrections to working tree files.
Stage changes for commit.
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
------------------------------------------------------------------------
: How to Resolve & Merge Conflicts manually locally :
------------------------------------------------------------------------
Merge The Conflicts that are not resolved automatically by bitbucket and needs to be resolved manually locally, can be sorted out as :
One Time Step ::
1. Add a Path Reference for the remote repositories.. should be done to all which has read n write access..
i. git remote add <nick-name-for-local> <remote-repo-path>
e.g. git remote add testing https://sandipt@bitbucket.org/project-name/testing.git and
git remote add development https://sandipt@bitbucket.org/project-name/development.git and
git remote add production https://sandipt@bitbucket.org/project-name/production.git
Repeatitive Steps ::
1. Get Code Information of Remote Destination..i.e. repo. on which pull request was created.. in our case..testing
i. git fetch <nick-name-for-remote-repo>
e.g. git fetch testing
Just FYI... One should add fetching for all of references we've pulled in step.. ideally should be done.. so, git fetch development and git fetch production should be fired each after adding reference in above step 1.i
All those added path references for remote repositories and code information of remote destinations can be always found in <path-to-project>/.git/refs/remotes e.g. /var/www/project-folder/.git/refs/remotes in our case..
2. Create local branch derived from Remote Destination Branch..
i. git branch <temp-name-for-branch> <nick-name-for-remote-repo>/<nick-name-for-remote-repo-destination-branch>
e.g. git branch tempConflictResolve testing/master
3. Pull the changes from source branch
i. git pull <nick-name-for-remote-repo> <nick-name-for-remote-repo-source-branch>
e.g. git pull development master
4. Resolve the merge conflicts if there are any..
i. git mergetool
This step will open up meld for resolving the conflicts..
In this, The left most file would be <nick-name-for-remote-repo-destination-branch> i.e. testing/master in this case The right most file would be <nick-name-for-remote-repo-source-branch> i.e. development/master in this case and the middle one would be Result after merge conflict resolved..
5. Commit the change after Merge Conflict Resolved
i. git commit -m "message-with-reason"
6. Push the changes to new branch on live Destination Remote repo.
i. git push <nick-name-for-remote-repo> <temp-name-for-branch>
e.g. git push testing tempConflictResolve
7. Create pull request on Remote Destination Branch from Temporary pushed branch
i. to be done through Bitbucket live url.
8. After Merging the above pull request from Bitbucket.. Delete the temporary branch from local and remote repo..
i. Delete off the live
ii. Delete off the local
a. git checkout master
b. git branch -D <temp-name-for-branch>
e.g. git branch -D tempConflictResolve
-----------------------------------------
: Adding tags to the commits :
-----------------------------------------
For adding tags to the commits such as declaring some versions into the repository..
1. $ git pull
First pull the repository to bring your local copy at latest state.
2. $ git tag -a v1.4 -m 'my version 1.4'
Here,
-a is for Creating an annotated tag in Git
-m specifies a tagging message, which is stored with the tag.
3. $ git show v1.4
You can see the tag data along with the commit that was tagged by using the git show command.
4. $ git push origin :tagname
Now, push this version tag to remote repository.
To remove the added tag..
i. git tag -d :tagname
ii. git push origin :refs/tags/:tagname (if tag was pushed to remote as well..)
OR git push --delete origin :tagname
Add new comment