Introduction
Git is a version control system that tracks changes in code. When writing code we need to save every significant change or feature that does a certain function as a commit that can be reverted to if we ever need that instance of the code. This helps programmers to traverse code since nothing is ever lost in the product life cycle of the project. It also helps in debugging since we have a copy of the previous working state. So how do we start out with git you ask.
Install git
- Install git for your specific operating system. After installing git you will note that the file comes with git bash which is a command line terminal that is used to run commands or instruct the version control git.
- To ensure that git has been installed open git bash and run the command
git --versionwhich will return the version of git installed
Download Visual Studio Code
- Install VS code a text editor for writing code.
Create a github account
GitHub is a cloud-based platform used by developers to store, track, and collaborate on software code. It operates as a hosting service for git, an open-source version control system that tracks changes made to files.
- Head over to github and create and account
Creating a directory tracked by git
- On git bash run
mkdir <filename>. This will create a folder ie the project that will contain your files of code. - Enter the folder through
cd <filename>and create a file README.md file which explains what this project doestouch README.md. Run the commandlsto list files in the directory - Open the vs code from terminal by running
code .and edit the README.md file. Now the file is ready to be tracked by git on github
Staging, committing and pushing
We now need to initialize the directory making sure that git is now tracking the file. Running the commands ls-la will confirm that git has been initialized in the repository. There are two ways of doing this either via the text editor VS code or the terminal. Let us go through both.
1. VS code
Make sure your VS code is connected to your github account.
- Click the source control icon (third from top) and Initialize Repository. This will introduce git to track the file
- Hover over the file and click the + button to add the file. This changes the file from U (untracked) to A (added)
- Write the commit message in the input box above describing what you are committing eg add README.md file.
- On the commit button press the drop down arrow **for more options and **press commit and sync. This will now stage your repository to be pushed to github.
- Click on publish branch and set the repository to either public or private.
- Go to your github and refresh to see your newly pushed repository that is now tracked by git.
2. Using the terminal
- Open your terminal ie. gitbash and go to your folder.
- Initialize Git using
git init. - Add your files using
git add .to track them. - Commit the files using
git commit -m "first commit". This should describe what we are committing and will to save the change locally. - Link your remote repository using
git remote add origin <your-repo-url>.Preferably use the ssh link option - Push the files using
git push -u origin mainto send your code to GitHub and publish the repository as the main branch.
With that now you know how to set up a project and keep track of your changes using github.
Top comments (0)