7 Git Commands You Need to Know to Collaborate on Projects
Collaborating on open source projects can be fun but you need to know how to use version control systems like Git for a smooth project completion.
7 Git Commands You Need to Know to Collaborate on Projects
Image by vectorjuice on Freepik
As the world begins to adopt the culture of working remotely, now more than ever, it is crucial that you understand how to work with version control systems like Git, as it is now a major requirement for employment in tech roles. It has become important because it allows developers and creatives in the tech industry to collaborate on projects without having to be in the same room or office.
As a developer, you will have to work on projects with others in your team, and you must understand how to use Git as a collaborative tool. Part of this understanding is knowing how to use some basic git commands that will prevent your project from having conflicts.
This guide will teach you seven basic Git commands you need when working collaboratively on a project. To perfectly understand this guide, you must have a foundational knowledge of Git and GitHub.
1. Git Branch
git branch <branchname>
The git branch command creates a new branch in a working tree. This command is especially important in collaborative projects where multiple people may work on the same features or files. To avoid destroying the main or master branch, each team member can create a separate branch to which they make changes. Whoever is in charge can decide what commits will be merged with the default branch.
To use this command, you must add the name you want to give the branch. After creating this branch, it is important to set the upstream of the branch with the command.
git branch -u origin/<branchname>
Or you can create an upstream while you push.
git push -u origin <branchname>
Note: In this case, -u is equivalent to --set-upstream.
To delete a branch, use the command below;
git branch -d <branchname>
2. Git Checkout
git checkout <branchname>
With the git checkout command, you can switch between branches in your repository, and it can be used to create new branches by adding the -b option followed by the branch name. It is important to remember to use git checkout to switch to the created branch because the git branch command does not automatically change your working branch. Also with this command, you can create a branch, set the upstream, and switch to the created branch.
git checkout -b <branchname>
You can think of the command above as a shorthand way of creating a branch and switching to it.
3. Git Fetch
git fetch
You use git fetch to download files and commits from a default branch to your working branch. With git fetch, these commits or files are not automatically integrated into your repository, and it is safer than using a git pull. A git fetch command is used with the git merge command to incorporate changes from the remote to the local. To fetch from a specific branch, you must use the command;
git fetch origin <branchname>
You can also use the command below to fetch the commits and files from all the registered branches.
git fetch --all
Git fetch is important if you need to see the changes made to the remote repository without having those changes in your local repository.
4. Git Merge
git merge
After carrying out a fetch command on your terminal and checking out the changes you want to download to your local repository, merging the files is next. To merge the files, you use;
git merge <branchname>
This command will merge changes from the specified branch into the current working branch.
Another way to do this is to use the command below;
git merge <branchA> <branchB>
This updates BranchB with the contents of BranchA–making them identical.
However, suppose a single file has been changed on both branches; in that case, there will be a conflict, and automatically merging these files will not work. You will have to correct those conflicts manually. After resolving the conflicts, you need to add the file, commit and merge again.
5. Git Pull
git pull
Git pull performs both the Git fetch and git merge commands simultaneously. It downloads the changes from the remote branch into the local branch. You must be careful while using this command, as it would completely override changes you made to your local branch.
git pull origin main
The above command will pull changes from the main branch of the remote repository. Always remember to commit your changes before pulling, or you will get an error as the files will not merge with your local repository.
6. Git Restore
git restore
Another useful command for collaborating on projects, git restore, is used to revert back to a previous commit state. Say you were working on a file, pulled from the remote repository, and discovered unwanted changes had been made to that file; you can use a git restore to go back to a previous commit.
git restore <filename>
You can also restore multiple files at once with;
git restore <filename> <another-filename>
7. Git Push
git push
Now you know how to switch and delete branches, fetch from a remote repository, merge conflicts, and successfully pull from a repository. You are practically a pro already. All that is left is to be able to push your files successfully to GitHub.
git push origin <branch name>
The above commands will only push commits from the specified branch to the remote repository.
git push -f
With the -f option, git push overwrites everything in your remote repository and replaces it with what is on your local computer. You must be sure that you do not have important files in your remote repository that should not be deleted.
Summary
With the commands above, you should be able to work comfortably with teams on various projects. Although these commands may seem numerous, with the many available options to combine the commands with, you do not need to memorize all of these commands. Regularly practicing the use of these commands will prepare you to work on any collaborative projects.