Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Git fetch
2.1.
Implementation
3.
Git Pull
3.1.
Implementation
4.
Git Push
4.1.
Implementation
5.
Git Remote
5.1.
Use cases: 
6.
FAQs
7.
Key Takeaways 
Last Updated: Jun 26, 2024
Easy

Git Local Commands

Author GAZAL ARORA
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Git commands are really helpful when you are working in a team to develop a project. In this blog, we will discuss the basics and working implementation of the following commands: 

  1. git fetch
  2. git pull
  3. git push
  4. git remote 

Git fetch

  • The git fetch command downloads commits, files, and references from a remote repository to your local repository. The retrieved metadata is stored in the .git directory, while the working directory is left unaltered.
  • Before accepting any changes to your working directory, git fetch allows you to review the code (pushed by other developers). The review procedure aids in the avoidance of merge conflicts.

Implementation

  1. Create a new directory on your local machine.
mkdir <directory_name>

 

2. Enter the directory.

cd <directory_name>

 

3. Initialize the local repository as a git repository. 

git init

 

4. Link the local repository to the remote repository via the remote URL. 

git remote add <remote_name> <remote_url>

 

NOTE: The command takes various URL forms depending on the platform and protocol. In most cases, the URL is provided by the remote host.

 

5. Confirm if the remote is added successfully.

git remote -v

 

6. Use the git fetch command to download all the content of the remote repo to your local.

git fetch <remote_name>

 

Example implementation: 

NOTE: The working directory remains intact. To fetch a specific branch, use:

git fetch <remote_name> <branch_name>

 

  • To reflect the changes downloaded from the remote onto your local repo, use:
git merge <remote_name>/<branch_name>
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Git Pull

  • Git "pull" gets changes from the remote server and merges them into your working directory. To pull a repository, use the git pull command.
  • The pull command is used to fetch changes (commits) from a remote repository and store them locally. It synchronizes the local and remote-tracking branches. Remote tracking branches have been configured to push and pull data from a remote repository. It's just a combination of the fetch and merges commands. It begins by retrieving changes from a remote repository and combining them with the local repository.

 

git pull = git fetch + git merge 
 

Implementation

  1. Create a new local branch
git checkout -b <branch_name>

 

2. Set the newly created branch to track the remote(origin) branch.

git branch -set-upstream-to=<remote_name>/<remote_branch_name> <local_branch_name>

 

3. Pull files/commits from the remote server

git pull

 

Example implementation:

Git Push

  • The git push command can be used to send (or push) commits from your local branch to a remote Git repository.
  • You must commit all changes to the local repository before pushing to the remote repository.

 

Implementation

 

  1. Create a new file and add some text to it.
echo "<text_you_want_to_add>" >> <new_file_name>

 

2. Move that file to the staging area

git add <file_name>

 

3. Commit the changes.

git commit -m "<commit_message>"

 

4. Push the changes to remote

git push

 

Example implementation:

 


 

After pushing to remote, the changes are reflected in the Github repository.
 

Git Remote


Although we already have previously used the "git remote" command to add/confirm the connection between the remote and local repositories, we will discuss the command more briefly. 

The "remote" command assists in the management of remote repository connections.

It not only allows you to see which remotes are currently connected, but it also allows you to add new connections and delete old ones.

Use cases: 

git remote add <shortname> <remoteURL>

 

add: This function establishes a new connection to a remote repository. The "shortname" you specify can be used instead of the URL when referring to the remote. The default shortname is "origin," which refers to the remote repository from which your local repository was cloned.

git remote -v

 

verbose: When listing your current remote connections, it displays URLs for remote repositories. You only see their shortnames when you list remote repositories (e.g., "origin"). You can also view the remote's URLs in listings using the "-v" option.

NOTE: the working example of the above two commands is shown in the previous section (git fetch). 

git remote remove <shortname>

 

remove: The remote is disconnected from the local repository. It's worth noting that this has no effect on the remote repository itself (i.e., the repository isn't removed/deleted/etc.).

No remote URLs are displayed because we are disconnected from the remote. 

Check out most important Git Interview Questions here.

FAQs

1. What is the difference between git fetch and git pull commands? 
Git retrieves any commits from the target branch that isn't in your current branch and stores them in your local repository when you fetch. It does not, however, merge them into your current branch. On the other hand, pull merges them automatically without letting you see the commits first.


2. Does Push push all branches?
No, git push just transfers commits from the current local branch to the remote branch given in the command.

Key Takeaways 

This blog discussed some useful git commands: fetch, pull, push, and remote. We also saw the code implementation and gained insight into how they work. 

You can check out this material to learn more about git commands and full-stack development. 

Recommended Reading:

wget command in linux

Explore more!

You can use Coding Ninjas Studio to practice various DSA questions asked in different interviews. It will help you master effective coding techniques, and you will also get interview experiences from people working in big companies.

Live masterclass