Some most useful Git commands

rixan4h4z8y94eq89som.png

Before Diving into knowing Git commands , Let's understand the concept of Git first .

Git is a change tracking mechanism for your source code . It tracks whatever changes you do in your source code and you can commit those messages with the help of various commit messages . Git is an important part of daily programming (especially if you're working with a team) and is widely used in the software industry.

Let's now understand how is it different from GitHub . For an instance , Think GitHub as a Google photos and Git as your mobile's gallery . When you click pictures from you camera it gets automatically saved in your mobile's gallery (as you do changes in your source code and Git keeps track of it and let you save ) and you have full privacy of your gallery , it's only you who can see your photos , but when you upload those photos public - in google photos or somewhere you are actually sharing with the public and letting them collaborate on those pictures , or make changes like filtering etc (similarly GitHub allows you to share your code with the general public , other devs and let them collaborate , download or make changes on your project / code.

We can access and make use of Git and GitHub from the desktop App itself but It good to have practice on Git Commands as it's useful while working in industry .

Okay! so folks , Here are some Useful Git Commands which are mostly used . Let's go through them one by one -

Note: To understand this article, you need to know the basics of Git.

1. Git Clone

Git clone is a command for downloading existing source code from a remote repository (like Github) . Git clone actually makes a exact same copy of the latest version of a project in a repository and saves it to your computer and then you can modify , make changes from your code editor .

It can be done in both ways i.e through GitHub as well as Git commands . But mostly I prefer the clone with https way:

git clone (name-of-the-repository-link)

For example, if we want to download a project from GitHub, all we need to do is click on the green button (clone or download), copy the URL in the box and paste it after the git clone command that I've shown right above. And then this will make a copy of the project to your local code editor and you can start working on that ...

image.png

2. Git Branch

Branches are most important in the Git . Branches are mostly used when developers are working in a team in the industry . With the help of branches , several developers can work on the different features of the same project simultaneously . The Git Command for creating, listing and deleting branches can be done as follows:-

Creating a new branch (This command will create a branch locally)

git branch (branch-name)

Pushing the branch into the remote repository

git push -u (remote) (branch-name)

Viewing Branches

git branch or git branch --list

Deleting a branch

git branch -d (branch-name)

3. Git Checkout

When devs work in a particular branch, you need to switch to it firstly. We use git checkout mostly for switching from one branch to another. Also use it for checking out files and commits. We use below command :

git checkout (name-of-your-branch)

Make sure that:

  • You have committed the changes done in your current branch and have stored safely before you begin to switch ..
  • The branch you want to check out should exist in your local.

4. Git status

To get all the necessary information about the current branch or to know its status , we use Git status command...

what type of information we can gather? -

  1. Whether the current branch is up to date
  2. Whether there is anything to commit, push or pull
  3. Whether there are files staged, unstaged or untracked
  4. Whether there are files created, modified or deleted

Command used is:

git status

5. Git add

When we create, modify or delete a file, these changes will happen in our local and won't be included in the next commit.

To add a single file

git add (file)

To add everything at once

git add -A

you will see that there are file names that are red - this means that they're unstaged files. The unstaged files won't be included in your commits. And the files with green are now staged with git add.

please note that the git add command doesn't change the repository and the changes are not saved until we use git commit.

6. Git Commit

Most used Git Command , Git is used to track the changes done in the source code and to commit those changes we use commit messages.

Git commit is like setting a checkpoint in the development process which you can go back to later if needed.

git commit -m "commit message"

Note that Git commit saves your changes only locally.

7. Git Push

Once you have Committed the changes of source code in Git , The next thing you have to do is to show / push your changes to remote server. Git push - shows your commits to remote repository..

git push (remote) (branch-name)

But if you have some new branch , you also need to upload that branch with the below command :- However , Git push is only used to push the commits to remote repository.

git push -u origin (branch_name)

8. Git pull

The next one is Git pull , It is used to get updates from the remote repo. This is a mixture of a git fetch and git merge , like when we'll use git pull it fetches the update from remote repository i.e using git fetch and then immediately it applies / merges the latest changes in your local i.e using git merge. However some conflicts may arise but you need to solve them manually. And the command we use to git pull is:-

git pull (remote)

9. Git revert

Git revert is used to undo the changes that we've made. The changes can be reverted back by both remotely as well as locally . However before reverting , it's good to see the commit history to avoid any unwanted situation .

To see our commit history , we need to use the following command :-

git log -- oneline

Then we just need to specify the hash code next to our commit that we would like to undo:

git revert 3321844

The Git revert command will undo the given commit, but will create a new commit without deleting the older one and that's one of the advantages of git revert that it doesn't touch the commit history and you can see all the commits in your history even the reverted .

Git revert is safer to use and is the preferred way to undo our commits because all the changes takes place in our local system unless we push them to the remote repo.

10. Git Merge

When each developer is done with the implementation of the given feature in their respective branches and everything seem to be working fine / implementation is done right . The final step is to merge their branches with the parent branch i.e main or master with the help of a git merge command ..

Git merge helps to integrate the feature branch to the main project branch (master / main) . It is necessary to be on the main project branch which has to be merged with feature branch .

Let's understand it practically . Suppose you need to merge your feature branch with the main project branch so for doing that , you need to switch to the main project branch . The command to switch is :-

git checkout main

then , before actually merging both the branches ,you should update your local main branch and the command for that is :-

git fetch

Now after updating , you can finally merge your feature branch to the main branch :-

git merge (branch-name)

However , there can be some conflicts after merging various branches to the main project branch , but that can be resolved manually .

That's it folks for this blog . Hope you learnt something new today in a detailed and easy way :) Want more of the learnings ? I keep writing blogs whenever I learn something more interesting related to web development , open source , programming . So stay tuned for that :)

cheers :)

#Github #gitcommands #opensource #usefulgitcommands

Did you find this article valuable?

Support Anjali Rohira by becoming a sponsor. Any amount is appreciated!