Skip to content

Instantly share code, notes, and snippets.

@niranjan-nagaraju
Last active September 2, 2024 21:27
Show Gist options
  • Save niranjan-nagaraju/01a27513279e8fc7c2be0086b94a4ad2 to your computer and use it in GitHub Desktop.
Save niranjan-nagaraju/01a27513279e8fc7c2be0086b94a4ad2 to your computer and use it in GitHub Desktop.

Git cheatsheet

git config (~/.gitconfig)

$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
$ git config --global core.editor emacs
$ git config --global merge.tool vimdiff

~/.gitignore [.a, .o and temporary files from emacs]

$ cat .gitignore
*.[oa]
*~

.gitignore grammar

# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt

Git aliases

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.last 'log -1 HEAD'
$ git config --global alias.visual '!gitk'

List global config

+ git config --list

Basics

Basic commands

$ git init
$ git add *.c
$ git add README
$ git commit -m 'initial project version'
$ git push origin master

git add

+ Edit <file>
+ git add <file>
+ Edit <file> again
  + git status (now shows <file> as both staged and unstaged)
  + git commit <---- Will commit last edit

git diff

Diff on staged area
git diff --cached // git diff --staged
--- {Staged but not committed} Vs {previously committed}

git rm

remove accidentally staged file
$ git rm --cached readme.txt
remove using wildcards
$ git rm log/\*.log

Commit

Skip ‘git add’
git commit -am <msg> 
Show changes from a commit
git show <commit-hash>
Ammend after commit
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
Ammend commit message before push
$ git commit --amend -m "New commit message"
Undo last commit
git reset --hard HEAD~1
Commit only current directory
git commit -m "<blah>" -- .
Commit specific directory
git commit -m "<blah>" -- <dir>

Undo

Unstage (Undo staged file)

This just unstages the <file>, => won’t erase your changes. -> follow by checkout if changes need to be discarded.
$ git reset HEAD <file>

Discard changes (Undo unstaged changes)

$ git checkout -- <file>

Revert to a specific commit

$ git checkout <SHA1 hash> file/to/restore

Revert to last committed version

$ git checkout HEAD file/to/restore

last but one commit

$ git checkout HEAD~1 file/to/restore

Rewrite history (undo some commit in the past)

+ rebase
  + http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html
  + git rebase -i dd61ab32^

Reset last commit (committed but not pushed)

+ git reset --hard HEAD~1

Remotes

List remotes

+ git remote
+ git remote -v

Add remote

$ git remote add pb git://github.com/paulboone/ticgit.git

$ git remote -v
origin  git://github.com/schacon/ticgit.git
pb  git://github.com/paulboone/ticgit.git

$ git fetch pb

fetch vs pull

pull == fetch + merge

push

$ git push [remote] [branch]
$ git push origin master

Inspecting a remote

$ git remote show origin
* remote origin
URL: git://github.com/schacon/ticgit.git
Remote branch merged with 'git pull' while on branch master
  master
Tracked remote branches
  master
  ticgit

Rename/rm remotes

$ git remote rm <remote>
$ git remote rename <remote> <renamed-remote>

Tags

Create a tag

$ git tag -a v1.4 -m 'my version 1.4' 

pushing tags

$ git push origin v1.4
$ git push origin --tags

Branching

Branches

+ Git Objects
  [commit object] -> [tree object] -> \ -> [blob]  
                                      \ -> [blob]
                                      \ -> [blob]
+ branch == pointer to commit object.
+ Default branch -> master
+ HEAD -> pointer to current branch.

Create a branch

$ git branch testing

Switch to a branch

$ git checkout testing

Create and switch to a branch

$ git checkout -b testing

See last commit on each branch

git branch -v

Push local branch to remote

git push -u origin <branch>

merging branches

$ git checkout master
$ git merge testing

Deleting a branch

$ git branch -d testing

3-way merge

[c0] <- [c1] <- [c2] <- [c4 | master]
                 \
                   <-    [c3] <- [c5 | branch2]

===>

[c0] <- [c1] <- [c2] <- [c4] <-      <-     <-   [c6 | master]
                 \                                   /
                   <-    [c3] <- [c5 | branch2]   <- 

git mergetool

Launches vimdiff or a configured merge tool

Show current branches

$ git branch
  iss53
* master
  testing

show merged and non-merged branches

$ git branch --merged
$ git branch --no-merged

Deleting an ‘unmerged’ branch will fail

Force delete
$ git branch -D unmerged

rebase

[c0] <- [c1] <- [c2] <- [c4 | master]
                 \
                   <-    [c3] <- [c5 | branch2]

===>

[c0] <- [c1] <- [c2] <- [c4] <-  [c3'] <- [c5' | master | branch2] 
rebase (replay) from server and client onto branches master branch
	$ git rebase --onto master server client

Delete a remote branch

$ git push origin :serverfix
To [email protected]:schacon/simplegit.git
- [deleted]         serverfix

$ git push origin --delete <branchName>

Navigate between commits

  • Switch to a previous commit (Move HEAD to some previous commit)
+ git checkout <commit-id>
  • Post checkout to a previous commit, Switch back to latest commit
+ git checkout <branchname>

Links and references

http://git-scm.com/documentation
https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial

feature commit workflow

create <new branch> at repo/master or repo/dev 
   // OR
  git checkout -b feature_branch
  git push -u origin feature_branch

git fetch origin -- should sync <new branch>

git checkout -b <new branch> origin/<new branch>

add, commit

git branch -vv

git push origin <new branch>:<new branch>

pull request

merge 
(remove remote branch after merging)

git branch --merged 
(remove locally too)
git branch -d <new branch>
@djsubstance
Copy link

thx .. this is great

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