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
- 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
- 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.