Updating the Repository with Git
Hello everyone! In this post, I am going to demonstrate how to make changes and updates to a repository safely with the source control tool Git with my Level Up in Tech GIT project.
The goal of the project is to make some changes to the Level Up In Tech Repo:?https://github.com/LevelUpInTech/LevelUpTeam.
First I will fork the project, then make some changes in my local branch. Then I will commit the changes and create a Pull Request to ask for merging the changes to the original branch.
Step 1 Fork the Level Up In Tech Repository
In this case, the new changes won’t be merged into the main branch without any reviews and approvals. So we can “play safe”.
Click the “Fork” button at the upper right corner.
Then copy the link of the fork repository under?Code > HTTPS.
Step 2 Clone the repository to local
First, open a terminal and check whether “git” is installed.
$ git — version
If git is not installed, use the following command to install it.
For Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS):
$ sudo dnf install git-all
For a Debian-based distribution, such as Ubuntu, try apt:
$ sudo apt install git-all
Choose a location for the repository and use “git clone” and the link of the Fork repository generated in step 1 to clone it.
$ git clone?https://github.com/graceivy/LevelUpTeam.git
Now we can see the repository is cloned to local.
Step 3 Make some changes and updates in the local repository
Usually we will create a new local branch to make the changes instead of using the local main branch to keep the local main branch up-to-date with the production main branch (the remote one).
$ git checkout -b <local branch name>
But since this project only requires to update the repository once, the update will be directly made in the local main branch.
Here the?README.md?file is updated with a brief introduction of myself.
Use Vim to create a change to the file that is in the local main branch.
Use the command cat?to confirm the change has been made.
领英推荐
Step 4 Save the file and add to your local repo, then commit the file
We can use the below command to list the updated files and their paths.
$ git commit
Use?git add?to add the changed files and/or directories and?git commit?to commit the files.
$ git add <path>
$ git commit -m ‘your messages about the updates’
If only?git commit?is used like below, a file will be open and you can write your message into the file.
After adding and committing the files, check the status of the local branch.
$ git status
You will see the local branch is ahead of the remote one ‘origin/main’ (the fork repository).
Step 5 Push the changes to the remote fork repository
As I changed the files in the local main branch,?git push?can push the changes to the remote main branch.
$ git push
If a new branch is created and we want to push the changes to the remote main branch, below command needs to be used.
$ git push -u origin master
Here username and token are needed.
To create the token, please refer to:?https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
Now check the status of the local branch again. It should be “up to date with” the remote ‘origin/main’.
Step 6 Merge into the original production repository
In order to merge into the original Level Up In Tech production repository from the fork repository, we need to send a pull request and ask for reviews and approvals. In this case, we are sure the new changes will not mess up the production.
Now the Pull Request is added. After the PR is reviewed and approved, the changes of the files will be merged into production repository.
In summary, Git is a powerful and wide accepted source control tool. We can use Git to effectively manage the changes and updates to the repository. Thus the production won’t be negatively affected.