git organize

1 Introduction to Git

Git is a free , open source distributed version control system for handling any project, small or large, with agility and efficiency.

1.1 The difference between Git and SVN

 

SVN

GIT

Is it distributed

non-distributed

distributed

storage method

file storage

Metadata storage

branch

A branch is just another directory

pointer

global version number

have

without

Content integrity

 

better than SVN

   

2 Installation and configuration

$ apt-get install git

$ git config --global user.name “Your Name”

$ git config --global user.email “[email protected]

3 local warehouse

The Git directory is divided into three areas:

working dir: the directory for operations

Temporary storage area (stage): In the .git directory, the add content is stored

Version area (HEAD): In the .git directory, information and content of each version are stored

 

3.1 Create a warehouse

 

3.2 Add files to the working directory

Add files on the working directory . Use git status to view the modification status

 

3.3 Add changes to the staging area

 

3.4 Submit changes to the version area

Use commit to submit changes, -m to bring submission instructions. After submitting, you can use the log to view the version record.

 

3.5 Modify the file again and submit

make changes to the file

 

Add and modify change1

 

Submit change change1

 

3.6 Undo changes

3.6.1 Restoring the workspace from the version area

Modify the workspace first, and then use the chekout -- <file> command to restore from the version area

 

3.6.2 Restoring the workspace from the staging area

Modify the file and add it to the temporary storage area, and then modify the file. At this time, you can use the chekout -- <file> command to restore the workspace from the temporary storage area.

 

3.6.3 Restoring the workspace and staging area from the version area

The command git reset --hard <commitid> restores the workspace and staging area from the version specified in the version area. <commitid> is the commit id, or version pointer.

Submit id: can be the first few digits of the id, but at least 4 digits

Pointer: HEAD current pointer

              HEAD^ previous version; HEAD^^ represents previous version, and so on

              HEAD~<N> previous versions

 

3.6.4 Summary

git reset --hard <commitid> restores the workspace and staging area from the version specified in the version area

git checkout -- <file> is to restore from the last add or commit

[Note] git checkout does not add --, it is to switch branches

3.7 Delete files

Use the git rm command to delete the file, and then git commit to submit it.

 

3.8 Branches

Branching in Git is done with pointers, so it is particularly efficient

3.8.1 Create a branch

git checkout -b <branch name> Create and switch to branch

git branch View the branch (* before the branch indicates the current branch)

 

3.8.2 Automatically merge branches

 

 

Make some changes on the dev branch first

 

merge dev on master

 

delete branch

 

3.8.3 Merge conflicts

If both branches have their own changes, the merge operation cannot be automated. Conflicts need to be resolved manually. There are two methods for merging branches: merge and rebase. Rebase is recommended.

3.8.4        merge

 

Modify readme.txt on dev

 

Modify readme.txt on master branch

 

Merge dev branch on master, prompt conflict

 

At this point, the readme.txt file becomes

 

Modify readme.txt to

 

Add and commit after resolving conflicts.

 

3.8.5 rebase

manual:

l Use git rebase to merge branches

l Modify the conflict. After the file is modified, use git add to add it

l Execute git rebase --continue to continue with the next conflict resolution until all conflicts are resolved

The branch before merging is as follows

 

After executing git rebase, each commit of the current branch will be cancelled, and they will be temporarily saved as pathch (in the .git/rebase directory), and then the branch will be updated to the new origin. Finally apply the saved patch to the branch.

 

The final effect, all commits are on a branch, as if no branch existed. The following figure shows the difference between rebase and merge.

 

4 Remote warehouse

4.1 Create a remote repository

 

4.2 Associate Remote Repository

 

4.3 Upload to remote warehouse

The format is git push –u <remote warehouse name> <local branch name>[:<remote branch name>]

[Note] The -u parameter is required for the first upload, and the -u parameter is not required for subsequent updates and modifications

[Note] If the remote branch name is the same as the local branch name, you can omit: and the following remote branch name

 

The dev branch can be seen on the remote repository.

 

4.4 Remote conflict

Because when a new remote warehouse was created, there were already changes (README.md).

First download with fetch, then merge, and finally push

# git fetch origin master

# git log -p master origin/master

# git merge origin/master

# git push origin master

4.5 Fork branch

fork from an existing repository

 

4.6 Clone branch

The command git clone downloads the remote branch to the local. If it is not the master branch, you need to add -b <branch name>

 

4.7 Push to remote

Modify the local repository first

 

Use git push <remote name> <local branch name>:<remote branch name>

 

Changes can be seen on the remote repository

 

4.8      Pull request (PR)

To merge the branch modifications from the fork to the source branch, a pull request needs to be submitted, and the person in charge of the source branch agrees to merge it.

Click New Merge Request on the iSource page

 

Fill in PR information

 

Click Merge on the source branch to modify the automatic merge

 

5 Summary

5.1 General Process

 

5.2 Common git commands

git init

Turn the current directory into a manageable git repository and generate hidden .git files.

git add <file>

Add file modifications to the staging area.

git rm <file>

delete xx file

git commit -m “XX”

Commit changes -m is followed by comments.

git status

Check warehouse status

git diff <file>

View what the XX file has modified

git log

View version history

git reflog

View the version number id of the history

git reset --hard <commitid>

              Recover from the version area. <commitid> can be a version pointer or a commit ID

              HEAD current version

              HEAD^ previous version. There can be multiple ^s here, each representing a rollback of a version

              HEAD~<N> means roll back N versions

              Commit ID can be the unique first few digits of Commit ID (at least 4 digits)

git checkout -- <file>

Undo all modifications made to the file in the workspace.

git checkout -b <branch name>

Create and switch to branch

git branch <branch name>

create branch

git branch

View all current branches

git branch -d <branch name>

delete the dev branch

git checkout <branch name>

switch to branch

git merge <branch name>

Merge the branch on the current branch

git stash

Hide the current work and resume work when the scene is restored later

git stash list

View a list of all hidden files

git stash apply

Recover hidden files, but the contents are not deleted

git stash drop

Delete Files

git stash pop

Recovering files also deletes files

git remote [-v]

View remote library name

-v list details

git remote add origin <remote repository>

Associate a remote library

git clone [-b <branch name>] <remote repository>

Clone from a remote repository.

-b <branch name> indicates which branch to clone. If not, the default master

git push [-u] <remote name> [<local branch name>][:<remote branch name>]

Push the local branch to the branch corresponding to the remote library

-u This parameter needs to be added for the first push, and it is not required in the future

If the remote branch name is omitted, it means that the local branch will be pushed to the remote branch with which there is a "tracking relationship" (usually the two have the same name)

If the local branch name is omitted, it means to delete the specified remote branch

Branch development principles:

If the local dev branch is modified, you need to switch to the master branch first, merge the modifications of the local branch back to the master (git merge dev), and then push the merged content to the master on the master; then switch back to the local branch, Then merge the contents of master back to the branch, and then continue to develop on the branch.



Development Process:

// view local branch

git branch

// Display all local and server branches

git branch -a

// Display the mapping relationship between the local branch and the server branch

git branch -vv



// switch branch (one -b parameter less than creating a branch)

git checkout {{branch_name}}


 // Create a new branch, the code of the new branch comes from the current branch

git checkout -b [branch name] 

// push the local branch code to the remote server, if the remote server does not have the branch, it will be created automatically;

git push origin [remote branch name]



// pull remote branch code to local current branch (recommended)

git pull origin master

If you only want to use git pull, you may need to establish a track relationship, then use
git branch --set-upstream-to=origin/<remote branch_name> <local branch_name> to
output the result: Branch h5_dev set up to track remote branch h5_dev from origin . 



// Merge the local master branch to the current branch, such as currently in the dev branch, merge the code on the master to the dev branch

git merge master

// Merge the remote master branch to the current branch

git merge origin/master



// delete the local branch

git checkout {{another_branch}}

git branch -d {{local_branch_name}}

// delete remote branch

git push origin --delete {{branch_name}}





#Assume there are two local branches : master and dev, use dev for local development

 

# Step1. dev.commit

git status #Current  branch , modified files

git diff    #specific modification

git commit -m "{COMMIT_MESSAGE}" .  # 提交

 

# Step2. master.update.merge-dev

git checkout master #Switch to master branch

git pull origin master:master   # 更新master

git merge dev #merge   the modifications of the dev branch

git push origin master:master #Submit   code to remote Git repository

 

# Step3. dev.update

git pull origin master:  dev #update dev

git checkout dev #Switch    back to dev for the next development



Common commands:

  1. git config --global user.name
  2. git config --global color.status auto
  3. git config --global color.diff auto
  4. git remote -v// View the git path of the remote branch
  5. git clone [email protected]/tv.git
  6. git status // View the status of the current version (modified or not)
  7. git add xyz // Add the currently modified file to the staging area or track new files (git add. Add all modified files to the staging area)
  8. git commit -m '{{update_msg}}'
  9. git rm xxx // remove the file from the current tracking list and delete it completely
  10. git rm -cached xxx// delete only in the temporary storage area, keep the file in the current directory, no longer track
  11. git mv old_name new_name// rename file
  12. git log
  13. git log -n
  14. git log --stat
  15. git show b4f44fe
  16. git diff

Code volume statistics

git log --author="pWX490102" --since ==2018-01.26 --until=2018-03-24 --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

Force rollback command:

git reset --hard e1da2c37bdaf3df52b9cf105ec7a7bd83f488bc1 (last commit number)

Pull code from master branch to new branch

git branch --set-upstream-to=origin/master appMarket-pyq

 

Pull the remote branch down to the local and establish an association track:

git checkout --track origin/jiangbo_dev
Branch jiangbo_dev set up to track remote branch jiangbo_dev from origin.

Switched  to a new branch 'jiangbo_dev'

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812065&siteId=291194637