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:
- git fetch
- git pull
- git push
- git remote
- 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.
- Create a new directory on your local machine.
2. Enter the directory.
3. Initialize the local repository as a git repository.
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>
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>
- 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
- 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
- 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.
- 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
After pushing to remote, the changes are reflected in the Github repository.
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.
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.
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.
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.
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.