Staged: if we are in this state, it means that the files with the updated changes are marked to be committed (added) to the local repository but not yet committed.
Modified: it means that our source code is not yet stored at the local repository.
Committed: it means that we already committed to local repository but the source code is not yet committed to remote site
git add: add files to the staging area
git commit: commit files to the local repository
git push: push the committed files to the remote repository
git fetch: get the file from the remote repository to local repository but not into the working folder
get merge: merge the files from local repository with the working folder.
git pull: get files from the remote repository and update to local repository then merge with the working folder. It is equivalent to execution two steps git fetch then git merge.
Install git
configure Git:
we nee to provide the username of git
$ git config --global user.name "YOUR_USERNAME"
provide the email
$ git config --global user.email "im_satoshi@musk.com"
$ git config --global --list # To check the info you just provided
setup ssh
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
create a new repository and a local repository => new a folder
go into the folder then initialize git
$ git init
create a readme.md then add to staging area
$git add readme.md
or
$git add . # to add all files changed
view status
$git status
commit code
$git commit -m "Message here"
Now we have some errors in the code then want to roll back. So we will unstage the files we just added in to the staging area.
$git reset HEAD~1
After fixing the bugs, we can commit again
Add a remote origin and push
Recently, all the changes are updated local repository, we need to update to the remote repository.
$git remote add origin remote_repository_URL
So what is the remote_repository_URL?
we use
$git remote -v
to list the remote connections you have to other repositories.
Now we can push the update to the remote repository
$ git push -u origin master # pushes changes to origin
See the change: To show the files changes not yet staged
$git diff
Revert to the last commit version
$git checkout .
if we want to specify any file we use
$git checkout --filename
View commit history
$git log
$ git add .
$ git status # Lists all new or modified files to be committed
$ git commit -m "Second commit"
$ git push -u origin master
How can we work with others in the same repository? So to make sure those changes are reflected on my local copy of the repo:
$git pull orgin master
Then we have to consider fetch and merge by ourselves or we can use git pull that automatically merges the commits without letting you review them first.
When you git fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files. To integrate the commits into your master branch, you use git merge.
.gitignore tells git which files (or patterns) it should ignore.
In some cases, we already modified the source code at local and made it broken. We want to reupdate again the source code from the remote repository. We need to do some following steps
revert to the previous commit by <git checkout .> ( has a dot and the end)
reset the local repository by <git reset>