[For Begginer] What git is?
1. Git Basic Knowledge
1.1 Repository
First, we call a whole of file to manage project 'repository'. Repository include some file like sorce code, data set, file related, log, etc..., git manage to a repository for proper and efficient work.
1.2 Commit
We using 'commit' to reflect modify of repository. Git saves history of changes of repository in each time.
1.3 Branch
When we want to make another application, we can create 'branch'. New branch is defined independently, creates another flow from master(main) branch.
This makes it possible to keep both original repository and new repository that has new application.
1.4 Merge
After creating new application, we can return new branch to main branch. This allows multiple people to implement separate features and integrate them into the master branch, making multi-person development more efficient.
2. Setup
2.1 Install
Assuming environment is mac.
- Check whether git is installed
git --version
# or
git version
If git isn't installed
2. Installation by brew
brew install git
brew --prefix git
# output
- Configuration of path
# XXXX → path outputted at above
echo 'export PATH="XXXX/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
2.2 Registration
- Register e-mail
git config --global user.email "sample@gmail.com"
- Register name
git config --global user.name "sample"
3. Commit
3.1 Define new repository
- Move to directory want to manage by git
cd git-sample
- Initialize as main repository
git init
- Check now status
You can confirm that you are in the initial state masterbranch.
git status
# On branch master
#
# No commits yet
#
# nothing to commit (create/copy files and use "git add" to track)
3.2 Prepare to commit
Git has place to prepare to commit called 'stage', files in stage are committed when 'git commit'.
The operation up file to stage is called 'staging' or 'add'. It can be done with the code below:
git add code1.py
If you want to add all of files in now directory, you can execute like this:
git add .
Here, git is determining whether it needs to track the history of each file.
Once a file is added to the stage, its history is tracked unless you reconfigure it.
By the way, if you change code1.py after add, the data only at the time of adding is still stacking stage and it will be committed when you commit.
If you want to commit data after committed, you have to commit once again.
3.3 Commit
Below code is used for commit. Commit always rquires message by -m.
git commit -m "first commit"
# [master (root-commit) ae8832c] first commit
# 1 file changed, 1 insertion(+)
# create mode 100644 code1.py
A part of "ae8832c" is hash value. git recognize commit with this value. We can confirm about commit by git log.
git log
# commit ae8832cd049407c5eedb9360f7d1c9db09e642ef (HEAD -> master)
# Author:
# Date: Sat May 4 16:33:13 2024 +0900
#
# first commit
3.4 Ignore files
There are some files in the repository that do not need to be tracked, such as log files. In such a case, create a '.gitignore' file and write '/file_name' for files and '/directory_name/' for directories so that git will no longer track that data.
/file_name
/directory_name/
Let's also add .gitignore.
git add .gitignore
4. Branch
4.1 Create new branch
This code is used for make new branch and checkout from now branch.
A word following -b will be a new branch name.
git checkout -b feature/user_login
When a moment that execute above code, the branch is switched to a new branch, and let's make/commit some file.
nano login.py
git add login.py
git commit -m "add login.py"
And let's go back to the master branch.
git checkout master
At this point, login.py has disappeared from the file.
4.2 Merge branch
Next, we merge feature/user_login branch to master.
At next code,
git merge feature/user_login
You can confirm existing in master branch by 'git status'
git status
# On branch master
4.3 Rewind the data
When you make a mistake and want to return data, you can use 'git reset' or 'git revert'
Return data to previous commit
After get a hash value by 'git log' and reset by using it. Please note that there is no history of reverting to a previous version.
git log
git reset --hard 5a45d2cc1fbdd68d552ca8a40b5b549cc530f03e
Undo the previous commit
If you want to cancel a previous commit instead of reverting, use the following:
git revert 5a45d2cc1fbdd68d552ca8a40b5b549cc530f03e
Summary
This time, I explained about git usage.
Thank you for reading.
The next article is published: How to use github?
Discussion