Skip to content

Instantly share code, notes, and snippets.

@frangeris
Last active October 3, 2024 20:12
Show Gist options
  • Save frangeris/9607610 to your computer and use it in GitHub Desktop.
Save frangeris/9607610 to your computer and use it in GitHub Desktop.
Common git commands

Common git commands

This is a list of useful commands for git for fast search and ops developers, enjoy and comments for suggest all welcomes :D


Public repo .tar for download versions: https://www.kernel.org/pub/software/scm/git/

Delete with git all files listed as deleted:

$ git rm $(git ls-files --deleted)

Delete a branch DOWN in the local repository:

$ git branch [branch name] --delete

Delete a branch UP in the services repository:

$ git push origin --delete [branch name]

Do not track changes on specific file:

$ git update-index --assume-unchanged [file]

Track changes again:

$ git update-index --no-assume-unchanged

List hidden tracked files:

$ git ls-files -v | grep '^[[:lower:]]'

Cleaning up cache when git ignore fail (options):

$ git rm -r --cached . 
$ git add . 
$ git commit -m "Fixed untracked files"

Clone a repo specifying the branch:

$ git clone -b <branch> <myproject>.git

Ignore mode file change, setting up git:

$ git config core.fileMode false

git checkout, doesn't work... possibilities:

$ git config --global core.autocrlf false
$ vim .gitattributes # comment line "* text=auto"

Setting up git color:

$ git config --global color.ui true

Rolling back a commit:

$ git reset --soft 'HEAD^'

Discard last commit:

$ git reset HEAD --hard
$ git reset --hard origin/master

$ git reset --soft HEAD~1

Merging conflicts (accept theirs):

$ git checkout --theirs

Merging conflicts (accept ours):

$ git checkout --ours

Remove a remote:

$ git remote rm <remote>

Resolve problem with CRLF sequence:

$ git diff --ignore-space-at-eol

Add just tracked files to the stage:

$ git add -u .

Revert changes on a specifuc file commited and begin again:

$ git checkout commit-reff~1 -- <file name>

Remove the last commit and set the change to "Changed but not updated":

$ git reset HEAD~1

Export a "clean" project:

$ git archive master | tar -x -C </somewhere/else>
$ git archive master | bzip2 > <source.tar.bz2>
$ git archive --format zip --output <zipfile.zip> master

Note: This is a synonym to svn export. What this does is export and compress a repository without any .git* files.

Removing file from all history: https://help.github.com/articles/remove-sensitive-data/

Setting global ignore

$ git config --global core.excludesfile ~/.gitignore_global

Pretty git log

$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Alias git lg:

$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Turn off the warning CRLF

git config core.safecrlf false

Change domain to clone (bower)

$ git config --global url."https://".insteadOf git://

Publish to github pages

$ git subtree push --prefix <my site folder> origin gh-pages
@SparoHawk
Copy link

Exporting a "clean" project

This is a synonym to svn export. What this does is export and compress a repository without any .git* files.

tar
git archive master | tar -x -C /somewhere/else
bzip2
git archive master | bzip2 >source-tree.tar.bz2
zip
git archive --format zip --output /full/path/to/zipfile.zip master

@jorwan
Copy link

jorwan commented Mar 9, 2015

// Delete a remote branch
git push origin :branchname

@jorwan
Copy link

jorwan commented Jun 29, 2015

// Let use git in a faster way ;)
alias gb='git branch'
alias gl='git log'
alias gd='git diff'
alias gs='git status'
alias gss='git stash'
alias gssa='git stash apply'
alias ga='git add'
alias gc='git commit -m'
alias gm='git merge'
alias gcc='git checkout'
alias gp='git push origin master'

PD. if you would like to keep permanently the aliases, write them in /etc/profile ( terminal must be restarted )
PD.2. I am using mac

@jorwan
Copy link

jorwan commented Aug 31, 2015

// Recover deleted files from git rm
git fsck --lost-found --unreachable

PD. Recovered files are in '.git/lost-found/other'

@jorwan
Copy link

jorwan commented Sep 23, 2016

Delete all merged branches with the active one

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

@jorwan
Copy link

jorwan commented Feb 20, 2019

Edit Commit Message

// X is the number of commits to the last commit you want to be able to edit
git rebase -i HEAD~X

Source: https://goo.gl/Jbtb8H

@jorwan
Copy link

jorwan commented Jun 25, 2019

// Show all branches with their owners
git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n

@frangeris
Copy link
Author

Delete all merged branches with the active one

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

I just came here to copy this one 🙌🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment