Git is a version control system. Is widely used by developers to track code changes over time. Essentially, it is a time machine for your project that allows you to revert to previous versions as needed.
Version Control
Multiple People can easily collaborate and everything stays in sync. You can also check the history of the file. Edited Code and the person who did those edits. Different versions of the projects can be stored, Over time, you will be able to easily understand how the project has evolved. Get back to specific changes in time. We can easily browse by commit, or branch. The version control system also provides the UNDO functionality. Multiple people can make changes to the project at the same time. We can also easily track the history of a set of files. Easily check the commit history.
Git Basics
Types of Git Commands
- Creating Repository – git init
- Making Changes. – git status, git add, git commit
- Parallel Development. – git branch, git merge
- Syncing Repository. – push, pull, origin, remote
How to create a git repository? using the git init command. This will create a hidden .git folder.
To track files & folders we use this command git status.
Tracking changes
Tracking your Files: In your working directory file will be in one state from two states: tracked OR untracked. A file in your working directory that is not in your staging area is an untracked File.
Tracking new files: ## for one specific file, the command will be git add <filename> AND ## for all files and folders command will be git add.
Commiting Changes: Moving files from the staging area to the git folder is called commit. For commit changes the command is git commit -m “Your Commit Message”
If you want to check the history, we run the command git log. which shows the commit unique ID, author, and date. AND to delete a git repository the command is rm -rf .git
Branch
It’s like a tree structure. To check all existing branches we run the command git branch. To create a new branch we run the command git branch <branchname>
To shift to another branch or moving between branches we run the command git checkout <branchname>
To create a branch and switch to the same branch with a single command. we can run the command git checkout -b branchname
To check all the commit across branches we need to run the command git log –oneline –graph –all
How to delete the branch? To delete the merged branch command will be git branch -d <branchname> and To delete a non-merged branch command will be git branch -D <branchname>
Github
GitHub is a cloud-based hosting service where a coder or developer and company can manage or host their projects. Anyone can review any person’s code, Also collaborate, and contribute to other people’s projects. We host the repositories in GitHub.
Remote Repository:
- How to move the server remote repository to the local?
The first step is to create a new repository, Then we push an existing repository to the local repository using the command. We need to set a remote URL / Central repository to make a connection between local repository and central repository.
get remote add origin https://github.com/{username}/{repository name}
git push origin master
After hitting this command, you have to enter the username and password for authentication.
There are three methods of authentication.
- UserID / Password
- Personal access token.
- SSH Keys.
Personal access token:
What is the process for generating a personal access token? To start, we need to go to the settings page and then click on the Developer settings link under the Dropdown list of Personal access tokens. Next, we need to click on the Tokens (classic) link. We can generate a new token on the Personal Access token page. After successfully generating the token. On macOS, we can store the token on the keychain access, and a new password will be generated. Using that password we will create a connection between local and remote repositories.
After creating the connections, How do we push the code from the local repo to the remote repo?
- We can add any line of code in the local repo file.
- After adding, first, check the git status on the command line, they will show the modified file.
- Then we add and commit the changes with a single command. git commit -a -m “Enter your message”
- Commit is done on the local repository.
- Now we push on the remote server using the command git push origin master
The code will be added to your remote repository file once you perform these commands.
Now we clone the repository from GitHub using the command.
For this, first, we copy the repository URL, then go to the command line, in the command line we can enter the command.
git clone https://github.com/{username}/{repository}.git
After running this command, they will clone the GitHub repository to your local system.
Fetch changes from GitHub
Using the git fetch command, we can download commits, files, and refs from a remote repository into your local repo. To integrate or to merge in the working directory we have to run the command git merge {remote repository}/{master branch}
How to contribute to open-source repositories or open-source projects? Fork a repository.
To create a fork, copy the open-source client/main repository to your own GitHub account. Additionally, you must download or clone the repository from your account to your local machine in order to make modifications to that project. Additionally, you can modify the system locally. Once the modifications are complete, you can push them to your account repository. Furthermore, a pull request to the client repository can be raised.
Git Diff Command, with this command you can easily check what you changed between the current working directory and the last staged area
git diff –staged diff of what is staged but not yet committed.
Git Ignore Command, If we don’t want git to keep track of some specific autogenerated files and logs, to avoid them you can list files in the .gitignore file.
Leave a Reply