Setup a new project on git and learn useful git commands

article-featured-image

Git has become an essential tool for modern software development, enabling developers to collaborate, track changes, and manage versions of their code efficiently. Setting up a new project on Git is the first step towards leveraging its power. This article will guide you through the process of initializing a Git repository for your project and introduce you to some useful Git commands that will help you manage your project effectively. Whether you're a beginner looking to learn the basics or a seasoned developer seeking to enhance your Git skills, this article has something for everyone.

Before going forward, first create an account on GitLab if not created already. In this article, I'm using Linux mint 21.3 but you can use any Linux distribution, Windows or Mac OS without any problem.

Install Git

Git installation process depends on what OS you are using. Follow the below instructions based on your OS.

For Linux:

Installing Git in Linux is very straightforward. Just run the below command:

$
sudo apt install git      #For Debian/Ubuntu linux

$
sudo yum install git      #For Redhat/Fedora linux

Executing the above based on your Linux distribution will install Git in your system.

For Windows:

To install Git in Windows OS, download the Git setup file (32-bit or 64-bit) from Git Downloads and install it in your system. After this, open Powershell and run git --version command to check Git version and verify It's installation.

Initialize Git repository

Now that we have successfully installed Git in our system, It's time to Initialize the Git repository. This remote repository will be further used to push the code. To initialize a new Git repository, use the below commands:

Initializing a new repository using Git command line required defining multiple attributes. Create a new directory and run the below commands in it:

$
git init

$
git remote add origin https://gitlab.com/protocolten/git_test.git

$
touch test_file

$
git add .

$
git commit -m "Initializing new git repository"

$
git push origin master
  • git init command will initialize a new git repository in your current directory. A new .git subdirectory will be created that contains all of the necessary Git repository files and data for tracking changes to your project.
  • git remote add origin ... command is used to add a new remote repository in the Git repository. Now you can push your code from local git to remote origin.
  • touch test_file command will create a new file named test_file in the current project directory.
  • git add . command is used to add changes to the staging area. This dot at the end is a wildcard that will add all your files in staging excluding hidden files.
  • git commit ... command creates a new commit object in git repository that contains the snapshot of the current state of the project. Keep in mind that the commit only contains changes that are in staging area. Option m is used to assign a comment on the commit.
  • git push ... command pushes the code from the local git repository to the remote origin in a new repository named git_test

Now login to your GitLab and you will see a new project repository named git_test containing all your local git project files.

Revert git commit

Reverting a Git commit is a common operation in version control. It allows you to undo changes made by a specific commit and restore the code. This is useful for correcting mistakes, and reverting to a previous state without losing the commit history. Use the below commands to perform this action:

$
git log

$
git revert <commit-hash>
  • git log command will display the git logs. Each commit is assigned a dedicated hash value. This value plays a crucial role in git reverting.
  • git revert command revert the repository back to the specified commit hash.

After performing a revert, the repository will be restored to the state of the specified commit. It will create a new commit and now you can use git push command to push this revert state in the remote repository.

Stash and pop git commits

Git stash is a powerful tool in Git that allows you to temporarily store changes that are not ready to be committed. This is useful when you need to switch branches or work on something else without committing your current changes. Later, when you want to reapply the stashed changes, you use git stash pop, which removes the changes from the stash and reapplies them to your working directory. This allows you to easily switch between different tasks without losing any work.

$
git stash push

$
git stash push -- <file-name>

$
git stash list

$
git stash show -- <stash-index>

$
git stash pop

$
git stash pop -- <stash-index>
  • git stash push command will put all files from commit into a stash.
  • git stash push -- <file-name> command will put only the specified files from commit into a stash.
  • git stash list command will display the list of all stashes along with their index number.
  • git stash show -- <stash-index> command will display all files in the specified stash.
  • git stash pop command will restore the stashed changes from all stashes.
  • git stash pop -- <stash-index> command will restore the stashed changes only from specified stash.

Usefull Git commands

In this section, we'll explore some of the most useful Git commands that every developer should know. Whether you're a beginner getting started with Git or an experienced developer, these commands will help you navigate through your Git projects with ease.

  • git branch To list the currently activated branch. Use option --all to list all available branches.
  • git checkout <branch-name> To change branch to specified branch.
  • git checkout -b <branch-name> To change current branch by creating a new specified branch.
  • git status It shows the status of Git repository like what files have been modified, files that are staged for the next commit, and untracked files that are not being tracked by Git. Any changes made in any file will be shown in Git status.
  • git clone <project-repository-address> It clones the project remote repository locally in the current directory. Make sure you have appropriate rights to access that project repository.
  • git pull <branch-name> To pull the code from specified remote branch. You might have to also use a remote origin name with this command like git pull origin <branch-name>. Most of the time developers don't change it and set it as origin but if you have used your own origin name while adding "git remote add origin", use that name instead of origin.
  • git push <branch-name> To push the code to specified remote branch. You might have to also use a remote origin name with this command like git push origin <branch-name>. Most of the time developers don't change it and set it as origin but if you have used your own origin name while adding "git remote add origin", use that name instead of origin.
  • git merge <branch-name> To merge the specified branch into current branch.
  • git merge --abort To cancle the merge and revert any conflicts.
  • git remote set-url origin <new-url> To change the remote URL of your Git repository.
  • git config core.fileMode false Usually, If you make changes in file ownership or permission, It'll reflect in "git status" as a modified file. You can prevent this from happening by using this command.
  • git checkout --theirs . Use this command to resolve the conflicts while merging branches by accepting all the incoming branch changes.
  • git checkout --ours . Use this command to resolve the conflicts while merging branches by accepting all the current branch changes.
  • git pull --no-ff origin <branch> This command pulls changes from the targeted remote branch and ensures that a fast-forward merge will not be performed. Instead, it will create a new merge commit even if a fast-forward merge is possible. This is useful for keeping a clear history of when merges occurred.
  • git reset --hard origin/<branch> It forces to reset the local branch back to specified remote origin. All changes in tracked files in the working directory are discarded. Which means any uncommitted changes in your working directory will be lost.
  • git clean -f -d This command is used to forcefully remove untracked files and directories from your working directory.
Learn Git essentials
protocolten-admin

Author: Harpreet Singh

Created: Mon 29 Apr 2024

Updated: 4 months, 2 weeks ago

POST CATEGORY
  1. Linux
  2. Windows
  3. Technology
Suggested Posts:
LINUX post image
Install Wordpress on LAMP Stack (Ubuntu)

According to a survey done by W3Techs, Wordpress is used by 41% of all …

LINUX post image
Top text processing and filtering commands in Linux

In this article you'll get to know about some of the best text processing and …

PROGRAMMING post image
Fastest method to list all Prime Numbers in Python

While writing code, most developers prefer to code less. And up to some point, …

LINUX post image
Containerization with docker

Containerization is a way of packaging an application along with all of Its required libraries, …

LINUX post image
Setup Network boot for installing OS using PXE server

Whenever you are installing a new operating system in the machine, you insert your …

Sign up or Login to post comment.

Comments (1)

2 months, 2 weeks ago

Sign up or Login to reply a comment