Brief Introduction of Git and GitHub
Git is a distributed version control system that allows developers to track changes in their code, collaborate with others, and manage different versions of their projects. It helps in maintaining a history of changes, making it easier to revert to previous versions if needed, and facilitates collaboration by allowing multiple developers to work on the same project simultaneously without conflicts.
GitHub, on the other hand, is a web-based platform that uses Git for version control. It provides a user-friendly interface for managing Git repositories, along with additional features like issue tracking, project management, and collaboration tools. GitHub is widely used for open-source projects and private repositories, enabling developers to share their code with others, contribute to projects, and showcase their work.
Git Installation and Getting Started with Tracking Changes
Install Git: Download and install Git from the official website (git-scm.com) and set it up on your local machine. (while installing the Git in Windows 7/10/11, make sure to select Git-bash as command line and use Git-bash command line to perform all the Git related commands)
Create a Repository: Since we have successfully installed Git, we need to tell what needs to be tracked, which folder/folders needs to be tracked, to start tracking we have first initialize a git inside that folder using the command
git init
. This creates a.git
folder that tracks changes.Make Changes and Commit: Since we have initiated the git to start tracking project directory/folder, we need to tell which files needs to be tracked (there can be many files inside the folder, either we can track all the files or specifically tell which needs to be tracked), to do that add files using command
git add <filename>
, this adding the file to Git to start tracking is called STAGING AREA.Commit Changes: After some making some changes to the file, you need to save those changes, in Git we call it as COMMIT, adding the file to commit changes with
git commit -m "commit message"
to save a snapshot of your project.
Those are the basic steps need to do to start tracking the files inside the directory/folder by Git.
How to connect and use GitHub
Before connecting the remote repository, it is best practice to run the below commands,
git status
- This command checks whether Git is initialized in the current directory.git remote -v
- displays a list of remote repositories (or "remotes") that are associated with your local repository, along with their corresponding URLs, if it is displaying corresponding URLs, then you will know for which repository connection is established
Connect to GitHub: Create a repository on GitHub website and connect it to your local repository using
git remote add origin <repository-url>
(Url will be in the form of github.com/SyedAhmedSM/Repositoryname.git)Eg:
git remote add origin https://github.com/SyedAhmedSM/Repositoryname.git
Rename master branch to main: To rename use
git branch -m master main
Push Changes: Upload your local commits to GitHub using
git push -u origin main
(this command should only be used once for each branch) (or)git push -u origin master
, depending on your default branchThe
-u
flag in thegit push -u origin main
command is used to set the upstream (tracking) branch for the current branch. This means that after you push with the-u
flag, you can simply usegit push
orgit pull
in the future without having to specify the remote branch.Collaborate: Use branches to work on features independently (
git branch <branch-name>
andgit checkout <branch-name>
) and merge them back into the main branch when ready.Pull Changes: Keep your local repository up to date with changes from GitHub using
git pull
.
By mastering these basic commands and workflows, you can effectively use Git and GitHub for version control and collaboration in your software projects.
Git Commands and it’s there uses
Git Get Started Commands
How to check the version of Git: git --version
How to Initialize Git on the current folder: git init
How to set the username for the current repository to "yourname": git config user.name "yourname"
How to set the username globally: git config --global user.name "syedahmedsm"
How to set the email globally: git config --global user.email "syedahmedsm@email.com"
How to check status of Git: git status
Git Staging Commands
How to add single file index.html
to the Staging Environment: git add index.html
How to add multiple files to the Staging Environment: git add --all
(or) shorthand command git add -A
Git Commit Commands
How to Commit the changes to the current repository with the message "First Commit!": git commit -m "First Commit!"
How to Check the compact version of the status for repository: git status --short
How to Commit the files directly, skipping the staging environment: git commit -a -m "second commit skipping staging environment"
How to view the history of commits for the repository: git log
(or) git log --oneline
Git Reset Commands
How to reset to the commit with the hash abc1234
: git reset abc1234
How to hard reset to previous commit and delete the new commit: git reset --hard kbc235
Git Diff Command
How to check difference between two commits: git diff --staged
(or) git diff abc1234 dss3434
Git Remote Remove Connection
How to remove the established remote connection: git remote remove <remote-name>
eg: git remote remove origin
How to rename default branch from master to main
First thing first run,
git status
- to check git initialization
git remote -v
- to check remote connection is established
Then, first rename the local master branch to main git branch -m master main
Then, we should rename remote repository as well by pushing to remote git push -u origin main
Now we need to delete the old master branch on remote, but before doing that go to Github —> Repository —> Settings —> General —> Default branch, change the default branch from master to main as shown in the below image
Now we can delete master from the remote repository git push origin --delete master
Next step is to unset upstream from master and update the local repository to set the upstream branch for main
git branch --unset-upstream
git branch -u origin/main main
Ok now we have successfully renamed master to main locally and in remote and hence from subsequent pushes will use git push
How to Contribute to open-source repository
If you though how to contribute to any repository and create a pull request for any repository, you need to follow the below steps
First, Go the repo and Fork the repo so that I will create a copy in your GitHub account
Second, go to your GitHub account and get the link to clone your repository in local
Third, after getting the link clone the repository using git clone <copied link>
so that it will be downloaded to your repository
Fourth create a new branch (git checkout -b <branchname>
) to add features or fix the bug (of course you will commit after adding features or fix the bug) and then push your code to the GitHub account repository which you forked
Fifth you will get a message stating whether you need to create a pull request to the original repo
Lastly, depending on your contribution whether it is useful or not, the original repositor will merge and decline your pull request, please note that update meaningful contribution to open-source repo.