IT

A short Git&github tutorial for beginners

A short Git&github tutorial for beginners

Posted by xuepro on May 4, 2020

git tutorial

youtube Video 视频

What is git?

Why git?

How to track the modification history of a file?

You might be tracking the revision history of a file with a different filename. For example:

work.doc
Work_2019_3_8.doc
Work_2019_9_5.doc
Work_2020_1_23_john.doc
Work_2020_2_1_smith.doc

If a file is modified many times, it is difficult to tell the difference between these different versions of the file

What git can do?

Git is a free and open source distributed version control system(VCS). Git use a repository to record all changes of all files of a project and can roll back to any verskion at any time. Every team member can clone a local copy called snapshot of a git repository and do work on local repository. Local repository can be pushed to remote repository on other place (shch as another machine or other place in his machine).

  • Track changes of all files of a project
  • Coordinates works among multiple developers
  • Who made what changes and when
  • Roll back to the version of any time
  • Local snapshot and remote repository

A Short History of Git

Linus Trovaldus, the creator of linux kernel, invent the git.

For most of the lifetime of the Linux kernel maintenance (1991–2002), changes to the software were passed around as patches and archived files. In 2002, the Linux kernel project began using a proprietary DVCS called BitKeeper.

in 2005, the relationship between the community that developed the Linux kernel and the commercial company that developed BitKeeper broke down, and the tool’s free-of-charge status was revoked. This prompted the Linux development community (and in particular Linus Torvalds, the creator of Linux) to develop their own tool based on some of the lessons they learned while using BitKeeper. 

The goal of git is :

  • Speed

  • Simple design

  • Strong support for non-linear development (thousands of parallel branches)

  • Fully distributed

  • Able to handle large projects like the Linux kernel efficiently (speed and data size)

install git

Linux(Debian, Ubuntu)

     sudo apt-get install git

Linux(Fedora, CentOS)

     sudo dnf install git-all

Mac

     https://git-scm.com/download/mac

Windows

     https://desktop.github.com/

git config

The git config command allows you to configure your Git settings. These settings can be system wide, user or repository specific.

  • git config –system store system setting in file “/etc/gitconfig
  • git config –global store user settings in the .gitconfig file located in the user home directory. This is also called the global Git configuration. Git stores the committer and author of a change in each commit.
  • git config –local store repository specific settings in the .git/config file of a repository

You have to configure at least your user and email address to be able to commit to a Git repository because this information is stored in each commit.

git config --global user.name “your name" 
git config --global user.email "your.email”

For example:

git config --global user.email "john@example.com"
git config --global user.name "john"

unset

# Remove repository configuration
git config --unset [key]
# Remove global configuration
git config --global --unset [key]
# Remove system configuration
git config --system --unset [key]

Query Git settings

$git config --list
$git config --global --list

local Git workflow

add adds your modified files to the queue to be committed later. commit commits the files in the index to the repository

reset resets the index without touching the working tree, while checkout changes the working tree without touching the index.

  1. Create a directory and enter the directory
  2. Create a Git repository in the current directory: git init
  3. Create new file or modify content of some file
  4. Add change to Stage(Index): git add .
  5. Commit staged changes to the repository : git commit –m”comments”
  6. See the current status of your repository: git status untracked, staged, committed
  7. View commit history in the repository: git log
  8. Viewing the changes of a commit: git show
  9. Roll back to some submit : git reset

An example of local Git workflow

  1. Create a directory and enter the directory
    cd ../..
    mkdir myproject
    cd myproject
    
  2. Create a Git repository in the current directory: git init
    git init
    
  3. Create new file or modify content of some file
    ls
    touch A.txt
    ls
    
  4. Add change to Stage(Index): **git add . **
    git add . 
    
  5. Commit staged changes to the repository : git commit
    git commit -m"create A.txt"
    
  6. See the current status of your repository: git status

modify the A.txt file

vim A.txt

you can type i in keyboard to enter insert or edit mode and type something such as “first” in the file.

to save youe modification, type “:” colon and “wq” .

run the git status

git status

you will see status message : Changes not staged for commit:

run

git add .
git status

you will see status message : “Changes to be committed:”.

run:

git commit -m"first modification"
git status
  1. git show
git show
  1. Now we add another file
    touch B.txt
    git status
    

    you will seee Status: untracked,

git add B.txt
git status
git commit - m"create B.txt"
git status
  1. git log to see all version history
    git log
    

    you will seee different version Id.

  2. git reset to roll back the version of any time

