Github Tips

Github Tips

Learn the efficient way to start standard projects and push it to Github/Gitlab

This article is for beginners as well as for the experts 😉, as this is going to help you to avoid all the pitfalls that could lead to a failure state.

Here, you will learn efficient ways to start the projects and push them into Github/Gitlab.

Let’s see what we are going to learn in this article:

  1. Synchronize Local with Remote

  2. Working on Branch

  3. Status of your work

  4. Land your work

Setup Git

Make sure to configure the user information which will be used across all repositories.

Set the user name: $ git config --global user.username

Set the Email Address: $ git config --global user.email

Once you have done the setup, will be able to push the changes into github.

1. Synchronize Local with Remote

It is important to always ensure that you have the repository on your local machine. There are two things that can happen:

A. You can create your own repository

  1. Go to the repository directory

     $ cd project-name
    
  2. Initialize the git

     $ git init
    

B. If you are working from another repository

  1. To clone the repository locally.

     $ git clone [git@github.com](mailto:git@github.com):suprabhasupi/suprabha.me.git
    

    Yuppy! Now have cloned the repository locally.

2. Branch

Git branches are effectively a pointer to a snapshot of your changes. You can create a new branch anytime when you have to work on any feature, fix, or any chore issues.

Why do you need to create a branch?

You create a branch to ensure that main the branch should always has a production-ready code. You can create a new branch by the following command:

$ git branch branch_name

The above command will create a new branch. So to confirm the branch names you can enter the following command:

$ git branch

main
branch_name

To switch from main to branch_name, or any other branch:

$ git checkout branch_name

If you want to create a new branch and switch to it, you can use the following command:

$ git checkout -b branch_name

The above command will create a new branch and switch to branch_name the branch.

3. Status

When you want to track the work you've done, such as which files you've updated Here is the command to check the status of your work:

$ git status

4. Land your work

Let’s add, commit and push your work into the remote repository.

Add a single file: $ git add file_name

Add all files: $ git add .

Commit your code with valid message: $ git commit -m “initial commit“

Push your work: $ git push origin branch_name

To check your commit log: $ git log

Merge local_branch to another_branch: $ git merge branch_name


Here are a few tips that can help you save time when working with Git:

  1. Let’s say you deleted a file a month back, but one day you needed it. How can you get the latest state of the file which has been already deleted?

    Ans: This is the command which helps you to find the commit log of that particular file:

     $ git log --full-history -- ${file_path}
    

    The above command finds the commit ID that deleted the file. After checking the commits, and backed up one more by entering the below command:

     $ git checkout HEAD~1
    

    This loads the file right before it was deleted 🙌

  2. Resolve Conflict

    Let’s say, you took a pull from the main branch and you had a conflict. There are a few commands which will help you in terms of fixing or aborting the conflict.

    This command will help to see a list of commits that caused conflict

     $ git log --merge
    

    Once you fix the conflict, make sure to commit and push.

    The below command helps to exit from the merge process and back to the original state.

     $ git merge --abort
    
  3. How to check staged changes of any tracked files. Let’s say you have added the file and now you wanted to check out the changes. This is the following command you can use:

     $ git add file1.jsx
     $ git diff --cached file1.jsx
    
  4. Do you know you can store temporary commits as well in local?

    Yes, you can stash your changes and when you want those changes back you can get them back.

    Check out these important commands:

    To save temporary the updated changes: $ git stash

    To check the list of stash stack: $ git stash list

    Get the last stash changes from stash stack: $ git stash pop

    Delete the last stash changes from the stash stack: $ git stash drop

Thanks for reading the article!

🌟 Twitter

📚 Ebooks

🌟 Instagram

Did you find this article valuable?

Support Suprabha's Blog by becoming a sponsor. Any amount is appreciated!