Basic GIT operations

Create/clone a repository

  1. Clone the remote GIT repository to the local directory.
Git ://url/projectName && git config -f. git/config user.name yourName && git config -f  .git/config user.email [email protected]Copy the code

git config -f

  1. Create a new project
mkdir projectName
cd projectName
git init
Copy the code

Create the project directory and initialize it with git init to generate the.git directory.

Push local changes to a remote repository

Check out the code from the remote GIT repository to the working directory, and when we complete a phase of development, we need to log the changes and push them to the remote directory.

git add <fileName> / git add .
git commit / git commit -m "commitMessage"
git push origin branchName
Copy the code
  • Do local development
  • git addKeep track of the required files;
  • git commitMake submissions and write submission information;
  • git pushPush the commit to the remote repository.

View the local repository status

Files in the working directory are classified into traced and untraced states. Git Status allows you to view the tracking status of a file (cached, uncached, and untracked), that is, the progress of git add and Git Commit operations.

$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/test.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/test.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/readme.md
Copy the code
  • Changes to be committed:The following file is the tracked state;
  • Changes not staged for commit:The trace file has changed, but has not been added to the staging area.
  • Untracked files:The following files are untracked.

You need to use Git Add to add changed traced files to the staging area; You can also change untracked files to tracked state and add them to the staging area.

The.gitignore file contains files you want GIT to ignore.

After git commit, the working directory will be the same as the last commit.

On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Copy the code

View the historical commit record

Git Log lists all commits in chronological order, with the most recent updates at the top.

Git log <-limit> Git log-2 git log — stat-2 Displays the last two commit records. Limit Specifies the number of submission times. The following parameters can be used.

Git log -p or git log –patch displays all the differences (diff) for each commit.

Git log –stat lists all files that were modified, how many files were modified, and which lines of the modified files were removed or added at the bottom of each commit.

Git log –name-status Displays a list of newly added, modified, and deleted files.

Git log –oneline shows a single commit in oneline.

Cancel the operation

1. Undo the previous commit

Sometimes when we finish the commit, we realize that we missed a few files, or the commit information was wrong. At this point, you can run the submit command with the –amend option to resubmit. This command will commit the files in the staging area (after git add). If you haven’t changed anything since the last commit, the snapshot will remain the same and all you’ve changed is the commit information.

Git commit –amend Enter Vim to edit the commit.

Try not to undo/modify code that has been pushed to the public repository

2. Undo files in the staging area

Git reset HEAD

3. Undo the file changes

Git checkout —

undo files in your working directory that have been changed and have not been added to the temporary.

Remote warehouse

Git Remote View remote repository server

  • -vThe abbreviation of the Git store that you want to read and write to the remote repository is displayed along with its corresponding URL
  • git remote add <shortname> <url>Add a new remote Git repository and specify a handy shorthand.
  • git remote show <remote>View a remote repository, for examplegit remote show origin
  • git remote renameRenames the shorthand name of the remote repository. For example,git remote rename origin origin2
  • git remote remove origin2orgit remote rm origin2Remove the remote repository, and all remote trace branches and configuration information associated with the remote repository will be deleted.

The git clone command adds the remote repository itself. Every version of every file in the remote Git repository with the default configuration will be pulled down

Keep code in sync (pull and push)

git fetch + git merge / rebase

  1. Get the remote branch commit

The git fetch

command only downloads remote commit data to a remote branch of your local repository. It does not automatically merge or modify your current working directory. Git checkout origin/branch Or manually merge it into a working directory.

Note: Origin /master is not the same branch as master.

  1. Manually merge

The git merge origin/master or git rebase origin/master command is used to merge the obtained remote repository with the local working directory.

git pull

Merge the remote branch into the current branch after pulling the remote COMMIT data.

  • git pull origin master = git fetch origin master + git merge origin/master

  • Git pull –rebase origin master = git fetch origin master + Git rebase origin/master

git push

Git push

Pushes the commit data from the local repository to the remote branch.

Branch merge

Merge hotFix branch into master branch

Git checkout master --> git pull origin hotfix --> git add. --> git commit -m 'merge' --> Git push origin master

GIT three trees

The local GIT repository contains three trees,

  1. HEAD is a pointer to the current branch reference, always pointing to the last commit of the branch;
  2. Index cache, used to temporarily store local changes;
  3. Working Directory Indicates the Working Directory that points to the actual local file.
  • Working Directory git add Index git commit HEAD
  • HEAD git checkout files Working Directory

