Git
Git is a version control system for tracking changes in computer files. It helps you keep a detailed record of the changes you make to your files, like a "history" feature for your project. It helps you keep track of every change you make to your files. So, if you accidentally mess something up, you can go back in time and restore an earlier version. This makes Git incredibly useful for fixing problems (debugging) and reviewing what changes were made over time. It's like having a safety net for your work!
So this is the simple example of version history of a project folder. In this, all three figures represent a different version (created time by time) of a same project A. With use of git we can go to and fro at any point of time according to our necessities, as if we want revert some changes etc.,
Git operates in three key states, allowing you to effectively track and manage your code changes:
Working Directory:
This is where you modify files.
Any changes made here are not yet tracked or saved to Git.
Staging Area (Index):
When you're ready to save changes, you "stage" the modified files.
Staging prepares snapshots of your changes to be included in the next commit.
Git Directory (Repository):
Committing moves the staged changes into the Git repository, storing them permanently.
You can also check out previous versions, make further changes, and repeat the process.
GitHub
GitHub is like a special online workspace for your projects.
It's a place to store your work online: Instead of keeping your files only on your computer, you can upload them to GitHub, making them accessible from anywhere with an internet connection.
It makes teamwork easier: GitHub provides tools that help multiple people work together on the same project smoothly. Imagine it as a shared online whiteboard where everyone can see and contribute to the work.
You can use Git without GitHub, but you can’t use GitHub without Git. There are also other platforms like GitLab, BitBucket, and self-hosted options like Gogs and Gitea. These platforms are called “remotes” in Git terminology. While using a remote is optional, it’s a great way to share your work and collaborate with others more efficiently.
The first two things to do are install git and create a free GitHub account.
Steps to be followed to use git and GitHub:
As a first step download and install git based on the operating system.
Create a new repository in GitHub.
If you have installed earlier you can check the version by using following git command in the git bash or command prompt (Using git bash or command prompt provides you full access of functionality, command line provides access to all Git commands and options, some of which may not be available in GUIs. This gives you fine-grained control over your version control operations).
to find the version of git, 'git --version' enter will provide you with version details
To view all the settings we can use the following commands
git config --global user.name "xxxxx"
git config --global user.name (this is to check username)
git config -- global user.email "xxxxxxx"
git config -- global user.email (this is to check email)
git config --global list (when you enter this command will provide you the details of username, email and password)
to check username registered with gitHub,'git config --global user.name' enter willl provide you with your registered username
Create a demo project/ folder on the workspace and adding to GitHub: The First Push to master
The following commands used to push your project to the remote repository(GitHub).
cd /path/to/your/project
copy the path of the project folder and paste in the git bash or command prompt and enter.
git init ( this is the comment to initialize the git, and navigate to the project directory and initialize it)
git add . (This is staging area we are using this command to add all our folders in our project. when we give gid add . we do not require to mention the specific name of files to be added, it will add altogether)
git commit -m "Initial commit" ( this is where we are committing or saving our project with the commit message. And it is important to commit with the message and it should be relevant for our convenience. A commit is a record of what changes you have made since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those changes into a commit)
git remote add origin https://github.com/yourusername/your-repo.git ( Here in this command we have to copy our repository URL that we created in remote repository or we can click on code to copy the URL. By this command we are linking our local repo to the remote repo)
git branch -M main (This will push your local files to the remote repository for the first time)
git push -u origin main (The -u flag sets the remote origin and branch main as the default for future pushes)
git status ( This command will implies our status of our branch. Currently it is up to date with origin/main.
With all these commands our local project updated with the remote repository
Project in the local repo
After the initial push, we can continue working on the project. Whenever we complete a set of changes, we can push them to the remote repository to keep it updated.
To update the latest changes again we use git status command, that will guide you with untracked files ready to be committed. To continue we proceed with git add . and git commit -m"message" and git push -u origin main as we did it above.
Some Basic other Commands of Git are as follows :
git branch
Lists all branches in the repository; highlights the current branch.
git checkout <branch>
Switches to the specified branch.
git branch <new-branch>
Creates a new branch.
git merge <branch>
Merges the specified branch into the current branch.
git push
Sends committed changes from the local repository to the remote repository.
git pull
Fetches updates from the remote repository and merges them into the local branch.
git remote -v
Lists the URLs of the remote repositories.
Conclusion
Since Git is an open-source and free distributed version control system, you can explore and learn its features practically. It is fast, efficient, and widely adopted by programmers across the globe to manage and track code. With its powerful features, Git has become an essential tool for software development teams and organizations. It prevents disasters! Git helps avoid losing work or accidentally overwriting important changes.