banner
Koresamuel

Koresamuel

Sometimes ever, sometimes never.
github
twitter
email

Common operations and scenarios of git

Here are some common operations or scenarios encountered in work.

Several concepts should be unified:

  • workspace: your local environment
  • staging area: the changes to a file are placed in this staging area by using the command git add <file>
  • local repository: the changes are placed in the local repository by using the command git commit -m "description of the changes"
  • remote repository: the changes are pushed to the remote repository, such as GitHub or GitLab, by using the command git push

The relationship between them is shown in the following diagram:

Mermaid Loading...

Here are some commonly used aliases that can be configured:

alias.amd=commit --amend --no-edit
alias.am=commit --amend
alias.br=branch --sort=-committerdate
alias.ci=commit
alias.co=checkout
alias.cp=cherry-pick
alias.df=diff
alias.drop=stash drop
alias.pci=!git fetch origin master && git rebase origin/master && git push
alias.la=log --oneline --decorate --graph --all
alias.last=log -1 HEAD --stat
alias.ls=log --graph --pretty=format:'%Cred%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %C(yellow)%D%Creset' --abbrev-commit
alias.lg=log --stat
alias.pl=pull --rebase origin master
alias.pop=stash pop
alias.pr=pull --rebase --recurse-submodules
alias.rc=rebase --continue
alias.ri=rebase -i HEAD~10
alias.root=rev-parse --show-toplevel
alias.save=stash save
alias.sl=stash list
alias.st=status
alias.sup=submodule update --init --recursive
alias.unstage=reset HEAD --

In addition, although the commands mentioned in the article have been tested, please try them out before applying them to the work production environment to avoid irreversible consequences.
All explanations with <> indicate that it is a variable that needs to be replaced according to your needs. When replacing, do not include this symbol in the command.

Quickly switch to the previous branch#

git co -

Delete a local branch#

git branch -d <branch-name>

Delete a remote branch#

git push origin --delete <branch-name>

Delete a remote branch that has been deleted locally but still exists remotely#

git remote prune origin

Rename a local branch#

git branch -m <new-branch-name>

Revert a commit (as a new commit)#

git revert <commit-id>

Go back to a certain commit and delete all subsequent commits#

The difference between reset and revert is that reset or erases all commits after the commit-id.

git reset <commit-id> # Default --mixed parameter

# --mixed: Does not delete the changes in the working directory, undoes the commit, and undoes git add .
# --soft: Does not delete the changes in the working directory, undoes the commit, does not undo git add .
# --hard: Deletes the changes in the working directory, undoes the commit, undoes git add .
git reset --mixed HEAD^ # Go back to the previous version, it will reset HEAD to the previous commit and reset the staging area to match HEAD, but that's it. The working directory will not be changed.

git reset --soft HEAD~3 # Go back three versions, only the commit information is rolled back, and the staging area and working directory remain the same as before the rollback. If you want to commit again, just use the commit command.

git reset --hard <commit-id> # Completely go back to the state of the specified commit-id, and the staging area and working directory will also become the content of the specified commit-id.

Save current changes without committing#

git stash

Save current changes, including untracked files#

git stash -u

List all stashes#

git stash list

Go back to a certain stash state#

git stash apply <stash@{n}> # Modify this n

Go back to the most recent stash state and remove it from the stash list#

git stash pop

Delete all stashes#

git stash clear

Apply modifications to a specific file from a specific stash#

git diff <stash@{n}> -- <file-path> # You can check the diff before applying
git co <stash@{n}> -- <file-path>

List all tracked files#

git ls-files -t

List all untracked files#

git ls-files --others

Delete untracked files#

The clean command can be used to delete newly created files. If no file name is specified, it will clear all untracked files in the working directory. Note:

  1. After clean, the deleted files cannot be recovered.
  2. It will not affect the changes in tracked files, only delete untracked files.
git clean <file-path> -f # It is better to manually delete them

Forcefully delete an untracked directory#

git clean <directory-name> -df # It just deletes the newly created directory and files, but they cannot be recovered

Clear the files recorded in .gitignore#

git clean -X -f

Show the commits that A branch has but B branch does not#

git log A ^B

Display the content of a specific file in any branch#

git show <branch-name>:<file-name>

Find commits based on keywords in commit messages#

git log --all --grep='<finding-message>'
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.