Git for beginners: Git basics you need to know

·

2 min read

Git for beginners: Git basics you need to know

This is the continuation of article Git for beginners : setting up your first project.

In this article, you will learn how to push your first code to the repository you created. Also, you will learn about pull, add files, branching and stuff.

1. Initiating a new repository

Open the repository in your local machine and perform

$ git init --bare

2. Add , commit and push

To push the code, you need to add the files first using

$ git add * # to add all files

$git add <filename> # to add specific file

Now commit these changes using

$ git commit -m "Commit message"

It is good practice to write commit messages in Imperative mood, for example " add awesome code" or " Update the image" More on this will be written in next posts.

Now push your changes using

$ git push # to push to current branch

$ git push origin <branch> # to push to <branch> in the remote repo

3. Adding origin

If you created the code locally and did not clone in the beginning, you need to do

$ git remote add origin <server>

to connect to the remote repository

4. Branching

Branching is necessary if you need to add some feature to current code base. This keeps the current code always in same state, and when your branch is working you can merge it to the main branch.

$ git checkout -b feature_1  # if you want to start a new branch 

$ git checkout  feature_1  # if you want to checkout branch

to push the branch to remote repository, so that it is available for others in the team

$ git push origin <branch>

5. Update and merge

To get the changes made to the branch by someone else

$ git pull

if you want to merge your feature branch to main branch, checkout the main branch and

$ git merge feature_1

and do a git push to push the changes.