Git and GitHub Practices: 2
Init command: Use the git init command to create a new Git repository in the current directory. It enables version control for your project by creating a new Git repository and starting over.
Syntax: git init
Here are the processes involved in setting up a fresh git repository.
Rather than using the touch command, we used New-Item to create a new file.
- Touch Command (Unix/Linux):
This command can be used to update the access and modification timestamps of files that already exist or to create new, empty files.
Syntax: touch [file_name]
If the file file_name doesn’t exist, it creates an empty file with the supplied name. Touch modifies the file’s timestamps (access and modification) if it already exists without altering its content. - New-Item cmdlet in PowerShell: To create new objects, such as files and folders, use the PowerShell cmdlet New-Item. To create an empty file with New-Item, use the -ItemType File parameter. opens a brand-new, empty file at the specified file_path.
To add a remote repository to your local project: To add a remote repository (e.g., on GitHub) as the “origin” of your Git project, you can use the following command.
Syntax: git remote add origin <repository_url>
After running this command, the remote repository with the given URL will be associated with the name “origin” in your Git project. You can then push and pull changes to and from this remote repository using the “origin” name.
Remote -v command: The git remote -v command is used to view a list of remote repositories that are associated with your local Git repository. It displays the URLs of these remote repositories, which you can use to fetch or push changes to and from those repositories.
Syntax: git remote -v
Now you can push your local repository to a remote repository.
Git Branches: Using branches in Git allows you to divide up multiple work streams. Until you’re ready to merge your changes, they let you work on various features, bug fixes, or enhancements concurrently without affecting the main codebase.
Git branch command: Use this command to view a list of the branches in your repository.
Syntax: git branch
Create a branch: Use the git branch command and the branch name to create a new branch.
Syntax: git branch <branch_name>
Switch branch: To switch to a different branch, you use the git checkout command.
Syntax: git checkout <branch_name>
Delete a branch: Using the -d option with the git branch command, you can remove a branch.
Syntax: git branch -d <branch_name>
Renaming branch: The git branch command can be used with the -m option to rename a branch.
Syntax: git branch -m <new_branch_name>
Merging branch: Combining modifications from one branch into another is known as branch merging on GitHub. There are two methods for branch merging.
- Using the command line: To find out what has been added to the new branch, you first compare the two branches to see what has changed.
Syntax: git diff <main_branch>
After that, you can combine two distinct branches.
Syntax: git merge <new_branch>
2. Using Pull Request: You can start the merging straight on GitHub if you’re utilizing pull requests.
Get PRATIKSHA GARKAR’s stories in your inbox
Join Medium for free to get updates from this writer.
a. Go to your GitHub repository.
b. Click on the “Pull requests” tab.
c. Open the pull request you want to merge.
d. Click the “Merge pull request” button.
e. Confirm the merge.
Merging Conflict: When Git is unable to automatically integrate changes from various branches, merging conflicts may arise. This frequently occurs when the same section of a file has undergone changes in both branches. Resolving merge conflicts requires manual intervention.
Git will alert you in the event of a disagreement. Use your text editor to open the files that are at odds. Git will highlight the portions that disagree.
Make the necessary modifications to the conflicting file by hand-editing it. Eliminate any extra lines and the conflict markers (\\\<\\\, =======, >>>>>>>). Then complete the merge process.
Syntax: git merge — continue
Aborting merge: To stop the merge, take the next action.
Syntax: git merge — abort
To reduce the possibility of conflicts, pull updates from the main branch into your feature branch on a regular basis.
Forking repository: One popular method for working on code jointly or contributing to open-source projects on GitHub is to fork a repository. By creating a personal copy of another person’s project under your GitHub account, you can modify it without having an impact on the original repository.
- Go to the repository on GitHub that you wish to fork. Example of Vscode
- Click the “Fork” button located in the upper-right corner of the GitHub page. After that, the window below will appear.
3. Then click on the Create fork button.
Conclusion: The purpose of this article is to understand the git initialization process, git branching, merging branches, and forking repository concepts.
Thank you for taking the time to read this. If you find this article useful, please help me reach 500 followers.
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.










