Very Basic Git Commands

Here is a list of some very basic Git commands. They are very useful for Git beginners.

Initializing:

Use the below command to initialize Git repository,

git init

After running the above command the .git folder will be created inside the current directory.

Then, use the below command to clone the files from remote server to your local

git clone username@host:/path/to/repository

Here, the example Git repo

git clone git@github.com:yourusername/sample-repository.git
--- or ---
git clone https://github.com/yourusername/sample-repository.git

Now, your local copy will be created in the folder named sample-repository.

If you want to clone the files with any other specific folder name instead of sample-repository, add the folder name as follows,

git clone git@github.com:yourusername/sample-repo.git MyFirstClone

Now, your local copy will be created in the folder named MyFirstClone instead of sample-repository.

Setting Your Details:

Sometimes you may need to configure your name and email on Git. Use the below command to check which name and email address is associated with Git

git config user.name
git config user.email

If you want to update the name and email address, use the below command,

git config --global user.name "Thomas Raj"
git config --global user.email "thomas@example.com"

Here, –global is used to set the configuration for all projects.

Git Branches:

The git branch command lets you create, list, rename and delete branches.

git branch
--- or ---
git branch --list

The above command used to list all of the branches in your local repository.

The below command used to list all remote branches.

git branch -a

To create new branch use the below command

git branch my_new_branch

To delete the specified branch

git branch -d branch_name_to_delete

This is a safe operation in that Git prevents you from deleting the branch if it has unmerged changes. Suppose if you want to delete the specified branch, even if it has unmerged changes, use the below command

git branch -D branch_name_to_delete

To rename the current branch name, use the below command

git branch -m new_branch_name

If you want to switch to another branch

git checkout another_branch_name

Working On Your Repository:

The below command is used to check what are all the files have been changed or added or removed in the local repository.

git status

Git has the following three areas,

  • First one is our working directory
  • Second one is Index which is act as a staging area.
  • The third one is Head.

By using the git status command we can know about the changes that what we have done in our local repository. Then we have to upload the local changes to the remote server. Follow the below steps to upload the local changes to the remote server.

git add this command is used to add the modification from our working directory to Index area.

To add all the files

git add -A

To add a single file

git add filename

To add a specific folder

git add foldername/

git commit this command is used to move the changes from Index (staging area) to Head. But the Head is still in your local repository.

git commit -m "Your commit message"

Finally, you have to use the below command to push all the local modification to your remote server.

git push origin master

Hope this helps you.

Leave a Reply

Your email address will not be published. Required fields are marked *