Remove Folder from bitbucket history - php

I am new to git repository. I have a 2gb limit in bitbucket and bymistake I added one images folder of 1.5 gb and pushed the code.
Now the bitbucket size is 1.8gb. To remove the folder from history I followed these steps.
Remove folder and its contents from git/GitHub's history
git filter-branch --tree-filter 'rm -rf import/images' --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git
update-ref -d
echo import/images/ >> .gitignore
git add .gitignore
git commit -m 'Removing images from git history'
git gc
git push origin master --force
I followed these steps and checked in my previous commits and the folder is removed. but git count-objects -v still gives the size-pack of 1.8gb
Also in the bitbucket repository settings its still showing 1.8gb
Am I missing something?

You have to go to bitbucket page in order to delete a file/files.
Go inside your repo,
select source (left panel)
pick your branch
find your folder
go inside and delete all the images you have there (there is the delete option top right where there are 3 clickable dots)
You can't delete the folder like that (for some reason bitbucket allows only file delete and branch delete manually) but you can always empty the folder manually and remove it through console in the next git push.
Caution. This work like a "hard delete" so be extra careful when you use it, and don't use it if you can use git commands.
Edit
git gc --prune=now //accepts date
Have a look at this link there are quite a few options that may suit your problem.
Another approach is the git filter-branch --force. More about this command you can find here. This command is specifically for "rewriting" git history so it will do your job. It's tricky but it will do.
One last and final approach (never used this one to be honest) is 3rd party software (Really secure as even atlassian includes it in their documentation) but is in java so first you will need to install java in order to run a java command in your console. I saw your php tag so i just leave this as last just to concider it. I leave ths link here.

Related

Clone latest committed files on Git Repo

I want to clone the last committed files via Git. I tried the --depth 1 parameter. But the all project was cloned. I want to download only the last edited files because the project size is too high.
The command I tried;
git clone --depth 1 https://USERNAME:PASSWORD#HOST/PATH
I want to clone last edited and updated files. I don't want to go back to the previous committed files. I will analyze the last edited files with SonarQube. I don't want to clone the all project for this. And : I'm using exec with PHP to run commands.
How can I clone only the last edited files? Thank you.
You can't just download a couple of files from a remote git repository. In order for git to restore a file, it must have all the data of the repository available. So unfortunately you will have to download the whole repository. If you would like to see which files have been changed, you can then run git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT HEAD to receive a list of modified files.
$ git archive --format=tar --remote=<repository URL> HEAD | tar xf -
Also, if your browse remote repository using some web interface like Gitweb or GitHub, then it may have 'snapshot' feature with it and you can also download the newest version.

GIT Ignore and GIT Hook - File is replaced after a commit-push

