Git
Git is a software tool based on distributed version control system, that is installed in an individual machine to maintain the local repositories as well as push/pull changes from/to the central repositories.
It supports distributed, non-linear workflows which means thousands of parallel branches can run on different systems.
Some git alternatives: Mercurial SCM, Apache Subversion
Three stages of Git:
Working directory/Workspace: The folder where you work/save all the work files. Inside this folder, you'll have to initialize the local repo:
git init
After initializing, add files to the staging area by:git add <filename>
orgit add .
to add everything to the staging area.Staging area: It acts like a caching zone or temporary storage of files before pushing them to a local repo. To send changes to the local repo:
git commit -m "your message here"
-m means message.Local Repository: It maintains the versions. git push and git pull commands can then be used to push/pull code from a central repository like GitHub, GitLab, or Bitbucket...
Initial setup commands
Here I have used Ubuntu, so the package manager will be apt. You need to use yum in the case of CentOS, Amazon Linux
Update system:
sudo apt upgrade -y && sudo apt update
Install git:
sudo apt install git
Check where git is installed:
which git
Check version:
git --version
To configure the username and user email to identify who made the changes:
git config --global user.name "<username>"
git config --global user.email "<your_email_here>"
To list created users:
git config --list
GitHub
GitHub is a web-based hosting service for version control using Git. It is a central repository. It provides access control and several collaboration features such as bug tracking, feature requests, and task management. Microsoft bought it on Oct. 26, 2018. Public repository is free but maintaining private repository is a paid service.
Open-source developers created GitLab to make everything free.
Bitbucket is also an alternative to GitHub.
Start working with Git and GitHub
Scenario: You have a directory with some contents and want to maintain the version.
Initialize git: git init
It will create a .git directory to maintain the local repo in your working directory.
Check git status: git status
Add files: git add <filename>
(single file) or git add .
(all files). Files now go to the staging area.
Commit files to the local repo: git commit -m "Your message here"
Now the files are sent to the local repo.
Check the git log: git log
for detail log or git log --oneline
for summary
To show what's in the specific commit: git show <commit_id>
Connect to central Repo
First, you need to create your GitHub account and a repository in your account.
git remote add origin <url_of_central_repo>
It will ask for your username and password. Check the username from your profile and for the password, you need to enter an access token for security reasons.
For access token: Settings> Developer Settings
Personal access tokens> Tokens(classic)> Generate new tokens> Generate new token(classic)
Assign a token name, choose the expiration period and select what permissions you want to give to this particular token.
To show which remote repository is connected: git remote -v
To remove the attached remote repo: git remote rm origin
To push your local repo to the central repo: git push origin master
It will ask you for authentication, enter your username and access token.
To pull from the remote repo: git pull origin master
If pulling branches other than master, change the branch name.
gitignore
Create a file with the name .gitignore in your working directory.
Write anything or any file you want to ignore in this file and add to your local repo by: git add .gitignore
and git commit -m "your ignore message"
Generally, specific file or files with the same extension are added in this file and git ignore all those files and doesn't even show them in the git status
as well.
Git Branches
Branches are used for parallel development. There is no restriction on the number of branches that can be created. It is recommended that each task is done in a separate branch and then merged after the successful completion of the task. Any new file is visible to all the branches unless committed to a specific branch.
To see all the branches in the system: git branch
To create a new branch: git branch <branch name>
To switch to another branch: git checkout <branch name>
Files committed in the master can be pushed to the central repository but not from other branches.
Once the task is completed, it should be merged into the master.
Git Merge
Git works in a pulling mechanism for merging and it can only be merged into the master branch. Master pulls the changes from other branches and merges them into itself.
To merge branches: Firstly, you must be on the master branch: git checkout master
git merge <branch_name>
mention the branch name you want to merge
After merging, you might want to delete the branch.
To delete the branch: git branch -d <branch_name>
There could be cases where you might not want to merge the branch and don't even want to work further in that branch.
To delete the branch without merging it to the master: git branch -D <branch_name>
Git Conflict
Multiple branches having a file with the same name but different content, which when tried to merge will create conflict.
Conflict here is about which content to write first and what to write later i.e. the order of content in a file.
To resolve the conflict: open the file and edit it to manually define the order of the content, and then commit the changes.
Git Stashing
To store the working file in external temporary storage to work on some other file beforehand.
Suppose, you are working on a file abc which is in the working directory/workspace.
To send this file to stash: git stash
Workspace is now clean, work on a new task as per the requirement. Once completed, commit the changes. Now the working directory will be clean again.
Multiple files can be stashed but new/blank files cannot be stashed.
To check all the stashed files: git stash list
Output:
stash@{0} <stashed_contents>.............newest stash
stash@{1} <stashed_contents>
stash@{2} <stashed_contents>
stash@{3} <stashed_contents>.............oldest stash
To bring back the stashed item to the workspace: git stash apply stash@{2}
Even after bringing the file from the stash to a working directory, that file will still be in the stash store.
To clear the stash store: git stash clear
Git Reset
Your task is done; you did git add .
and added that file/content to the staging area. But you want to get back the file to the working directory instead of committing to the local repo.git reset <filename>
or git reset .
It will reverse the action of git add .
It can also be called resetting the staging area as the files are removed from the staging area.
To delete the file from both the staging area and workspace: git reset --hard
Git Revert
A document is committed to your repository, but you want to go back to the previous commit and undo the recent commit, git revert <commit_id>
can be used.
It won't delete the recent commit but rather will go back to the previous commit.
Git Clone
Get the GitHub repo URL of any repository you want to clone to your local system.git clone <github_repo_url>
It will create a new folder with the repo name and download all the files of that repository to your present working directory.