Git Notes #4 [commands list]

Since that time, when my eyes saw Git, I wrote lot of code. I studied Git on practice, on live examples. I wrote 3 articles here on my blog. I shared knowledge with my team mates. So goals of this particular article are:

  1. collect all my experience.
  2. classify commands for myself and other readers by similarity in use and frequency of usage.
  3. minimized explanation, due to the fact, that on web site https://git-scm.com/ or simply Googling, you can find more details.
  4. this will be placeholder for future updates (I will add some new item from my work life).

 

Image taken from: https://www.git-tower.com/learn/git/ebook/en/command-line/remote-repositories/introduction
Image taken from git-tower.com

Notes

  • I get used to work with Git from console, so either it’s on Windows or Linux or Mac OS, I always have gitk pre-installed program, which is very minimal and enough tool for me as developer to visualize git history.
  • Below commands ARE NOT in order to execute (not always), don’t be mislead. It’s simply grouped list of commands. I use these commands in different project life-cycle moments.
  • In below text, I use shortcut SHA or SHA ID means ID of commit in SHA-1 encoded string.
  • There is dedicated article for “git edge case issues” I faced with.

git-config

git config --list || -l
git config --list --show-origin # shows all locations of .gitconfig files
git config --list --local || --system || --global

git config --local user.name # check user name in local repository
git config --system user.name # check user name in system git config .
git config --global user.name # check globally configured git user name.
git config user.name # check user name in all config files.

git config user.name "Andrii Lundiak" # set globally
git config user.email "landikeATgmail.com"
git config push.default simple # Defines the action git push should take if no refspec is explicitly given

git config --global credential.helper store # if lazy to type password always or setup SSH keys
git config --global core.autocrlf false
git config --global core.eol lf
git config --global core.safecrlf true
git config --global core.ignorecase false

git config --unset core.eol # remove config from config file

git config -e # vim based editor of configs in CLI

 

Next list of commands reflect my idea of this post and it’s very close to such visualization:

https://github.com/nerdgirl/git-cheatsheet-visual/

“Create” area

git init # when u need to create your own git repository from scratch
git clone git@github.com:username/repo.git
git clone git@github.com:username/repo.git -b my_custom_branch
git clone git@github.com:username/repo.git my_folder_for_repo
git clone --config  core.autocrlf=false git@bitbucket.org:username/repo.git
git clone --bare git@github.com:username/repo.git # note, doesn't work as bare, when folder/PATH provided. In fact need to provide $PATH.git, and then we can use as valid git repo.
git clone ../../my_project_folder/.git # in fact, u can also copy <code>.git</code> folder, and put/paste somewhere else, and u will be able to checkout code from branches. In this way, by default all files will be deleted/not-available.

git remote add origin git@github.com:username2/repo.git
git remote set-url origin git@github.com:username1/repo.git # change repo URL (because re-add cause error)
git remote rm origin

git remote show origin
git remote update -p # || --prune

# Single and Multiple Remote repositories
git clone git@github.com:MyUserName/repo.git
cd repo # origin = git@github.com:MyUserName/repo.git
git remote add upstream git@github.com:username/repo.git
# meaning, that u can't push to upstream, but u can push to ur fork (origin) and later create PR from origin-aka-fork into upstream.

“Browse” area

git show # shows HEAD

git show HEAD~ # shows parent of HEAD
git show HEAD^
git show HEAD~1

git show HEAD~~ # shows parent parent of HEAD
git show HEAD^^
git show HEAD~2

git diff HEAD~ HEAD
git diff master my-branch

