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 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...

Below are commonly configurable aliases:

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 actual work production environment to avoid irreversible consequences.
All explanations with <> indicate that they are variables that need to be replaced according to your needs. When replacing them, do not include the < and > symbols 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 remote branches that have been deleted locally#

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 specific commit and delete all subsequent commits#

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

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

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

git reset --soft HEAD~3 # Rolls back to three versions ago, 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 rolls back to the specified commit-id state, and the staging area and working directory will also become the content of the specified commit-id version.

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 specific 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's better to manually delete them

Forcefully delete an untracked directory#

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

Clear files recorded in .gitignore#

 git clean -X -f

Show 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.