Git

init, add, commit, checkout, branch, push, pull request, pull, logs, fetch

Naranjito 2021. 6. 13. 20:46

$ git init
Initialized empty Git repository in /Users/joohyunyoon/workspace/practice/vendingmachine/.git/

$ git add *

$ git commit -m "message"
[master (root-commit) 17d9543] message
 2 files changed, 165 insertions(+)
 create mode 100644 README.md
 create mode 100644 vendingmachine.ipynb
 
 $ git remote add origin https://github.com/mellamonaranja/vendingmachine.git
 
 $ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 1.53 KiB | 1.53 MiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/mellamonaranja/vendingmachine/pull/new/master
remote: 
To https://github.com/mellamonaranja/vendingmachine.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

 

  • git init

The area(.git) that stores every edit history in this folder from now on. 

"Hey, we noticed you created a new file called vendingmachind, but unless you use the 'git add' command we aren't going to do anything with it."

 

  • git add -A or git add *

All, I want to back up all the files.

Commit all the files what I edit, delete, add...

$ git commit -a
[hotfix f1afbf0] add output 'tell your world'
 1 file changed, 1 insertion(+)
$ git status
On branch hotfix
nothing to commit, working tree clean

 

  • git commit -m " "

A record of what changes you have made since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those changes into a commit.

Whenever there is meaningful change, do commit. It only records modification history.

 

  • git checkout -b 

This command will automatically create a new branch and then 'check you out' on it, meaning git will move you to that branch, off of the primary branch.

Let's say you are on the primary branch and want to create a new branch to develop your web page.

$ git checkout -b my-new-branch
Switched to a new branch 'my-new-branch'

 

  • git branch

After create the new branch, check that new branch was created.

The branch name with the asterisk(*) next to it indicates which branch you're on at that given time. 

$ git branch
  master
* my-new-branch

 

  • git branch name

Create the 'name' branch. (I created 'hotfix' and there are two branches-hotfix, master)

$ git branch hotfix
$ git branch
  hotfix
* master

 

  • git push origin

To push changes onto a new branch on GitHub. GitHub will automatically create the branch for you on the remote repository.

origin : When you clone a remote repository to your local machine, git creates an alias for you. This alias is called 'origin'. It's essentially shorthand for the remote repository's URL. 

$ git push origin my-new-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'my-new-branch' on GitHub by visiting:
remote:      https://github.com/mellamonaranja/newrepo/pull/new/my-new-branch
remote: 
To https://github.com/mellamonaranja/newrepo.git
 * [new branch]      my-new-branch -> my-new-branch

 

  • pull request

I want to make some changes to repo's owner.(on Github website)

 

  • git pull origin master or git pull

In order to get the most recent changes that you or others have merged on GitHub. 

This shows you all the files that have changed and how they've changed.

$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/mellamonaranja/newrepo
 * branch            master     -> FETCH_HEAD
   70af5e1..744b472  master     -> origin/master
Updating 70af5e1..744b472
Fast-forward
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md

 

  • git logs

Show me the commit history

 

  • git reset --hard 3r8da0

It restores of 3r8da0

 

  • git merge "branch"

If I am on master branch now, then bring the "branch" to here(master branch) and merge it with master branch.

$ git merge hotfix
Updating 4c8868a..f1afbf0
Fast-forward
 hello.py | 1 +
 1 file changed, 1 insertion(+)

 

  • git remote add origin 
$ git remote add origin https://github.com/mellamonaranja/palindrome.git

 

  • git fetch

It lets you to downloads commits, files, and refs from a remote repository into your local repo. But it doesn't force you to merge the changes into your repository.

$ git fetch
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/mellamonaranja/palindrome
 * [new branch]      main       -> origin/main

 

  • git checkout name

Designate the 'name' branch what I want to use, in order words, bring 'name' name branch to current place.

$ git checkout hotfix
Switched to branch 'hotfix'

 

  • git checkout -b name

branch + checkout

$ git checkout -b hotfix

 

  • git status

Show me the current status.

It has been changed from master to main.

$ git status
On branch main
Your branch is up to date with 'origin/main'.
...

 

  • How to prevent useless files using with .gitignore

https://www.toptal.com/developers/gitignore --> https://www.toptal.com/developers/gitignore/api/python

$ touch .gitignore
$ ls -a
.		..		.git		.gitignore	hello.py
$ vim .gitignore
$ git add .gitignore 
$ git commit -m 'added '.gitignore' file'
[hotfix 61b1f7b] added .gitignore file
 1 file changed, 145 insertions(+)
 create mode 100644 .gitignore