Git is a version control system that allows you to keep track of changes made to a project over time.
GitHub is popular hosting service for Git repositories. GitHub allows you to store your local Git repositories in the cloud. With GitHub, you can backup your personal files, share your code, and collaborate with others.
To install Git on Mac, we first need to install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install git
For Git to work properly, we need to configure Git with the following commands. Enter your own information inside the quotes. It’s a good idea to use the same email as your Github account email.
git config --global user.name "Your Name" git config --global user.email "email@example.com"
To verify that the output matches your name and email address.
git config --get user.name git config --get user.email
Go to GitHub.com and create an account. If you already have one, sign in.
Check if you have an SSH key
If you receive a message “No such file or directory”, you’ll need to create a new one. Create SSH To create SSH key, run the following command
ssh-keygen -C <youremail>
A Git project has three parts:
git initinitialize a new Git repository
git statuscheck the status of changes
git add filenameadds files from the working directory to the staging area
git add filename_1 filename_2add multiple files to the staging area with a single command:
git diff filenameshows the difference between the working directory and the staging area
git commitpermanently stores changes from the staging area inside the repository. the option
-mfollowed by a message.
git commit -m "First commit"
Standard commit messages:
git log shows a list of all previous commits. The message includes
In Git, HEAD commit is the commit you’re currently on.
git checkout HEAD filenamerestores the file in your working directory to look exactly as it did when you last made a commit. We can also use
git checkout -- filename
git reset HEAD filenameunstages file changes in the staging area.
git reset commit_SHAresets to a previous commit in your commit history. This command works by using the first 7 characters of the SHA of a previous commit.
Git allows users to create branches to experiment with versions of a project. To list all a Git project’s branches, use this command
git branch new_branch
git checkout branch_name
All the commands you do on master branch, you can also do on this branch.
git add filename
git commit -m "Commit message"
Firstly, we need to switch to master branch
git checkout master
Then use this command to merge the new branch to a master branch
git merge branch_name
After the branch has been integrated into master, you can delete it.
git branch -d branch_name
If the feature branch is never merged into master branch, we use the -D option
git branch -D branchname
git clone https://github.com/USER_NAME/REPOSITORY_NAME.git
git branch <name of the branch> git push -u origin <name of the branch>
For example, if your branch is “develop”, you can enter the following command.
git branch develop git push -u origin develop
git checkout <name of the branch>
git status git add --a git commit -m "Add your comment here"
git checkout master git merge <name of the branch> git push -u- origin master
A remote (Git repository) can live on the web, on a shared network or even in a separate folder on your local computer.
To create a local copy of a remote:
git clone remote_location clone_name
remote_locationtells Git where to go to find the remote.
clone_nameis the name you give to the directory in which Git will clone the repository
See a list of a Git project’s remotes with the command:
git remote -v
To fetch work from the remote into the local copy:
To merge origin/master into your local branch:
git merge origin/master
To push a local branch to the origin remote:
git push origin <branch_name>