Categories
Tools

How to use Git?

As a brief introduction, Git is a distributed version control system used to track changes in the source code of an application. This tool is designed to coordinate the work between programmers during the software development process. One noteworthy mention is that Git is a tool, whereas GitHub is a service that hosts Git repositories. Let’s jump to the commands.

HOW TO CREATE A NEW REPOSITORY – GIT INIT

The basic command for creating a new repository is git init. After running the “git init” command, Git starts tracking the changes to the folder/project where the command was run.

After running the above command, the folder/project becomes a local git repository.

GIT WORKFLOW – HOW DOES IT WORK

Before going further, let’s see how a local git repository works. The repository is made up of several “trees”, more precisely – three:

  • Working directory – which stores the project files
  • Index – which is the staging area
  • HEAD – this tree is pointing to the last commit made

When a file is modified, it is added from the Working directory to the Index tree and then to the HEAD tree.

HOW TO CLONE AN EXISTING REPOSITORY – GIT CLONE

The command git clone “downloads” a repository that already exists on a remote server. It is essential to specify that git clone creates a directory with the same name as the target repository, initialises a .git directory inside it, and it downloads all the latest data.

  • There are two ways to make a copy of an existing repository: 
  1. git clone <url>
  2. git clone username@host:<url>

The first one downloads a repository from a specified URL using HTTPS, whereas the second command downloads a repository using SSH. After running either command, you will have the repository on your machine, and you are ready to use the other Git commands.

Tl;dr:

  • git clone <url> – clone (download) a repository from a specified URL using the HTTPS protocol
  • git clone username@host:<url> – clone (download) a repository from a remote server using the SSH protocol
HOW TO ADD & COMMIT CHANGES – GIT ADD & GIT COMMIT

Once you have modified a file and want to push it to the remote repository, the first step is to use the command git add to add the file/files to the Index tree. To add only one file to the Index tree use – git add <filename>, and to add multiple files use – git add * (beware that it adds all the folders/files from the project to the staging area) or git add <filename1> <filename2> <filenameNTH>.

Once the files are modified and added to the staging area, it is essential to provide a short message that specifies what changes were done in that particular commit. That is done using the git commit command – git commit -m "Your message". After using this command, the file/files are committed to the HEAD, but they are still not in the remote repository just yet.

Tl;dr:

  • git add <filename> – add a file to the staging area
  • git add * OR git add <filename1> <filename2> <filenameNTH> – add multiple files to the staging area
  • git commit -m "Your message" – commit the changes to the HEAD
HOW TO PUSH CHANGES – GIT PUSH & GIT REMOTE ADD

After committing the changes to the HEAD tree, the files can be sent to the remote repository. The command git push origin master allows you to send the files to your remote repository. If you want to push changes to another branch, you need to change from the master branch to whatever the name of the branch is – git push origin <branchName>.

However, if you did not clone any repository and you want to push changes to a selected remote server, the command you need to use is – git remote add origin <remoteServer>.

Tl;dr:

  • git push origin master – push changes to the master branch on a remote repository
  • git remote add origin <remoteServer> – connect the local repository to a remote server

BRANCHES

Git commits to the master branch by default because that is the default branch when a new repository is created. However, there are times when we want to push changes to another branch. Also, branches are used by developers to push changes independently of each other or to develop features independently of each other. The branches are then merged back to the master branch.

How to create a new branch in your local repository? A new branch is created with the git branch <branchName>. The command creates a new branch with the specified name, but you are still in the master branch. To use the newly created branch, you need to type the following command git checkout <branchName>. However, these two steps can be done together – git checkout -b <branchName>. The command creates a new branch and switches to the newly created branch. 

After creating a new branch, you can continue the work and use all the commands presented above. To make the branch available to others, you have to push the branch to the remote repository. That can be done with git push origin <branchName>.

How to delete a branch? First of all, you have to change to the master branch with the following command – git checkout master. Then, you need to run git branch -d <branchName> , and the branch will be deleted. 

How to see all the available branches in the local repository? git branch lists all the branches available in the local repository. 

Tl;dr:

  • git branch <branchName> – creates a new branch with the specified name
  • git checkout <branchName> – switches to a branch with the specified name
  • git checkout -b <branchName> – combines the above commands into one (creates a new branch and switches to it)
  • git branch -d <branchName> – deletes the specified branch
  • git branch – lists all the available branches

UPDATING THE LOCAL REPOSITORY – GIT PULL 

When multiple programmers are working on a repository, there will be constant changes made to the code. However, the local repository does not update by itself. As a result, it is essential to run the git pull command all the time before making changes to the code.

The git pull command pulls (downloads) the latest changes from the remote repository into the local repository. If you make changes without pulling first, you might run into issues (that is when multiple people are working on the same code).

MERGING CHANGES – GIT MERGE

When we want to bring back all the code from a specific branch to the master branch, we need to use the command git merge. First of all, we need to change back to the master branch (git checkout master), and then we need to run git merge <branchName>. These two commands should be enough to merge the code from a specific branch to the master branch. 

However, there are situations when there will be conflicts between the remote and local repositories. A useful command in such cases is the git log command. You will have to merge the conflicts manually by editing the files that cause conflicts and mark the edited files as merged with git add <fileName>.

Tl;dr:

  • git checkout master – change the branch to the master again
  • git merge <branchName> – merge the code from a specific branch to the master branch
  • git log – shows repository history
  • git add <fileName> – mark the edited file/files as merged

REPLACE LOCAL CHANGES 

There are cases when something goes wrong, and you want to replace the changes you have made on your local repository. Thankfully, that can be done using the command git checkout -- <fileName>. This command replaces the changes in your working directory (working tree, see this subheading – “GIT WORKFLOW – HOW DOES IT WORK“) with the latest files from the HEAD tree. It is important to mention that the changes you have added to the INDEX tree, alongside new files, are kept.

Do you want to drop all the local changes and commits? You need two commands: git fetch origin and git reset --hard origin/master. These two commands fetch the latest history from the server and drops all the changes and commits.

Tl;dr:

  • git checkout — <fileName> – replaces the changes made 
  • git fetch origin & git reset --hard origin/master – drop all local changes and commits

THE END

There are countless tutorials online about Git, and a lot of them are better than my article. However, I wanted to test my knowledge and to make it as simple as possible. Maybe a beginner (just like me) will stumble upon this and might learn a thing or two. I have checked my knowledge against the articles mentioned below, and I have also taken inspiration from them. Therefore, for a better understanding and more details, I would suggest visiting those links. 

If I made a mistake or if you want to add more, leave a comment below!

SOURCES

  • https://stackoverflow.com/questions/13321556/difference-between-git-and-github
  • https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository
  • https://rogerdudler.github.io/git-guide/
  • https://www.freecodecamp.org/news/what-is-git-and-how-to-use-it-c341b049ae61/

Leave a Reply

Your email address will not be published. Required fields are marked *