DEV Community

Cover image for Basic Popular Use Cases of Git
Ashique Billa Molla
Ashique Billa Molla

Posted on

Basic Popular Use Cases of Git

So, all the people out there who are writing fabulous codes that has to be backed up somewhere. The changes you are making in your code on the way of developing some applications, you must keep track on that. And also while you are in a team, everyone's collaboration has to be summed up nicely. And for that we have a great Version Control System (VCS), Git.

In this post I have stated some basics and popular applications of git which will help you while you are using git repositories (repos) like GitHub or Azure DevOps (ADO) repos.

I have answered the following questions designing some cases:
How to create a git repository in the local as well as in the remote repos?
How to push your code?
How to pull your code?

Case I- Create a Remote Repository and Clone It to Your Local:

Step1. Create a repo in the ADO Repository section or in the GitHub by providing a title to your project.

Step2. A project is created generally with a 'main' branch. But it is always better and standard way to create a new branch and work on that new branch. So create a new branch, by clicking on the dropdown where you see 'main'. I have created the new branch and named it as 'first-branch'.
Create A Branch

Step3. Copy the link of the remote repo to your clipboard.
Image description

Now It's time to bring the repo into your local.
Step4. Create a folder in your local at a desired directory.

Step5. Open your terminal (like command prompt in windows) by selecting all the keywords from your current directory header field and typing cmd. It will directly open the cmd in the current directory. Or just open your terminal and type cd your_current_folder_directory. In my case the folder is Demo_Folder.
Open CMD

Step6. Type git clone <url>, the URL you copied. -> You will see a local copy of that remote repository has been created. Now you can build project in your folder.

After creating a project(adding files in the "cloned" repo) you obviously want it to push in the remote repo. Follow the steps:
Step7.At first open your terminal with the "cloned" folders as the current directory.

Step8. To know you are on the appropriate branch run the command git branch. It will show us that we are on main branch as the main branch will be highlighted. But we have to switch the branch.

Step9. To know all the created branches run git branch -a.
all branch

Step10. Now to switch to the 'first-branch' run git checkout first-branch.
git checkout

Step11. Again run the git branch command to asure that you are on the 'first-branch'.

Step12. To know what are the files there just run git status. In my case I have created two files -> index.html and styles.css
git status

Step13. Now run git add --all to "stage" all the files in your repository.

Step14. git commit -m "<message>" will commit your code. And within a string you can write your message to indicate the changes and updates you have made.
Git Commit

Step15. Now finally run git push which will push your codes/ files to the remote repository. Congratulations, you successfully pushed your code. Your git repo will be something like this πŸ‘‡
pushed

Now, every time you update your code just follow the steps from step no. 7 mentioned above to push them.

Merging the branch:
After your work is complete and you have to merge the manually created branch with the 'main-branch'. Follow the steps below:
Step16. At first you have to create pull request. By clicking on the option Pull request.
Pull request

Step17. Then click on the 'New pull request' button.

Step18. Compare two branches and after observing the changes click on 'Create pull request' button. Describe your pull request and Create pull request. While you are on a team, basically one reviewer reviews the pull request and approves. Then the approver merges the pull request by clicking on 'Merge pull request'. button.
Compare two branches

Step19. Confirming the merge you have successfully merged the branches and you can delete the custom branch you created.Merge pull request

Case II- Local Project is Already Created:

Step1. At first create your repo in the cloud(GitHub).

Now you have to initiate a git repo in that local folder where the project is already present:

Step2. Open your terminal with the current folder as the current directory. In my case it's 'new-project'

Step3. Then run git init

Step4. then run git add --all

Step5. Run git commit -m "<your_commit_message> just like step 14 in the previous case.

Step6. Run git branch -M main to push the code into main branch. At this time if you check your current branch by using git branch you will see Main is your branch now.

Step7. git remote add origin <url> command will add your remote repo into the local.

Step8. finally push by using git push -u origin main.

Case III- Syncing Up Local with Remote Repo:

You might face one case when the project is set up in your local but it is not up-to-date. To sync up your local repo with the remote repo just run:

Step1. git pull

Then the further pushes can be done by following the previously mentioned steps in the case I from 7 to 15.

Although I have only talked about GitHub repositories but the configurations and user flows are almost the same for ADO Repos. You just have to play around with them. If you observe any further issue while you are pushing your code, comment it here or DM me on twitter or just google it.

Thank you for reading it. Hope it has helped you. Follow me on twitter, I post about web development. See you on the next article!!

Oldest comments (2)

Collapse
 
dheirya_tyagi profile image
Dheirya Tyagi

Nice article!!!

Collapse
 
glenguson profile image
GlenGuson

you added a bug in the commit and want to roll back, so how do you delete the commit once it’s pushed to your remote? Spells to make someone move away