Common git commands

Source: Internet
Author: User

Warehouse 1. Cloning a remote repository to a local

    • HTTP way: Git clone https://github.com/linus87/GitTest.git
    • SSH way: git clone[email protected]: Linus87/gittest.git
    • Git read-only way: Gitclone git://github.com/linus87/gittest.git

Generally speaking, both HTTP and SSH are supported by git, but not absolutely, if HTTP is not, then ssh. Vice versa.

If it is a custom directory, it might look like this:

git clone [email protected]:username/gittest.git my_gittest

Specifies the clone of repository, which will create all branches under this repository, but only one currently active branch is checked out.

2. Delete a warehouse

git remote Remove <repository-name>

3. Renaming a warehouse

git remote rename <old> <new>

View 1. View branch Status

Files that are not tracked will be displayed here, including the modified files.

git status

Files modified at stage:

git add project/src/main/webapp/css/tab.css

2. View the status of individual files

Git status--project/src/main/webapp/css/tab.css

3. View the commit log for a single file

Git log--project/src/main/webapp/css/tab.css

Comparison 1. Compare individual files in the work tree with their previous commit

Git diff--Project/src/main/webapp/css/tab.css

Submit 1. Discard changes made to the modified file (has been changed by the stage)

Git reset<paths>

Paths is the head by default and can be customized as a different file name and path.

In addition, using git reset on a file will remove a newly added file from the tracked record, which is the reverse command of git Add.

2. Submit Changes

Git commit–m "commit messages"

3. Files that are changed or deleted automatically at the time of submission

Git commit–a or git commit–all

4. Roll back the submission
Git reset--soft commits

Commits format: HEAD, head^, head~2, respectively, representing the last three commits.

Edit Index

The content of index refers to the change that is staged.

1. Stage new or changed files
git add project/src/main/*.css
Edit Workingtree

Working tree's leaf node is a file that has been altered (but not added to index, which is not staged).

1. Save local changes without adding index, and roll back to head
Git stash

Edition 1. Delete files from the work tree and also delete them from index
git rm--A.CSS

2. Remove the file from index, but keep the contents of the file and the changes
git rm--cached--A.CSS

3. Delete folders with files (all files not changed)
Git rm-r folder

4. Forcibly delete the folder (used if the file contains changes)
Git rm-r-F folder

5. Fetch and merge from another warehouse or local branch
Git pull Origin Master

Explanation: Origin is a repository name and master is a branch.

Branch 1. List Local Branches
Git branch

2. List the branches on the remote server
Git branch--remotes
3. Also list remote and local branches
Git branch-a
4. Create a branch
Git branch <branchname>

This is also a local creation of a branch, and after a successful local creation, a git push is required to commit to the remote server.

5. Set up tracking information

Git branch--set-upstream-to=origin/<branch>

6. Delete local Branch
Git branch (-d |-D) <branchname>

The difference between-D and-D is that the-D has the meaning of forced deletion, ignoring the merge state.

7. Delete local Remote branch
Git branch (-d |-D)-R origin/<branchname>

This command does not delete the real remote branch, just delete the local remote branch, and the next time git fetch/pull again from the real remote.

To delete a real remote branch, you have to log on to GitHub and delete the true remote branch on GitHub.

8. Modify the name of the branch
Git branch (-m |-m) [<oldbranch>] <newbranch>
Configuration 1. How to configure GIT to push only the current branch
git config--global push.default current

The value of the Push.default is possible with the following: Nothing,matching, upstream, current.

If you do not configure it, you must write it manually each time:

Git push Origin My-branch
2. Configure the display user name and email address
git config user.name Linus yangit config user.email [email protected]
3. Save the user name and password for the submission code
git config crediential.https://github.example.com.username Linus

The password section is slightly more complex and needs to be saved in two ways:

    • Save in cache:
    • Save on hard disk:

git config--global credential.helper ' store--store=~/.git-credentials '

If it is a single item, but the global setting is used:

git config credential.helper ' store '

Contents of the. git-credentials file:

Format: https://user:[email protected]
The following example uses token instead of pass (because 2FA is used):
Https://linus:[email protected]
Merge 1. Historical merging of the current branch
Git merge topic

Because Git is a distributed system, the history on the local and server may be inconsistent, so you need to merge the server and the local.

Topic refers to a named commit on the local branch.

2. Abandonment of the merger
git merge--abort

Usage Prerequisites : Before using git merge, there is no commit change. Otherwise, git cannot be restored.

Working directory 1. Convert work Branches
git checkout <branchname>git Pull

First, replace the current work branch with Git checkout, and then use git pull to fetch the latest code on the server.

Sometimes the direct git pull is not valid, try using the following command:

Git pull Origin <branchname>
2. Merging two different branches
git checkout <branchname>git Pull

First checkout another branch to the local and get the latest code, then go back to the current branch

Git merge<branchname>

You can use the merge again.

Both the main branch and the Sub-branch develop at the same time 1. Secondary Branch merge Main branch

Suppose the current branch is topic:

A---B---C topic

/

D---E---F---G master

Topic as sub-branch, master Main branch

git rebase master topic

The following will be:

A '--b '--c ' topic

/

D---E---F---G master

Modify Project Repository Source
git remote Set-url origin git://github.scm.corp.ebay.com/my-org/my-repo.git

Common git commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.