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.:
1 |
$ cd ~/projects/spring_boot |
and clone the link to your new remote repo:
1 |
$ 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:
1 2 |
$ cd ~/project/spring_boot/my_awesome_project/ $ git init |
and check its status:
1 |
$ 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:
1 |
$ git add -A |
Another $ git status shows all tracked files marked green. Now you can make your first commit:
1 |
$ 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:
1 2 |
$ 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
:
1 2 |
$ git checkout -b develop $ git push -u origin develop |
Now you can list all your branches
- stored locally:
1 |
$ git branch |
- stored online:
1 |
$ git branch -r |
Remember to commit small, commit often and regularly push to your remote storage!
Photo by Autumn Mott on StockSnap