Replacing Git files for new ones - java

I have created an Eclipse project from an existing Git repository.
It contains some sample documents and code, but i find it easier to delete them and start from scratch.
Will the old documents be deleted in the repository without any trouble the next time i do commit and push?

Follow the following steps :
NOTE : origin here refers to your remote project location.
Step 1 : From command line navigate to project directory where your working
Step 2 : In command line run the following commands
git pull origin master /* Just to make sure you will get all the latest files from remote repository where you have hosted your project */
git rm * /* To remove all the files from you current directory */
OR
git rm file1.txt file2.txt /* to remote specific files */
git add . /* Add the deleted files for staging */
git commit -m "Removed obsolate files" /* Commit the deleted files into your local repository */
git push origin master /* Now push to the remote repository all the changes that you have done, all the deleted files will be now removed from remote repository */
Step 3 : Now you can start working with your fresh files

If you want to delete a file, use git rm ${fileName} (in case you want to delete in only in git, but keep it locally, use the --cached flag. After that, once you commit and push, the changes will be available in the repo.

Related

Git manage environment specific configuration

I have a requirement to have a property configuration for different environment like dev, uat and production. For example a config.properties having and entry like environment=dev, this I need to change for staging branch as environment=uat and for master branch as environment=prd .
I tried to commit these files in each branch respectively and tried adding config.properties in gitignore so that it will not consider in next commits.
But git ignore not getting updated so I ran command
git rm -rf --cached src/config.properties
git add src/config.properties
git commit -m ".gitignore fix"
But this command is deleting the file from local repository itself and the proceeding commits also deleting from branches. I want to handle the branch as such so as Jenkins will do the deployment without editing config file manually. I am using fork for git UI. Is there any way to handle this kind of situation?
You should not version a config.properties (git rm is right), and ignore it indeed.
That way, it won't pose any issue during merge.
It is easier to have three separate files, one per environment:
config.properties.dev
config.properties.uat
config.properties.prd
In each branch, you would then generate config.properties, with the right value in it, from one of those files, depending on the current execution environment.
Since you have separate branches per environment, with the right file in it, you can have a generation script which will determine the name of the checked out branch with:
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
That means you could:
version only a template file config.properties.<env>
version value files named after the branches: config.properties.dev, config.properties.uat...: since they are different, there is no merge issue when merging or switching branches.
Finally, you would register (in a .gitattributes declaration) a content filter driver.
(image from "Customizing Git - Git Attributes", from "Pro Git book")
The smudge script, associated to the template file (package.json.tpl), would generate (automatically, on git checkout) the actual config.properties file by looking values in the right config.properties.<env> value file.
The generated actual config.properties file remains ignored (by the .gitignore).
See a complete example at "git smudge/clean filter between branches".

Access a Java variable from Jenkins

I am trying to find out if I could get the value of a variable declared inside a Java program or maven-plugin and store it in a Jenkins environment variable.
This is because the Jenkins file has to create a new git branch from dev and call it Release-9.0.86 for example but the version number is inside a pom.xml.
I have already written a maven-plugin that retrieves the version from the pom.xml an writes it into another XML file, but I need to know if I could send the version to Jenkins to handle it.
You could perhaps create a properties file instead of a xml file with the needed information. Example contents of created branch-name.properties file:
NEW_BRANCH_NAME=Release-9.0.86
Then you can add a build step "Inject environment variables" after the one that produces the properties file, and configure that to read the properties file you just created.
Properties File Path: branch-name.properties
After that you can use the environment variables as usual in the jenkins build, for example a "Execute shell" build step that creates the branch:
git branch ${NEW_BRANCH_NAME}
To then push the newly created branch you could add a "Git Publisher" post-build action.
Branch to push: ${NEW_BRANCH_NAME}
Please note that it might be useful to configure Git Publisher to "Push Only If Build Succeeds" i.e. branch will not be created if the build fails for any reason.

CVS cannot download project to a specified directory

I'm trying to checkout a project from my local cvs repository to a location. Why is this giving this error? Am i not using the options properly?
ne#ne3:~/JarTester$ cvs -d:pserver:uname#localhost:/home/uname/cvsrepo/ co -P MavenTestApp /home/ne
cvs [server aborted]: Absolute module reference invalid: `/home/ne'
cvs [checkout aborted]: end of file from server (consult above messages if any)
I can use cd but i have to run it in java. So cd is not in consideration.
Thank You
If this is your first cvs checkout, create a folder in Windows Explorer to hold all of your cvs project folders. Then create a subfolder for this project. (You may even want to create separate subfolders for each module if you're working in more than one.)
In Cvs, select Cvs Admin - Login and enter your cvs password.
Click on the left window in the program and select a folder. Then select Cvs Admin - Checkout Module. Select the project folder you created earlier.
Enter the project module name and click OK. You should see a scrolling list of filenames as these are created in your folder(s).
Repeat the module creation process for each additional cvs module you wish to check out.

Git repository - switch project local repo and push it to existing git repo

Lets say i have inited new repo onto my eclipse workspace project "Myproject". I have created a user and a repository to bitbucket.org/myuser/MyRepoHoldingMyProject. Now unfortunatelly i messed up "Myproject" and i created a new project "MySecond" . What is the best way to push "MySecond" to MyRepoHoldingMyProject, to overwrite its files. Do i have to delete the repo and make a new one, and then init "MySecond" ? Or will it work when i just repleace the content and name of the "Myproject" ?
It would be a good idea to rename your remote repo to other name e.g. MyRepoHoldingMyProject_backup and create a new repo MyRepoHoldingMyProject.
Doing so you will not lost commit history or may be some valuable code.
After that just:
git init in MySecond project root directory, if you have not done it yet
git git remote add origin link_to_new_repo
git add -A check with git status which files were added
git commit -m 'Initial commit'
git pull
git push

Is it possible with Git to commit a file in a specific folder in the repository?

I'm new with git and I'm trying to use JGit in a java application. I would like to know if Git has a 'commmit of files into a specific directory' or is Git only based on the working tree where files are stored?
For example, let's say I have a file saved in C:/tmp/myfile.docx. Is it possible to tell Git to store it in the repository as /myProject/myfile.docx ?
Thank you :)
That is not possible. Think of your Git repository as a time machine for a tree of directories. You can only add files to git that are inside that directory tree. And don't try to make C:\ a Git repository... :)
So, let's say your Git repository resides at C:\tmp\myRepo. To add your file to the repository you'd do this:
move myfile.docx to C:\tmp\myRepo
add the file to Git: (using Unix style command line) cd C:/tmp/myRepo && git add -A
create a commit: git commit -am "added myfile.docx"

Categories