No comments yet

Useful Git commands

Version control refers to the process of saving different files or different ‘versions’ of files throughout the various stages of your project. This enables developers to keep track of work or rollback the work (return to a previous phase) if they decide they don’t want to commit the changes.

What is GIT? 

Git is a distributed version control system. It gives flexibility to developers to work independently on the latest copy of code, keep track of multiple versions of code, compare the code, commit or rollback the code etc.

The way GIT works is a bit different than other VCS. Unlike most other Version Control Systems (VCSs), git stores each saved version as a ‘snapshot’ instead of a list of changes made to each file. You can reference old snapshots whenever you need to, and new snapshots are created when your project is modified. You can stage files before committing.

Git also enables you to ‘push’ and ‘pull’ changes to and from on other computers. This makes it what is known as a ‘Distributed Version Control System’ and enables multiple developers to work on the same project.

What is GITHub?

Sometimes people get confused with these two: Git and GitHub, initially. GitHub is a platform that can hold repositories of code in cloud. It allows multiple developers to work and contribute on a single project. GitHub repositories are publicly available and developers from any location can interact and contribute to improve each other’s code as needed.

Git vs GITHub

Git is a local version control system (VCS) that enables developers to save snapshots of their projects. It’s mainly for individual use. 

On the other hand, GitHub is a web-based platform that uses git’s version control features so they can be used collaboratively. 

Git and GitHub both work together but on the contrary, git is an open source software, while GitHub is owned by Microsoft. Git is free tool and GitHub has different pricing model depending upon the need and features.

Though GitHub provides a simple UI tool for making changes but most of the developer prefer to work locally in IDE and use git command to interact with GitHub. In the other blog, we will use Visual studio code (VS code with Salesforce) for the same in this tutorial. 

Key Terms:

  1. Repository: A collection of source files for your project.
  2. Branch:  Every repository has a default(master/main) branch, which should be production ready code or production code. For any work , whether it’s experimental work or enhancement work developer needs to create branches for their work. 
  3. Commit: In our project work, we either add new file or change existing file or remove existing file. We commit the changes to indicate that point of change.
  4. Push: Push the changes to remote (GitHub) from local.
  5. Pull: A commit request which you are requesting to be merged into the default branch. 
  6. Merge: The combined history of two or more branches. Most of the time, you’ll merge your feature branch into the default or deployed branch of the repository in order to move the features into production
  7. Tag: A pointer to a specific commit, which provides a persistent reference to an event. Typically, tags are used with semantic versioning to represent points when your application was released

Install Git

Before we do some hands-on exercise, we need to install Git. Use below link to download and install GIT.

Link https://git-scm.com/download/win

Basic Git commands which every developer should know

Local Environment Setup before using Git

We should set some configuration before using git commands like user information. To set user name for local git repository use below commands

git config --global user.name "Apex Hours"

To set user email for local git repository use below commands.

git config --global user.email "amit.salesforce21@gmail.com"

Lets work on Git Project.

Git Init

Create one folder on your local system and open the Git bash. Right click and chose “Git bash here” (This option you will get after you install Git).

Git Init: It Initializes the directory as a local code repository for Git. This command will create few hidden directories those are needed for some commands to work but we need not to worry about it. Once we have initiated the local code repository, Git will track the changes in this directory now.

git init

Git status

Once you are done with git init command. Start working on your project and add some files. Once you are done with your changes and you want to track the changes in git. Then you can use the git status command to see untracked files.

git status

Git add

Git add command is used to add the file in stage (meaning ready to commit).

git add logfile.txt

Above command will add only logfile.txt in stage. If we issue Git status command again, it will show one file waiting for commit and others file not tracked yet. As we have only added one file (logfile.txt) in stage. 

We can also add multiple files to stage together by using wildcards. 

git add *.txt. (This will add all .txt file to staging)

git add . (This will add all files in staging irrespective of their file type/extension)

Git rm

In the last command, we have put files in stage, which means files are ready to commit. In case, we have accidentally added a file in stage and now want to unstage it. The command which we are going to discuss now will help us.

