Git & GitHub: Useful command for developers
Gaurav Jain
Sr IT Architect at IQVIA | Salesforce Certified Application Architect | Salesforce Certified Integration Architect | Salesforce Certified Data Architect | 9X Salesforce Certified | 4X Trailhead Ranger | Copado Certified
What is version control system?
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.
Install Git:
Before we do some hands-on, we need to install Git on our local system.
Use below link to download and install GIT.
https://git-scm.com/download/win
Basic Git commands which every developer should know -
I have created a folder on my local system ->myproject ->project1.
Inside project1 folder -> right click and chose “Git bash here” (This option you will get after you install Git).
#1. 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 them. Once I have initiated the local code repository, Git will track the changes in this directory for me. For demonstration, I have created few test files in that folder. I can use any editor to create file. I have used touch command here from git bash window for the same.
#2. Git status:
If you noticed in below window, I have used Git status command and In our scenario, it is showing that 2 files (which we have added recently) are untracked. Meaning we have not added them in stage yet, but those 2 files exists in our local repository. (How to stage them? will learn in next command),
#3. 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 another 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)
#4. git rm:
In the last command, we have put files in stage, which means files are ready to commit. In case, we have accidently added a file in stage and now want to unstage it. rm command will help us.
I have added test3.txt file in my local repository and with the help of git add command, I have added that in stage.
I have executed git status command to confirm that file is available in stage.
Later I decided to unstage that file, so I have executed git - - rm cached <file name> command to unstage it.
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.
https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things
#5. git commit:
Git commit with switch -m allows us to commit the files in local repository. Please see below -
#6. .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.
#7. Git branch:
To create a new branch in the repository, we use -
git branch <name of branch>.
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, I 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. You can notice in the below image.
To switch the branch, we need to use Git checkout command.
#8. Git Checkout:
With the help of git branch command, you can create the branch in Git but as I mentioned in order to work on a particular branch, you need to checkout that branch explicitly by using Git checkout <branchname>.
See below image for #7 and #8.
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.
#9. 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.
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.
I have created a Test repository with name ‘TestRepo’. I will use the highlighted URL from my git bash (local repo).
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.
2. We have used push command to push our local repo to remote repo.
If you notice the above window, I have also used a command git remote. This command will tell us that what other origin we have already added.
In my repo, I 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 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.
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.
Thank you for reading!
Senior Software Consultant, .NET core, UI frameworks, Azure, AWS, Microservices, SAFe Agile
3 年Very good article Gaurav
Leader in Technical Content and User Experience | Inspiring Technical Content Teams for SaaS Excellence | Trainer for Global Tech Comms Teams
3 年This is awesome. I learnt Git and GitHub from Anindita Basu. She created an entire documentation using Markdown on Vedas through Git and GitHub. Knowing these greatly helped me understand Copado. I loved your documentation, hence sharing the same ??