Note: WordPress can’t convert “```” markdown when content inside contains tilde “~“.

git status

git blame a.txt

git ls-remote origin

From git@bitbucket.org:alundiak/ggg.git
f64a9b1d63c884084c08cf20a10a9f996eb91bbb    HEAD
f64a9b1d63c884084c08cf20a10a9f996eb91bbb    refs/heads/master
f57d7c21fa716934e49a57229974117a062766fe    refs/heads/teat-master
f64a9b1d63c884084c08cf20a10a9f996eb91bbb    refs/tags/v1.0.1

git log --format=oneline # nice view
git log branch-1..branch-2
git log HEAD^^..HEAD
git diff tag1..tag2

git reflog show # almost the same, but with short SHA

"git reset --hard" uses the same SHA from/for HEAD, but "git reflog" shows all execution of reset command:
a33ceb3 HEAD@{0}: reset: moving to HEAD
a33ceb3 HEAD@{1}: reset: moving to HEAD
a33ceb3 HEAD@{2}: reset: moving to HEAD
a33ceb3 HEAD@{3}: commit: Added change D
f64a9b1 HEAD@{4}: commit: Added last file for testing bare repo
a14b621 HEAD@{5}: commit: Added abc file to fetch it later in bare repo

and "git log" shows always one record:
a33ceb3c97dd8e197c9df46b9d2a9a8b493b4968 Added change D
f64a9b1d63c884084c08cf20a10a9f996eb91bbb Added last file for testing bare repo
a14b621aeb386f96008cd3cbec24d12e76a8c036 Added abc file to fetch it later in bare repo

git ls-tree HEAD
git branch
git branch -D my-branch
git branch -r
git branch -a
git branch -m old-name-branch new-name-branch
git branch --merged master #very useful for branches cleanup

Working with git stash

git stash # it stores changes to all modified files in specific
... # other code changes, etc.
git stash apply

git stash save "text message here"
git stash list /* shows whats in stash */
git stash show -p stash@{0} /* Show the diff in the stash */
git stash pop stash@{0} /*  restores the stash deletes the stash */
git stash apply stash@{0} /*  restores the stash and keeps the stash */
git stash clear /*  removes all stash */
git stash drop stash@{0}

 

“Revert” area

http://plain-text.co/record.html

 

git checkout some-local-branch
git checkout SHA
git checkout . # kinda rest of code changes

git checkout -b my-new-branch
git checkout -b my-new-branch origin/another-remote-branch # this way, git will track remote branch and keep informing you when in regards to updates for local branch

#see edge case bout detached state
git checkout origin/mybranch
git checkout mybranch/2.3
git checkout --track origin/mybranch
git checkout --track mybranch/2.3
#see edge case bout detached state

git checkout --orphan
#Create a new orphan branch, named , started from and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

git revert SHA

git reset # Reverts and unstage changes
git reset --soft HEAD^6 # Does not touch the index file or the working tree at all
git reset --soft tags/v1.0.1
git reset --hard # Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

“Update” area

https://duzun.me/tips/git
git fetch
git fetch --dry-run
git fetch origin
git fetch upstream
git --git-dir=my_working_copy/.git fetch

git pull
git pull origin master
git pull upstream master

git pull origin branch-name -r # pulling changes by "rebase" strategy

“Commit” area

git add . # all, including untracked new added files
git add -u # only modified (already tracked files)
git add -A [--all]  # all, including untracked and new but entire working tree (like "../../" also)
git commit -C SHA # || --reuse-message SHA

git rm --cached PATH (file, dir) # unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone. Not: "git reset HEAD" will reset to the last INDEX state, even if "rm" was executed before.

git commit -m "msg"

git commit -a[--all] -m "msg" # (new files not affected !!!)
git commit --amend
git commit --allow-empty

git commit --squash=SHA-ID
git commit --fixup=SHA-ID

“Merge” area

git merge branch-src branch-dst
git merge --continue
git merge -s ours obsolete
git merge --no-commit maint

git rebase master
git rebase --interactive master
git rebase --interactive --autosquash # covers both squash! and fixup! commits. #see edge case about squashing commits
git rebase --continue

git cherry-pick
git cherry-pick --continue

 

“Publish” area

git push --delete origin mybranch
git push origin :mybranch
git push --force origin mybranch
git push origin +mybranch

git push -u origin master # || --set-upstream

git format-patch -n HEAD^ --stdout > my-file.patch # important to have --stdout, otherwise patch will be created empty
git apply my-file.patch

 

Working with tags

git tag --list | -l

git tag v1.0.0
git push --tags

git fetch --all --tags --prune

git checkout tags/v1.0.0 -b my_branch_based_on_tag_v1.0.0

git tag -d v1.0.0
git push origin :v1.0.0
or git push origin :refs/tags/v1.0.0

Git Clean / Archiving / Compressing

git clean -dff #tested in 2017-mar-10 (there were files added as new but on top level folders, and git clean -df removed but it removes ONLY in current level of hierarchy)
git clean -x
git clean -X

Nicely written about git clean:
https://medium.freecodecamp.com/7-git-hacks-you-just-can-t-ignore-41aea137727a

git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (44/44), done.

git gc # --prune
Counting objects: 158615

Counting objects: 176052, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (56253/56253), done.
Writing objects: 100% (176052/176052), done.
Total 176052 (delta 86621), reused 168547 (delta 81578)
Removing duplicate objects: 100% (256/256), done.
Checking connectivity: 177028, done.

TODO

I’m not using these things so frequent, but it’s interesting, and useful stuff to get experience:

 

Resources

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s