Git is version control system that you can record the current state of files.
So you can easily go back to previous state of file whenever you want.
let's look at several git command.
Staging area: The place that you put the file before commit
Repository: The collection of versions of committed files.
git add . # changes will move to staging area git commit -m"commit msg" # commit with commit msg git status # all the files that have been changed and staged git log --all --oneline # check commit records
If you keep writing code while committing, you may suddenly need to add a new feature.
In that case, you can add the code to the original file and commit it, but what if you make a mistake and the program you've written so far is broken?
If you want to safely add new features without worrying about that you can make a copy of the project and make changes there first.
git branch Apple # create new branch which name is Apple git switch Apple # move to branch Apple
- if you forgot the current branch, use git status
- HEAD in git log means your current location
I love my changes in the branch. Let's merge it into master or main branch!
If you want to merge
Back to the main/master branch and
enter git merge "branch name" to merge.
git switch main # back to main branch git merge Apple # merge Apple into main
if two branchs changed same file and same line of code Merge confict will happen
You need to choose either Current Change or Incoming Change and commit again
3-way merge
sfsfast-forward merge
rebase and merge
squash and merge
git branch -d Apple # if branch 'Apple' is not merged yet git branch -D Apple # if branch 'Apple' is already merged