Reset/rollback

git reset

7.7 Git Tools – reset disclosure

Git reset is actually the operation of the three Git trees, first move the HEAD forward.

  • --softMove the HEAD. I just changed itHEAD.IndexWorking DirectoryIt’s not changed. It’s essentially undonecommitThe status of the working directory is cached.
  • --mixedUpdate Index Index. Index is updated after HEAD is moved, which is undocommitThe working directory is not cached.
  • --hardUpdate the working directory. After Index is updated, the working directory is revoked to the same state as IndexcommitIf the local changes are revoked, the last committed changes cannot be returned.Danger ❗️ danger ❗️ Danger ❗️

git revert

Git Revert commitId rolls back and forth between previous commits with a new commit.

Scenes from Reset and Revert

  • Use if the code to roll back the branch is needed latergit revert
  • If I’m using the wrong branch, the code on that branch is not useful and I don’t want anyone to see it, okgit reset

git config

Git config --global user.name "John Smith" git config --global user.email [email protected] Config --global alias.st status git config --global alias.ci commit # git push master => git push origin HEAD:refs/for/master git config --global alias.gpush '! f() { : push ; r=$1; [[ -z $r ]] && r=origin; b=$2; t=$(git rev-parse --abbrev-ref HEAD); t=${t#refs/heads/}; [[ -z $b ]]; cmd="git push $r HEAD:refs/for/$b"; echo $cmd; echo; $cmd; }; f'Copy the code

Git Checkout

  • git checkout <branch>By moving theHEADImplement the checkout branch
  • git checkout <commitId>Checks out a branch based on a commit of the current branch
  • git checkout -b <branch>Creating a new branch
  • git checkout <fileUrl>Undo the changes to the file

The difference between git merge and Git Rebase

See the git merge and Git rebase differences

1 - > 2 - > 3 - > 4 - > master └ ─ - > 5 - > 6 - > hotfixCopy the code

1. Submit record differences

The merge # 1 - > 2 - > 3 - > 4-7 - > > master/hotfix └ ─ - > 5-6 - > ─ ┘Copy the code
# rebase
1 -> 2 -> 3 -> 4 -> 5' -> 6' -> master/hotfix
Copy the code
  • The merge merges the public branches with your current COMMIT to form a new COMMIT.
  • Rebase will place the commit of your current branch at the end of the public branch, making the commit a straight line.
  • Merge can represent the timeline; Rebase time is confusing;

2. Resolve conflict differences

The current local branch is the master branch

  • Merge: The operation is in progressgit mergeAfter the conflict is displayed, modify the conflict and execute again. The execution process is as follows:
    Git add. --> git commit -m 'xx' --> git pull origin hotfix --> conflict resolution --> git add. --> git commit -m 'xx' --> git push origin masterCopy the code
  • Rebase: Each commit of the Hotfix branch needs to be compared with the master’s new code, so the conflict needs to be resolved in a loop. The execution process is as follows:
    Git add. -->git commit -m 'xx' -->git pull origin hotfix --rebase --> (' Repeat 'resolve conflict -->git add. --> Git rebase --continue) --> Git Push Origin master -- fCopy the code

Use of Git Rebase

Git rebase -i [startPoint] [endpoint] can merge multiple commits, where -i stands for –interactive.

[startpoint] [endpoint] specifies an edit interval. If you do not specify [endpoint], the endpoint of the interval is the default commit of the current branch HEAD.

Git rebase -i HEAD~2 HEAD~2 indicates that the last two commits are merged.

Git: git: git: git: git: git: git Pick: keep the commit (abbreviated :p) reword: keep the commit, but I need to modify the comments on the commit (abbreviated: R) Edit: Keep the commit, but I need to stop to modify the commit (not just the comments) (abbreviated: E) Squash: Fixup: Merge the commit with the previous commit, but I don't want to keep comments on the commit (f) exec: Execute shell command (x) drop: I want to drop the commit (D)Copy the code

Label the git tag

  • git tag -a tagName <-m "added description release notes">Create a label
  • git tag git tag --list/-lLook at the tag
  • git checkout tagNameChecks out the specified tag
  • git tag -d tagNameDeleting a Local Tag
  • git push origin tagNamePush the local tag to the remote

Refer to the article