For example if we added test3.txt file in our local repository and with the help of git add command, we added that in stage. Later We decided to unstage that file, so we have to executed git – – rm cached <file name> command to unstage it.

git -- rm cached <file name>

Github provides few other commands besides rm –chached to unstage the file. Those commands are reset –head and git –restore. It will be worth to understand the difference on github documentation.

git commit

Git commit with switch -m allows us to commit the files in local repository. Please see below – 

git commit -m "Initial commit"

.gitignore: (It’s not a command but a file in your local repo)

.gitignore file is used to specify that which file you never want to add in stage/commit. You can specify the name of the files or even folders and those will never show up in the list.

Git branch

To create a new branch in the repository, we use git branch <BranchName>

git branch BugFix

Once you created the branch you need to manually change the branch by using git checkout <branch name>. 

If you notice the commands used in below window, we have created a new branch “BugFix” by using the git branch command. Creating the branch does not mean that it will switch you to the new branch automatically.

Git Checkout

With the help of git branch command, you can create the branch in Git but as we mentioned in order to work on a particular branch, you need to checkout that branch explicitly by using Git checkout <branchname>. 

git checkout BugFix

Important – While switching the branch, please notice the difference in the files present in physical folder in your laptop. It should show you the content of only active branch. If it’s not changing the content then make sure that you do not have any file left in stage and run git clean -f -d command to clean your repo.

Git merge

As it is clear from the name itself if you are currently in master branch and want to merge it with the content of BugFix branch or vice versa. You will use Git merge command. 

See below, I am in BugFix branch and used the command Git merge main

Text

Description automatically generated

Working with Git Remote

So far, we have learnt the basics of Git commands which we can use in our local repository. The next and important part is, how to use remote repository in GitHub and push the code to remote repository.  

Before starting on remote repo- 

  • Create a GitHub account, if you don’t have one already. 
  • Login to your GitHub account. 
  • Now Create a remote repository. Please see below screen, for now you can leave all options as default and select this as a public repository. 

We have created a Test repository with name ‘TestRepo’. We will use the highlighted URL from my git bash (local repo).

Graphical user interface, text, application

Description automatically generated

    After getting the URL, we will use following commands – 

  1. Git remote add <Origin-name>* <URL>

* Origin-name is just a unique name(key) in your setup. It associates your remote repo with a label and should be unique.

  1. We have used push command to push our local repo to remote repo.

If you notice the above window, we have also used a command git remote. This command will tell us that what other origin we have already added. 

In our repo, we see two 

  • a. origin (prv used one) 
  • b. Testrepoorigin (we have added this one recently to associate our new repo with this key.) 

After creating a new repo in your GitHub account, you can use all those commands mentioned above to add, commit, and push the changes directly to your remote repo.

git push

The git push command is used to upload local repository content to a remote repository. Pushing is transferring commits from local repository to a remote repository

git push <remote> <branch>

Git Clone

If you or some other developer in your team wants to take code from remote repository to his/her local system for his/her own work, he needs to clone the repository.  He / She can do that by using Git clone command and continue working on his local repo and whenever he wants to add or commit, he will use the commands mentioned above. 

git clone <repo> <directory>

git fetch

The git fetch command downloads commits, files, and refs from a remote repository into local repository. It will download the remote content but not update local repo’s working state, leaving current work intact.

git fetch <remote>

git pull

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content

git pull <remote>   

Some Other useful command

git log

The git log command enables us to display a list of all of the commits on current branch. By default, the git log command presents a lot of information all at once.

git log -10
will only show the 10 most recent commits.

git diff

git diff will compare between any two commits, branches, or tags in the repository.

git diff <commit-id> <commit-id>

git revert

git revert will undo changes. It can be used on commits at any point in the repository’s history without affecting other work. This will revert from remote repository as well.

git revert 
Commit-id will be checked using git log

git reset

This will help to rewind committed code to local repository. It will not revert code if it is already pushed to remote. Use git revert in that case.

git reset --hard <commit-id>  
That’s it about the basics of git command and I hope you like it. In the upcoming blog, I will connect a salesforce Org with VS code by using Git and Salesforce CLI. 

Post a comment