Git is a version control system that allows you to keep track of changes made to a project over time.
GitHub is a popular hosting service for Git repositories. GitHub allows you to store your local Git repositories in the cloud. With GitHub, you can backup your 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 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 "yourname@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
ls ~/.ssh/id_rsa.pub
If you receive a message “No such file or directory”, you’ll need to create a new one. Create SSH To create an SSH key, run the following command
ssh-keygen -C <youremail>
cat ~/.ssh/id_rsa.pub
A Git project has three parts:
git init
initialize a new Git repositorygit status
check the status of changesgit add filename
adds files from the working directory to the staging areagit add filename_1 filename_2
add multiple files to the staging area with a single command:git diff filename
shows the difference between the working directory and the staging areagit commit
permanently stores changes from the staging area inside the repository. The option -m
followed 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, a HEAD commit is the commit you’re currently on.
git checkout HEAD filename
restores 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 filename
unstages file changes in the staging area.git reset commit_SHA
resets 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
.
git branch new_branch
git checkout branch_name
All the commands you do on main branch, you can also do on this branch.
git add filename
git commit -m "Commit message"
Firstly, we need to switch to the main branch git checkout main
Then use this command to merge the new branch to a main branch git merge branch_name
.
After the branch has been integrated into the main, you can delete it.
git branch -d branch_name
If the feature branch is never merged into main 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 main
git merge <name of the branch>
git push -u- origin main
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_location
tells Git where to go to find the remote.clone_name
is the name you give to the directory in which Git will clone the repositorySee a list of a Git project’s remotes with the command: git remote -v
To fetch work from the remote into the local copy: git fetch
To merge origin/main into your local branch: git merge origin/main
To push a local branch to the origin remote: git push origin <branch_name>
.
Resources: