Git command - Details

First, configure the account, pay attention to the needs and Github account settings as

git config --global user.name xxx

git config --global [email protected]

Second, view the configuration

git config --list

user.name=xxx

[email protected]

Third, create a Git repository

First, create a new local warehouse

$ git clone http://git.dayuan.cc/practice/git-exmple.git

cd git-exmple

$ git remote add origin http://git.dayuan.cc/practice/git-exmple.git 、

$ git add .

$ git commit -m "Initial commit"

$ git push -u origin master

Second, create a warehouse in an existing directory

cd existing_folder

$ git init

$ git remote add origin http://git.dayuan.cc/practice/git-exmple.git

$ git add .

$ git commit -m "Initial commit"

$ git push -u origin master

Third, pull the remote branch and create a local branch

// dev2 for remote branch, dev1 local branch

$ git checkout -b dev1 origin/dev2;

Dev pulled from the remote to the local branch and create a local branch dev, and establishes a mapping relationship between two persons, while the current branch will switch to dev1

// dev2 for remote branch, dev1 local branch

$ git fetch origin dev2:dev1;

Using this new approach will branch dev1 locally, but does not automatically switch to the local branch dev1, manually checkout. The use of this method to establish a local branch and a remote branch does not establish the mapping.

Fourth, the local existing warehouse pushed to the remote repository

cd existing_repo

$ git remote rename origin old-origin

$ git remote add origin http://git.dayuan.cc/practice/git-exmple.git

$ git push -u origin --all

$ git push -u origin --tags

Fourth, view the status Git

git status

Generally displays the file (uncommited) are not required to submit and track files (untracked)

uncommited: already, just not yet submitted a modified

untracked: not original, new

View branch-related commands

$ Git branch -r; // Check remote branch

$ Git branch; // Check local branch

$ Git branch -a; // View all branches

Mapping relationship between the local branch and a remote branch (or to track the relationship track)

Such use git pull or every time it is not necessary to specify which branch from the combined pulling and pushed to the remote which branch when the remote git push.

$ git branch -vv

Output mapping relationships

// dev remote branch name

$ git branch -u origin/dev

The current local branch and a remote branch establish the mapping

$ git branch --unset-upstream

Revocation of the current mapping between the local branch and a remote branch

Switching Current local branch

// dev local branch name

$ git checkout dev;

Remote pull branch code

The premise of using the current branch need to establish the mapping between remote and branch

Push local branch code to the remote branch

$ git push

The premise of using the current branch need to establish the mapping between remote and branch

Merge branch

There are dev local branch and a remote branch, master local branch and a remote branch will now be branching dev code into the master trunk

1. dev switch to the local branch, and pull on the pulling at the branches changes place remote dev

2. All local modifications made to commit and push on the remote dev branch, to ensure that no omissions, ensure that the current local and remote dev dev is consistent

3. The current local switch to the local branch master

4. The merged local branch to a local master dev

5. The local branches have been merged dev push to be the master remote master probably thinking that's it. Note that during the merge (merger) when you need to disable fast-forward mode

Specific merge command: git merge --no-ff dev (dev branches merged local name)

Auxiliary branch

feature branches:

Develop branch from branch to build a feature, feature and switch to the branch

$ git checkout -b myfeature develop

Switched to a new branch "myfeature"

Merge feature branch to develop:

$ git checkout develop

Switched to branch 'develop'

$ git merge --no-ff myfeature

Updating ea1b82a..05e9557

(Summary of changes)

$ git branch -d myfeature

Deleted branch myfeature

$ git push origin develop

Above we merge the branch when using the parameters --no-ff, ff is the fast-forward means, - no-ff is to disable fast-forward.

release branch

New rlease branches:

$ git checkout -b release-1.2 develop

Switched to a new branch "release-1.2"
$ ./bump-version.sh 1.2

File modified successfully, version bumped to 1.2.

$ git commit -a -m "Bumped version number to 1.2"

[release-1.2 74d9424] Bumped version number to 1.2

1 files changed, 1 insertions(+), 1 deletions(-)

release branches merged into the master branch:

$ git checkout master

Switched to branch 'master'

$ git merge --no-ff release-1.2

Merge made by recursive. (Summary of changes)

$ git tag -a 1.2

release branch into the develop branch

$ git checkout develop

Switched to branch 'develop'

$ git merge --no-ff release-1.2

Merge made by recursive. (Summary of changes)

Delete release branch

$ git branch -d release-1.2

Deleted branch release-1.2 (was ff452fe).

hotfix branch

New hotfitx branch

$ git checkout -b hotfix-1.2.1 master

Switched to a new branch "hotfix-1.2.1"

$ ./bump-version.sh 1.2.1

Files modified successfully, version bumped to 1.2.1.

$ git commit -a -m "Bumped version number to 1.2.1"

[hotfix-1.2.1 41e61bb] Bumped version number to 1.2.1

1 files changed, 1 insertions(+), 1 deletions(-)

Fix bug

$ git commit -m "Fixed severe production problem"

[hotfix-1.2.1 abbe5d6] Fixed severe production problem

5 files changed, 32 insertions(+), 17 deletions(-)

After buffix, hotfix incorporated into the master

$ git checkout master

Switched to branch 'master'

$ git merge --no-ff hotfix-1.2.1

Merge made by recursive.

(Summary of changes)

$ git tag -a 1.2.1

hotfix merge to develop branch

$ git checkout develop

Switched to branch 'develop'

$ git merge --no-ff hotfix-1.2.1

Merge made by recursive.

(Summary of changes)

U delete the hotfix branch

$ git branch -d hotfix-1.2.1

Deleted branch hotfix-1.2.1 (was abbe5d6).

Fifth, Git add files to the staging area (repository needs and differentiate)

git add

Six, Git submission

git commit -m "add a function in test.java"

-m indicates a comment, to illustrate when submitted, must have!

Seven, Git delete files (folders)

git rm test.txt // delete files

git rm -r filebook // delete folders

The distinction between direct and git rm delete git rm that this will delete log files, delete and just delete the physical file, and the file related records are not deleted. git rm will produce the difference in the repository (an operating log), but not delete.

git rm test.txt => git commit -m 'delete a file'rm test.txt => git commit -am' delete a file 'Note: git rm command is used to delete a file. If a file has been submitted to the repository, so you never have to worry about mistakenly deleted, but be careful, you can only restore files to the latest version, you will lose the contents of the most recent commit your changes.

Eight, Git operation log

git log --decorate --graph --oneline --all # Display the version number of the current and previous

git log --pretty = oneline # version history will appear on one line, all the historical version number is displayed

git log --pretty = oneline --abbrev-commit # Version History will appear as a line, part of the history of the version number displayed

git log --graph # View branch merged graph

Nine, version rollback

After performing version return, local content and work area will automatically fall back to the repository versions of content synchronized

git reset --hard HEAD ^ fall back to the previous version

git reset --hard HEAD ^^ fall back to the previous version, and so on, is the first to submit a version

git reset --hard e9efa77 fallback version e9efa77

In this way the fallback code obvious drawbacks, information submitted after the rollback version will lose, so some companies banned the use git reset command to roll back the code

Therefore, Git Revert was born!

git revert role by doing anti create a new version, this version of the content and we retreated to return to the target version of the same, but HEAD pointer is a pointer to the new generation version, rather than the target version.

$ git revert -------

Of course, we can also batch fallback!

$ git revert OLDER_COMMIT^..NEWER_COMMIT

Wrong submitted C and D remain, and time in the future also be thrown pot according to follow. And this approach, it is encouraged by the enterprise.

Ten, Git staging area undo

Modify the workspace file, and perform the add, but did not perform commit, staging area or revocable

git reset HEAD readme.txt

Note: git reset command either rollback version, can also modify the temporary area to fall back to the work area. When we use HEAD, represent the latest version.

Original: https: //www.jianshu.com/p/559684b6fdd9

Guess you like

Origin www.cnblogs.com/PoetryAndYou/p/12186196.html