Skip to main content

Git Commands

remote

View:

git remote -v

Set:

git remote set-url origin git@github.com:AlbertVilaCalvo/Wiki.git

Change:

git remote set-url origin git@github.com:AlbertVilaCalvo/Wiki.git

Remove:

git remote remove origin

log

Docs: https://git-scm.com/docs/git-log

Viewing the Commit History: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

View file line history/changes

Show the changes from line 135 to 140:

git log --pretty=short -u -L 135,140:file/path/something.txt

source

View log (commits) for a file or folder

# file
git log package.json
git log -- package.json

# folder
git log path/to/folder
git log -- path/to/folder

https://stackoverflow.com/questions/11950037/view-git-history-for-folder

Search string on git history

git log -S something
git log -p -S something

source

Search by commit message

git log --all --grep='Something'

source

Show author, author date, commit and commit date

git log --pretty=fuller

Output:

Author:     Albert Vila Calvo <albert@gmail.com>
AuthorDate: Sun Jun 2 20:41:28 2024 +0200
Commit: Albert Vila Calvo <albert@gmail.com>
CommitDate: Sun Jun 2 20:42:45 2024 +0200

For the difference see:

show

https://git-scm.com/docs/git-show

git show
git show HEAD~1
git show HEAD@{5} # Use 'git reflog' to see the reflog
git show @~1 # @ is the most recent commit
git show -2 # Last 2 commits, both
git show --summary # Do not show file changes, only header info
git show 2.4.0 # A tag
git show branch:file # Eg 'git show main:README.md'
git show main@{yesterday}

Referencing previous commits

https://stackoverflow.com/questions/16062358/referring-to-the-previous-next-commit-in-git

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

add

https://git-scm.com/docs/git-add

Stage all (new, modified and deleted): git add -A or git add --all

Stage new and modified files, but not deleted, in the current directory: git add .

Stage modified and deleted files, but not new files: git add -u

Add portions of file: git add -p see commands here

reset

https://git-scm.com/docs/git-reset

Unstage all staged files:

git reset

Reset parts of a file: git reset -p

stash

https://git-scm.com/docs/git-stash

Stash with message

git stash push -m "Some message"

Stash single file

git stash -- path/filename.txt
git stash -- path/filename1.txt path/filename2.txt
git stash -m "Some message" -- path/filename.txt

source

Stash only staged changes

https://git-scm.com/docs/git-stash#Documentation/git-stash.txt---staged

git stash push --staged
git stash push -S
git stash save --staged
git stash save -S