Categories: Tools

Keep your code safe through repositories

To make sure you will never lose your code, store your project on an online repository hosting service. No matter if you already have some work in progress on your local machine, or you are starting the project from scratch – backup your code online as soon as possible.

You are starting the project from scratch

Log in to your account on Github/Bitbucket. Create a new remote repo – check out the documentation for GitHub or Bitbucket to learn how to do this. Find the link to your repository, generally it is placed with the Clone option – once again visit GitHub or Bitbucket docs if you need to. On your local machine go to the directory where you store all your projects, eg.:

$ cd ~/projects/spring_boot

and clone the link to your new remote repo:

$ git clone full_link_from_bitbucket_repo

This command will create in your current folder the project directory that is already linked to the remote repository and named after it.

Whatever files you add there, will be tracked by git unless you list them in the .gitignore file. It keeps information about which files should be ignored by git during commits. Usually you will put there things that are sensitive – like passwords or API keys; wouldn’t be useful to other collaborators – like IDE files; and assets – like minified js/css files and external libraries.

You already have project files on your local machine

Create a new repository using a repository hosting service you prefer. Check out the documentation on how to set up a repo with existing project files on your local system for Github or Bitbucket.

Initialize the repository in the main directory of your project:

$ cd ~/project/spring_boot/my_awesome_project/
$ git init

and check its status:

$ git status

You will see all files in the project marked red – git recognizes them as new but does not track them yet.

Specify in .gitignore those which you wish to keep untracked. If you check the status again, they won’t be listed any more.

To keep track of all the others, add them to git:

$ git add -A

Another $ git status  shows all tracked files marked green. Now you can make your first commit:

$ git commit -m "Initial commit."

Find the link to your remote repository, it is usually placed with the Clone option – check out docs for cloning a repo in GitHub or Bitbucket if you can’t locate it. Now you can push the project to your online repo binding the local master to the remote one using -u option:

$ git remote add origin full_link_from_bitbucket_repo
$ git push -u -f origin master

Setting the upstream with the -u option allows git to track the changes between remote and local repo, thanks to that you can later use $ git pull  and $ git push  without specifying the branch.

What is next?

You will need more branches than one. For now create the develop branch, alongside the master:

$ git checkout -b develop
$ git push -u origin develop

Now you can list all your branches

  • stored locally:
$ git branch
  • stored online:
$ git branch -r

Remember to commit small, commit often and regularly push to your remote storage!

Photo by Autumn Mott on StockSnap

little_pinecone

Recent Posts

Simplify the management of user roles in Spring Boot

Spring Security allows us to use role-based control to restrict access to API resources. However,…

3 years ago

Create a custom annotation to configure Spring Boot tests

A custom annotation in Spring Boot tests is an easy and flexible way to provide…

3 years ago

Keycloak with Spring Boot #4 – Simple guide for roles and authorities

Delegating user management to Keycloak allows us to better focus on meeting the business needs…

3 years ago

Keycloak with Spring Boot #3 – How to authorize requests in Swagger UI

Swagger offers various methods to authorize requests to our Keycloak secured API. I'll show you…

3 years ago

Keycloak with Spring Boot #2 – Spring Security instead of Keycloak in tests

Configuring our Spring Boot API to use Keycloak as an authentication and authorization server can…

3 years ago

Keycloak with Spring Boot #1 – Configure Spring Security with Keycloak

Keycloak provides simple integration with Spring applications. As a result, we can easily configure our…

3 years ago