git reset --hard HEAD^2

to check if we roll back to the first version

cat A.txt
git log

working with branchs

Branch operation allows creating another line of development. We can use this operation to fork off the development process into two different directions.Branching means diverging from the mainline and continue to work separately without messing with the mainline.

# list all branches
git branch
# breate a new branch
git branch <branch>
#delete a branch
git branch -d <branch>
git branch -D <branch>
#switch between branches in a repository
git checkout <branchname> 
# you create and switch to a new branch
git checkout -b <branchname>  

For example:

git branch
git branch another
git checkout another
vim A.txt
# do some modify ,such as add a new line
git add .
git commit - m"add a line:hwdong"
git check master
git branch -d another  # error: The branch 'another' is not fully merged.
git branch -D another  # Deleted branch another (was b98579f).

now breate and checkout a new branch

git checkout -b another
vim A.txt
#do some modification
git add .
git commit -m"add: hwdong"
git checkout master
git merge another
git branch -d branch #Deleted branch another (was 254366f).

continue:

git checkout -b branch
vim A.txt
#do some modification
git add .
git commit -m"add: hwdong.net"
git checkout master
vim A.txt
#do some modification
git add .
git commit -m"modified A"
git merge another #CONFLICT (content): Merge conflict in A.txt

you need to modifed the conflicted file in the master branch and the merge

git add .
git commit -m"coflict solved"
git merge another
git branch -d another

Git Checkout:

git branch
git checkout -b new_breanch
git branch

The branch name with the asterisk next to it indicates which branch you’re pointed to at that given time.

Now, if you switch back to the master branch and make some more commits, your new branch won’t see any of those changes until you merge those changes onto your new branch.

git checkout master
git branch

The -b option is a convenience flag that tells Git to run git branch before running git checkout .

  1. Merge one branch to another branch: git merge

# Start a new feature
git checkout -b new-feature master
# Edit some files
git add <file>
git commit -m "Start a feature"
# Edit some files
git add <file>
git commit -m "Finish a feature"
# Merge in the new-feature branch
git checkout master
git merge new-feature
# delete branch new-feature
git branch -d new-feature

working with github (Remote repositories)

A remote repository on a server typically does not require a working tree. A Git repository without a working tree is called a bare repository. You can create a bare repository with the –bare option.

 git init --bare

If you use github as server,you can create a git repository with you github account.

1. git clone

The git clone command creates a new git repository by copying an existing Git repository located at the URI you specify. This copy is a working Git repository with the complete history of the cloned repository. It can be used completely isolated from the original repository.

git clone https://github.com/hwdong-net/test.git

If you clone a repository, Git implicitly creates a remote named origin by default. The origin remote links back to the cloned repository.

cd test
ls

2. do some work in local repository

touch A.txt
git status
git add . 
git status
git commit - m"create A.txt"
git status

3. push changes to this repository

The git push command allows you push changes to this repository to remote remote repository.

git push <repo name> <branch name>

<repo name> is the local git repository,<branch name> is the branch of remote remote repository

for example:

git push -u origin master

You can simply use git push as Git uses origin as default <repo name> and master as default remote branch.

git push

Of course, pushing to a remote repository requires write access to this repository.

If there is a conflict related with use account,you can delete github user account on windows.

start menu -> windows -> control panel -> User Accounts->Credential Manager
                ->Windows Credential ,delete the github account

you can do modify the repository directly in github, for example modify the A.txt file,for example all a line “hello world” to the file.

click the submit button.

4. git pull

Then you can use git pull in your local machine to pull the modification from the github repository.

git pull
cat A.txt

5. Adding remote repositories

Use the git remote add command to add a new remote, in the directory your repository is stored at.

The git remote add command takes two arguments:

  • A unique remote name, for example, “my_remote_repo”
  • A remote URL, which you can find on the Source sub-tab of your Git repo

For example,to set a new remote:

git remote add origin https://github.com/user/repo.git

to Verify new remote

git remote -v

output:

> origin  https://github.com/user/repo.git (fetch)
> origin  https://github.com/user/repo.git (push)

After some work on this local git repository,you can push it to github server.

git push -u origin master

What is Git and why should I use it?

An Intro to Git and GitHub for Beginners (Tutorial)

The Best Git Tutorials

The Git Push Command Explained

https://www.atlassian.com/git/tutorials/

How to remove git account from local machine and add new account

git的reset和checkout的区别

Git & GitHub Crash Course For Beginners

键盘上所有特殊符号的英文读法


支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!