Learn useful Git command!
A couple of weeks ago, I wrote about the importance of having a clean git workflow and discussed important tips of merging branches. I wanted to tackle the topic of Github and Git Commands but on a broader scale!
Version Control is such an important aspect of technological development workflow. I hope to create a cheat sheet of important git commands that every developer should know to have a successful and fluid work flow. I will also go over the basics of Git in general. It might seem intimidating at first but these commands will be used throughout your whole software engineering career!
What is Git? And What is Github?
Git is a free and open source version control system. It allows developers to organize, manage, and keep tack of their projects. It provides a history of changes and additions made to projects and files. It even holds the different versions of the progress allowing easy access for developers to recover and view their previous code.
Git is one of the many free version control system out there. It was created by Linus Torvalds in 2005 and again it allows developers to manage their projects development progression. These projects are contained in repository, or repo for short.
Github is a website that hosts developer’s projects and code. Mainly, it hosts our git repository online. Outside of our local computer, we have a documentation of our project and its past versions.
GIT CHEAT SHEET
This section deals with creating repos or starting to work from existing repos.
After navigating to the required directory where your code is, this command is used to initialize a new Git repository. This is hosted locally.
git clone [enter url]
Now let say that instead of starting a fresh new repo, we want to work on an already existing repo hosted on Github. We can use this command to retrieve that repo from a hosted location outside of our computer.
Stage & Snapshot
Along your development, you will be adding and creating new features. We want to save your new work but at the same time we want to be able to revisit our old code.
This commands allows us to see what files have been staged or tracked and what files have not.
git add [enter file name] OR
git add .
After modifying a file, we want to add that file to the staging phase which allows us to commit that change later. To add specific files, we can use the first command with the file name entered. To add all files to the staging phase, we can use the second command (Note: The period is apart of it!).
git reset [enter file name]
This command allows us to unstaged a file but it will have the changes we made still in the directory.
git diff OR
git diff --staged
In the first command we can view changes we have made but that are not staged yet. In the second command, we can review changes we have made that have not yet been committed.
git commit -m “[enter description message]”
This is one of the most important commands. After adding our changes to the staging phase, we want to commit our changes. Commit our changes will save our changes as a snapshot of our files at its current state. We can revisit this version of our project later as new additions are added.
Sharing & Updating Repos
In this section we talk about updating our repos with our commits and changes. We also talk about receiving updates from other repos.
After committing our changes, we can push our local commits to a remote repo that is hosted online or somewhere else.
When we want to fetch changes made from a remote branch and have those changes on our local repo, we use this command.
When we want to see our commit history on the active branch, we can use this command to check.
Branching & Merging
In a previous blog, I have talked about the useful commands that allow us to have a better workflow. I will briefly go over them here.
git checkout [enter branch name]
We can checkout other branched in our repo with this command.
git checkout -b [enter branch name]
With this command, we can create a new branch and switched to it at the same time.
git merge [enter branch name]
When you want to merge a branch into the one you are currently on, we simply use this command and enter the name of the branch we want merged.
git merge --abort
When we encounter conflicts in our merging, we can abort this process with the command above.
These are the most useful commands that you will be using almost on an everyday bases. I hope this helps and happy coding!