How to not overwrite database information in a Git merge - database

I have a CRUD app that will be used to track trucking appointments. I'm ready to deploy the app, but I'm worried about merging a future branch into the main branch without overwriting the existing information in the database. How can I use CLI git commands to avoid overwriting the data?
These are the general steps I've been using to merge a branch that I've made edits to into the main branch:
>git checkout main
>git merge <branch_name>
>git branch -d <branch_name>
>git push origin --delete <branch_name>

Note: putting test.db within my .gitignore configuration file is not enough.
If it was tracked, you would need to "untrack" it with:
git rm --cached -- test.db
Then you can check your new .gitignore rule is effective with:
git check-ignore -v -- test.db

Production data should not be stored in the same directory as code for many reasons other than worrying that git will overwrite your data. For one, your software shouldn't have permissions to modify itself, even though it needs permissions to overwrite its database. You'll need to search/ask how best to package Python and set up a service on the operating system you'll be using.
git has some safety features that protect untracked files. checkout and switch won't clobber them. But clean is designed to delete untracked files. Worse: reset --hard will overwrite untracked files without warning. Do not put important data in a working directory unless it's part of the project.

I included test.db within my .gitignore configuration file.

Related

Why has GitLab saved one folder of my project as a Subproject commit?

I have started developing a website, saved in my local folders, and I am trying to save it to a GitLab repository. I created a new repository on GitLab and then did the following:
cd existing_folder
git init --initial-branch=main
git remote add origin https://gitlab.com/...
git add .
git commit -m "Initial commit"
git push -u origin main
The project comprises two folders, Server and Client, the Client is a React App. However the Client folder is appearing as a red folder icon that can't be opened:
When I click on the initial commit it says that Client has been added as a Subproject commit:
I don't know what this means, I have built websites with a similar structure before and Gitlab has not done this. I just want the contents of both Client and Server folders to be saved in the repo.
What you did:
Created a Git repository holding the client side of the app, and committed stuff to that. This repository resides in client/.git/. It contains files that are presumably named things like main.js and so on (or main.ts and so on, if TypeScript, etc.).
Created, in .git, a second, over-arching Git repository holding the server side of the app, and committed stuff to that. This repository resides in .git/ and contains files named server/functions.js and so on. Git will be calling this second over-arching repository a superproject. It's just a Git repository though, albeit one with one special property.
Note that as far as Git is concerned, folders don't even exist: files just have names with embedded slashes—always forward slashes, never backslashes—in them. Git will convert these to the folder-and-file arrangements that your OS requires, as needed. This deals with any backslash things that the OS requires as well (e.g., converting names from foo/bar.js to foo\bar.js on Windows). But for the most part this is invisible and we can pretend that we have folders full of files; we're going to work with OS-level files extracted from commits anyway, and those really are files-in-folders. So for the rest of this we'll pretend we do have folders.
Anyway, for good (security) purposes, Git refuses to ever store anything named .git. So all the files in client/.git/* literally cannot be stored: you cannot put the sub-repository in client into the superproject in ., even though ./.git holds server/ and all those files just fine.
Instead, Git will add to the superproject something that Git calls a gitlink. This is an entry that says: After you have cloned and checked out a commit for the superproject, clone another Git repository too. In that new, additional Git repository, I have saved the raw hash ID of the commit you should check out in that sub-project.
This is why you see that instruction:
Subproject commit 327f06...88c
That's the thing that makes this superproject Git repository a superproject: it contains a gitlink named client, and the gitlink itself—which is otherwise just like a file—says 327f06...88c. That 327f06...88c hash ID is the hash ID of a commit in the Git repository that Git should clone and place into a client folder within the superproject.
Now, the trick here is that for Git to run:
git clone <url> client && (cd client && git checkout 327f06...88c)
Git is going to need to know the url to use on this command-line. The gitlink records only the commit hash ID and the path name client. The URL goes into a separate file, as chickahoona mentioned, named .gitmodules.
The thing is, when you ran:
git add .
this will not have created the .gitmodules file for you. To create that file, you must use git submodule add instead of git add. The git submodule add command takes the same kind of argument as git add—the name client in this case—but also takes a URL, which it will add to the .gitmodules file in the form that Git will need to run the git clone command later.
You won't have to run this git clone command on your computer, because you already have a Git repository in client/.git. So the fact that git add . only does half the job doesn't affect your use of this superproject/gitlink pairing. But it means no one else can use your superproject because the cloning directive is missing.
What's not clear is whether you, personally, want to store your server and client in two separate Git repositories like this—one a superproject and one a submodule—or whether you want to store this in, say, three Git repositories:
one for the server only,
one for the client only, and
one to bind them.1
The last one would be the only superproject in the setup, containing only two gitlinks and a .gitmodules file referring to the two submodules. The server's files would be in a Git repository stored in server/.git and the client's files would be in a Git repository stored in client/.git (as they are now).
Or, perhaps you don't want any submodules at all. In this case, you will need to remove the client/.git folder (or move it elsewhere). This will destroy (or move elsewhere) the entire repository containing the client. You will, however, retain any working tree files from that repository. Now that the inner repository is gone, those working-tree files can be git add-ed to what was the superproject. These files will have named like client/main.js and Git will create a client/ folder and extract them to files named main.js and the like within that client/ folder.
Only you can decide what you want done here. Decide and then proceed: either add the submodule correctly, with git submodule add, or turn the server into a submodule as well and add that, or remove (or move) the client Git repository but not the current version of the client files.
1Three rings for the Elven-kings under the sky,
Seven for the Dwarf Lords in their halls of stone...
My guess would be that you were doing a git init in the wrong folder. Now your Client folder is maybe a submodule of the actual top git repo.
Check if you have in your Client folder a hidden .git directory
To fix that remove the .git folder in the Client directory. Also check for a file called .gitmodules in the top folder and remove it if it exists.

Removing a file in Git repo but file doesn't exist

In committing and pushing to my repo, I get the following error:
The issue is that I've already manually deleted the video file. The video does not exist anywhere in my repo.
I also tried to
git rm src/assets/video/Greensleeves and it says fatal: pathspec src/assets/video/Greensleeves did not match any files.
How can I get passed this so that I can commit/push?
Try and apply the new git filter-repo, which does replace the old git filter-branch or BFG.
It has many usage examples, including path-based filtering, in order for you to remote the src/assets/video/Greensleeves file in past commits:
To keep all files except these paths, just add --invert-paths:
git filter-repo --path src/assets/video/Greensleeves --invert-paths
Then git push --force (that does rewrite the history of your repository, so make sure to notify any other collaborator)
Since it must be done on a fresh clone:
Don't touch anything to your current clone folder
Create a separate clone of the repository, where you do the filter repo
In that second clone, now cleaned (no more big file in its history), import your work from your first repo
That is, for point 3:
cd /path/to/second/clone
git --work-tree=/path/to/first/original/clone add .
git commit -m "Import work from first clone"
git push --force

Push changes to bitbucket using atom

I am using the text editor Atom and it should be possible to upload directly to bitbucket from atom. I found a thread here on stackoverflow someone asked for this and someone recommended git-plus package. I did download this package but it tells me i need to edit user.email and user.name variable in gitconfig file. But it doesnt say anywhere where i can find this gitconfig file. I found a few config files but none of them have these variables. Is this gitconfig file in my .git folder in my project or is it somewhere in atom? I hope someone here can help me with this
Set up git in your shell to use bitbucket as the remote repo.
You will first need to sort that basic Git config thing about the emails:
$ git config --global --get user.name
Your Name Here
$ git config --global --get user.email
your_email#someplace.com
If you don't have these values already added, then simply add them. Put your current directory someplace under the root of your atom repository (or, if you want to use the --global parameters [recommended]), then anywhere will do, really so long as it's your login you're working with.)
$ git config [--global] user.name "Your Name"
$ git config [--global] user.email "your_email#somplace.com"
First set up your ssh key so you don't have to type your password on every command. Once that's done, then go to the Git repo you set up for your Atom project. Most likely that's in the top level of my project. Go there and...
$ git remote -v
$ git remote add origin git#bitbucket.org:youruid/yourrepo.git
Now try it from the shell to make sure all is working:
$ git push
It's very likely you'll get a message similar to:
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
Follow the advice given, then, if you're feeling particularly paranoid, make a change to any file in the repo -- using atom if you like, doesn't matter -- and try it all again:
$ git add -u
$ git commit [-v]
...
$ git push
This time, it should go off without a hitch.
Now back to atom...
If you've configured things there so that atom can access your Git repo locally, there should now be a Fetch widget in the lower right corner:
If you make a change now in atom, you'll see this change to Push... with the functionality to match.
The GitHub widget still doesn't do anything -- which is mildly annoying -- but I believe you might be able to find an atom package out there that does similar things.

Download from gitHub [duplicate]

The command git clone git#github.com:whatever creates a directory named whatever containing a Git repository:
./
whatever/
.git
I want the contents of the Git repository cloned into my current directory ./ instead:
./
.git
Option A:
git clone git#github.com:whatever folder-name
Ergo, for right here use:
git clone git#github.com:whatever .
Option B:
Move the .git folder, too. Note that the .git folder is hidden in most graphical file explorers, so be sure to show hidden files.
mv /where/it/is/right/now/* /where/I/want/it/
mv /where/it/is/right/now/.* /where/I/want/it/
The first line grabs all normal files, the second line grabs dot-files. It is also possibe to do it in one line by enabling dotglob (i.e. shopt -s dotglob) but that is probably a bad solution if you are asking the question this answer answers.
Better yet:
Keep your working copy somewhere else, and create a symbolic link. Like this:
ln -s /where/it/is/right/now /the/path/I/want/to/use
For your case this would be something like:
ln -sfn /opt/projectA/prod/public /httpdocs/public
Which easily could be changed to test if you wanted it, i.e.:
ln -sfn /opt/projectA/test/public /httpdocs/public
without moving files around. Added -fn in case someone is copying these lines (-f is force, -n avoid some often unwanted interactions with already and non-existing links).
If you just want it to work, use Option A, if someone else is going to look at what you have done, use Option C.
The example I think a lot of people asking this question are after is this. If you are in the directory you want the contents of the git repository dumped to, run:
git clone git#github.com:whatever .
The "." at the end specifies the current folder as the checkout folder.
Go into the folder.. If the folder is empty, then:
git clone git#github.com:whatever .
else
git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master
Basic Git Repository Cloning
You clone a repository with
git clone [url]
For example, if you want to clone the Stanford University Drupal Open Framework Git library called open_framework, you can do so like this:
$ git clone git://github.com/SU-SWS/open_framework.git
That creates a directory named open_framework (at your current local file system location), initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the newly created open_framework directory, you’ll see the project files in there, ready to be worked on or used.
Cloning a Repository Into a Specific Local Folder
If you want to clone the repository into a directory named something other than open_framework, you can specify that as the next command-line option:
$ git clone git:github.com/SU-SWS/open_framework.git mynewtheme
That command does the same thing as the previous one, but the target directory is called mynewtheme.
Git has a number of different transfer protocols you can use. The previous example uses the git:// protocol, but you may also see http(s):// or user#server:/path.git, which uses the SSH transfer protocol.
You can use following git command to clone with custom directory name
git clone <git_repo_url> <your_custom_directory_name>
Note: You don't need to create your custom directory because it will create automatically
To clone git repository into a specific folder, you can use -C <path> parameter, e.g.
git -C /httpdocs clone git#github.com:whatever
Although it'll still create a whatever folder on top of it, so to clone the content of the repository into current directory, use the following syntax:
cd /httpdocs
git clone git#github.com:whatever .
Note that cloning into an existing directory is only allowed when the directory is empty.
Since you're cloning into folder that is accessible for public, consider separating your Git repository from your working tree by using --separate-git-dir=<git dir> or exclude .git folder in your web server configuration (e.g. in .htaccess file).
To clone to Present Working Directory:
git clone https://github.com/link.git
To clone to Another Directory:
git clone https://github.com/link.git ./Folder1/Folder2
Hope it Helps :)
If you want to clone into the current folder, you should try this:
git clone https://github.com/example/example.git ./
When you move the files to where you want them, are you also moving the .git directory? Depending on your OS and configuration, this directory may be hidden.
It contains the repo and the supporting files, while the project files that are in your /public directory are only the versions in the currently check-out commit (master branch by default).
Usage
git clone <repository>
Clone the repository located at the <repository> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.
git clone <repo> <directory>
Clone the repository located at <repository> into the folder called <directory> on the local machine.
Source: Setting up a repository
Clone:
git clone git#jittre.unfuddle.com:jittre/name.git
Clone the "specific branch":
git clone -b [branch-name] git#jittre.unfuddle.com:jittre/name.git
Make sure you remove the .git repository if you are trying to check thing out into the current directory.
rm -rf .git then git clone https://github.com/symfony/symfony-sandbox.git
From some reason this syntax is not standing out:
git clone repo-url [folder]
Here folder is an optional path to the local folder (which will be a local repository).
Git clone will also pull code from remote repository into the local repository.
In fact it is true:
git clone repo-url = git init + git remote add origin repo-url + git pull
Here's how I would do it, but I have made an alias to do it for me.
$ cd ~Downloads/git; git clone https:git.foo/poo.git
There is probably a more elegant way of doing this, however I found this to be easiest for myself.
Here's the alias I created to speed things along. I made it for zsh, but it should work just fine for bash or any other shell like fish, xyzsh, fizsh, and so on.
Edit ~/.zshrc, /.bashrc, etc. with your favorite editor (mine is Leafpad, so I would write $ leafpad ~/.zshrc).
My personal preference, however, is to make a zsh plugin to keep track of all my aliases. You can create a personal plugin for oh-my-zsh by running these commands:
$ cd ~/.oh-my-zsh/
$ cd plugins/
$ mkdir your-aliases-folder-name; cd your-aliases-folder-name
# In my case '~/.oh-my-zsh/plugins/ev-aliases/ev-aliases'
$ leafpad your-zsh-aliases.plugin.zsh
# Again, in my case 'ev-aliases.plugin.zsh'
Afterwards, add these lines to your newly created blank alises.plugin file:
# Git aliases
alias gc="cd ~/Downloads/git; git clone "
(From here, replace your name with mine.)
Then, in order to get the aliases to work, they (along with zsh) have to be sourced-in (or whatever it's called). To do so, inside your custom plugin document add this:
## Ev's Aliases
#### Remember to re-source zsh after making any changes with these commands:
#### These commands should also work, assuming ev-aliases have already been sourced before:
allsource="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh; clear"
sourceall="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh"
####
####################################
# git aliases
alias gc="cd ~/Downloads/git; git clone "
# alias gc="git clone "
# alias gc="cd /your/git/folder/or/whatever; git clone "
####################################
Save your oh-my-zsh plugin, and run allsource. If that does not seem to work, simply run source $ZSH/oh-my-zsh.sh; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh. That will load the plugin source which will allow you to use allsource from now on.
I'm in the process of making a Git repository with all of my aliases. Please feel free to check them out here: Ev's dot-files. Please feel free to fork and improve upon them to suit your needs.
If you are in the directory you want the contents of the git repository dumped to, run:
git clone git#github.com:origin .
The "." at the end specifies the current folder as the checkout folder.
If you are using ssh for git cloning you can use the following command.
git -C path clone git#github.com:path_to_repo.git
eg:
git -C /home/ubuntu/ clone git#github.com:kennethreitz/requests.git would pull the git repository for requests to your /home/ubuntu/ path.
go to the directory where you want to clone the repo.
(don't run git init command inside that directory)
simply run the command,
git clone <git repo url> .
Example: git clone https://github.com/Rashmi-Wijesekara/portfolio.git .
Although all of the answers above are good, I would like to propose a new method instead of using the symbolic link method in public html directory as proposed BEST in the accepted answer. You need to have access to your server virtual host configurations.
It is about configuring virtual host of your web server directly pointing to the repository directory. In Apache you can do it like:
DocumentRoot /var/www/html/website/your-git-repo
Here is an example of a virtual host file:
<VirtualHost *:443>
ServerName example.com
DocumentRoot /path/to/your-git-repo
...
...
...
...
</VirtualHost>
If you use github cli you can use the following command:
gh repo clone <repository> [<directory>] [-- <gitflags>...]
So for example you can run this:
gh repo clone repository-name-on-github my-local-folder
For Windows user
1> Open command prompt.
2> Change the directory to destination folder (Where you want to store your project in local machine.)
3> Now go to project setting online(From where you want to clone)
4> Click on clone, and copy the clone command.
5> Now enter the same on cmd .
It will start cloning saving on the selected folder you given .
Regarding this line from the original post:
"I know how to move the files after I've cloned the repo, but this
seems to break git"
I am able to do that and I don't see any issues so far with my add, commit, push, pull operations.
This approach is stated above, but just not broken down into steps.
Here's the steps that work for me:
clone the repo into any fresh temporary folder
cd into that root folder you just cloned locally
copy the entire contents of the folder, including the /.git directory - into any existing folder you like; (say an eclipse project that you want to merge with your repo)
The existing folder you just copied the files into , is now ready to interact with git.

Can I recover a deleted file that was never committed?

I added a directory to a repository but did not commit the change. Then I removed it, not realizing it would delete the entire directory. To be clear this is exactly what I did:
svn add directory/
svn remove directory/ --force
Is there any way to recover the file or am I SOL?
P.S. Who the hell decided SVN should have two different commands delete and remove that do the exact same thing?
There is no way, through svn, to recover a file that was not committed to svn.

Resources