Git Learning - By reading ProGit

时间:2022-12-04 04:32:53

Today I begin to learn to use Git.
I learn from Pro Git. And I recommend it which is an excellent book.
Some Ideas:

Git is a Distributed Version Control System and it is brilliant.
And we know that DDoS attack is famous and distributed as well.
So, distributed things may be very wonderful.

Notes:

  • Git regard the whole directory as a filesystem and create snapshots for it, not for single file
  • Files in the respository may be in one of four types: untracked, tracked:modified, tracked:staged, tracked:committed
  • Git uses SHA-1 Hash Algorithm
  • .git is the most important part of Git, and it is what is copied when you clone a repository from another computer

Git Learning - By reading ProGit

Git Learning - By reading ProGit


# Configure

git config --global user.name "brant-ruan"
git config --global user.email xxxx@xx.xx
git config --global core.editor vim # specify editor
git config --list # show your configurations


# Init

# Go into a new directory which you will use as a respository
git init # create a .git directory
# Then I write a helloworld program named "hello.c"
# And I write a plain text README
# Each file in your working directory can be in one of two states: tracked or untracked.
# Tracked files were in the last snapshot(they can be unmodified, modified, or staged)
git add *.c
git add README
git commit -m 'initial project version'


# Clone

git clone URL [optional: your directory name]
# GIt has many transfer protocols you can use


# Check Status

git status # check status of your files
# Git stages a file as it is when you run the [git add] command.
# now I add a new sentence in README and stage it:
git add README


# Ignore files

# you may have a class of files that you don't want Git to automatically add or show you as beingg untracked.
# and you can create a file listing patterns to match them named .gitignore:
vim .gitignore
*.[oa]
*~
!lib.a # means that lib.a will be showed and not ignored


#View Your Staged and Unstaged Changes

git diff
git diff --staged
# git diff --cached to see what you've staged so far.


# Commit

git commit -m "commit message"
# it will output:
[master 9932eed] second commit
1 file changed, 4 insertions(+)
# the 9932eed is the result SHA-1 checksuming the commit
git commit -a -m 'message'
# Git automatically stage every file that is already tracked, and you can skip git add


# Remove files

git rm filename
# then commit
git rm --cached README# just rm it from your staging area (still on your disk)


# Rename a file in git repository

git mv file_src file_dst


# View the commit history

git log
git log -p # show difference introduce in each commit
git log --stat # show modified files


# Undo things

# Undo commit
git commit --amend [-m 'string']
# This command takes your staging area and uses it for the commit

# Unstage a staged file
git reset HEAD filename

# Unmodify a modified file
git checkout -- filename # Attention ! That may be dangerous !


# Work with Remotes

cd respository-path # you have cloned a respository here before
git remote # to see which remote servers you have configured
git remote -v # show URLs to be used when reading and writing to that remote

git remote add [shortname] [url] # add remote repositories
e.g.
git remote add pb https://github.com/paulboone/ticgit

# fetch
git fetch pb

# If you clone a repository, the command automatically adds that remote
# repository under the name “origin”. So, git fetch origin fetches any new
# work that has been pushed to that server since you cloned (or last fetched
# from) it.
# git fetch command pulls the data to
# your local repository – it doesn’t automatically merge it with any of your work
# or modify what you’re currently working on. You have to merge it manually into
# your work when you’re ready.
# git clone command automatically sets up your local master branch to
# track the remote master branch (or whatever the default branch is called) on
# the server you cloned from

# push to remotes
git push [remote-name] [branch-name]
e.g.
git push origin master

# inspect a remote
git remote show [remote-name]

# remove and rename remotes

git remote rename [old-shortname] [new-shortname]
git remote rm [short-name]


# Tag

git tag # list tag
# create tag
# Git uses two main types of tags: lightweight and annotated.
# A lightweight tag is very much like a branch that doesn’t change – it’s just a
# pointer to a specific commit.
# Annotated tags, however, are stored as full objects in the Git database.
# They’re checksummed; contain the tagger name, e-mail, and date; have a tag-
# ging message; and can be signed and verified with GNU Privacy Guard (GPG).
# It’s generally recommended that you create annotated tags so you can have all
# this information; but if you want a temporary tag or for some reason don’t want
# to keep the other information, lightweight tags are available too.

git tag -a v1.4 -m 'my version' # annotated tags
git show v1.4
git tag v1.4-lw # lightweight tags
# share tag:
git push origin [tagname]