My GIT repository is located /var/repo/myRepo.git. I set a GIT hook post-receive* to copy the files from my repository to the folder of my project
git --work-tree=/var/www/laravel --git-dir=/var/repo/myRepo.git checkout -f
Each time I commit and push something on the server, the file var/www/laravel/config/services.php is replaced and the modification I did on the server is replaced by my local copy.
For instance, if I manually modify the following file like this on the server (by ssh session)
var/www/laravel/config/services.php
This is the modified content of this file
It will be like that after a commit and push
var/www/laravel/config/services.php
This is the default content of this file
I tried to add /config/services.php to my .gitignore but it does not seem to work.
.gitignore
/node_modules
/public/storage
/public/hot
/storage/*.key
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
/config/services.php
What should I do so this file is not replaced each time I commit something on my server ?
What should I do so this file is not replaced each time I commit something on my server?
You have only two options:
don't check it in, or
don't check it out.
Your git checkout -f command means "get me the latest commit, overwriting everything." If the latest commit has a new version of a file, that overwrites the old version of the file.
(Moreover, a .gitignore file does not mean what you think it means. It's not a list of files to ignore. It's a list of files—or name patterns—not to complain about. Usually most important, it lets you declare to Git: "Yes, I know these are in my work-tree and not in my index; don't tell me that." That's on the input side—i.e., the "don't check it in" part.)
This leads to a general rule about configurable software, where the software itself is maintained in Git, or indeed any version control system: Do not put the configuration into the version control system. The configuration is not part of the software.
Consider Git itself, for instance. You must configure Git to tell it your user.name and user.email in order to make commits with your user-name and email address. Now imagine Git came with its configuration file built into the software, that said your user name is Fred and your email is fred#fred.fred. Every time you updated Git to a new version, it would change your name back to "Fred <fred#fred.fred>". That's not very friendly, is it?
Git stores your configuration outside of the Git software. There are, to be sure, default configuration entries, but anything you set, is kept elsewhere. Updating Git does not touch this. This is not specific to Git, or even version-control systems: any system that provides upgrades must not store its configuration in a file that is destroyed by the upgrade.
So, stop doing that.
I did git rm /config/services.php and reimported the file manually. Now the file is not replaced by GIT.

Uploaded files not appearing in Github

I am having a really strange problem. I'll try keep it simple.
I am developing a web app using Laravel and it involves allowing users to upload and download files. I have created a function that allows a user to upload a file. This works grand. I can login to my site from another computer and see that the file was uploaded, and I can download it. So it seems to be working grand.
However, the file that I have uploaded is not appearing in the folder which it is supposed to on GitHub. This is really strange because the file definitely is there...because I can download it from another computer.
Code for uploading the file.
$destinationPath = public_path().'/files/';
$file->move($destinationPath, $file->getClientOriginalName());
I cannot see why the file I have uploaded isn't appearing on Git seeing as it has definitely uploaded.
Any ideas?
The files that you are looking for are located on your server. Adding or removing files from the server does not add or remove them from your git repository. In order to add them to your git repository, you would need to run the git commands to do so. The commands depend on what exactly you want to add, but as a general example:
To add all files
git add -A
Followed with a commit
git commit -m "example commit"
They will not appear on GitHub until you push
git push origin branch-name
For Git you have to manually commit the files.
Go to your command line and run
git status
If it shows public/ files then you would need to manually add them.
git add public
If they're not showing there, they're either being ignored or they've been committed and possibly not pushed.
First thing inside your project folder if the .git folder is created, delete it. Then write this series of code
commands:
git config --global user.name "your name"
git config --global user.email "your email"
git init
git add .
git commit -m "Initial commit"
git status
git remote add origin # paste here ssh link from github repository
git push origin master
Have a look at your .gitignore file. It might be the case that you instruct Git there to ignore the file.

Git checkout from one directory to another on server (cloudways)

I have a laravel app on Cloudway DigitalOcean, my app in /public_html , I want to update my app using git , so I have created folder private_html/git , where I pull my edited project from bitbucket, now I want to checkout it to my public_html/ , how do I do that ? Thank You
You can either deploy the changes to public/html via git or manually copy the files across.
Manually copy option
Depending on full path, something like:
cp -a private_html/git/. public_html/
Note: -a is a recursive option that preserves file attributes
This won't remove any files that have been removed in private_html/git so you would have to do that manually, or remove everything before copying the files across.
Git pull option
First, make sure you have pushed all the changes made in private_html/git to your remote (bitbucket repo).
Set up the current copy in public_html/ as a git repo.
In public_html/
git init
Then add the bit bucket remote
git remote add origin git#bitbucket.org:user-name/repo-name
Note: Get the proper bitbucket remote from your bitbucket account
Then pull the changes from the remote
git fetch --all
git reset --hard origin/master
Warning: You'll lose any differences that are currently in public/html so be careful. Always a good idea to back up everything before these kinds of changes so I'd suggesting archiving the code in public_html before overwriting it.

git config files for php app

So here's what happened:
I have a local repo for dev
pushed to origin master
from my test env I always do git pull to update
Now of course I have different config files in the test env.
I locally changed my config file on test env.
In a hurried moment (...) I committed my local changes after a merge was conflicting.
Now the app runs fine, but git tells me
Your branch is ahead of 'origin/master' by 13 commits.
I understand, as I committed my local change - which I do NOT wish to push to master.
What would be the correct way to fix this? I want to:
Have my local copy of the config file
do not want to mess up with the basically correct config on the test env
get rid of the 'your branch is ahead' message
keep my master and my test env clean
Thanks!
You want to throw away your local commits on dev with
git push -f . origin/master:master
or
git reset --hard origin/master
if you are on that branch already.
The best way to treat config transformations is with smudge/clean scripts. They are explained it the progit.org/book in the attributes chapter. You commit a config that will work nowhere. Rely on the scripts to transform the config to what it needs to be on each environment.
If you just want to change your most recent commit, it is pretty easy. You stage the changes you want to make, and then amend your last commit.
In your case, you would change your config file back to the way you want it, stage it, then commit an amendment. Using config.php for your config file, it would look like:
$ git add config.php
$ git commit --amend
See the Rewriting History chapter of Pro Git

Categories