Skip to content

Instantly share code, notes, and snippets.

@MirKml
Last active January 9, 2022 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MirKml/4d5147e6f21ecc79329ba0816b245044 to your computer and use it in GitHub Desktop.
Save MirKml/4d5147e6f21ecc79329ba0816b245044 to your computer and use it in GitHub Desktop.

Workflow

  1. New bug of feature issue in issue tracker arrives.
  2. Then create branch from master for particular bug or feature.
$ git pull
$ git checkout master
$ git checkout -b feature-or-bug-<issueNumber>

... code ...

$ git status
$ git diff

$ git commit -am "new commit"
$ git push -u origin feature-or-bug-<issueNumber>
or if we are in current branch which we want to push to remote
$ git push -u origin HEAD
  1. Makes the pull request on bitbucket into staging according branch.
  2. Wait for Team City info, check on testing.
  3. Code for finishing work on testing, communicate with author, updates branch. Web is on http://test.myweb.page.
  4. Makes the pull request on bitbucket into master according branch.
  5. Wait for merge, branch is closed.
  6. Delete branch $ git branch -d feature-or-bug-<issueNumber>

Remove merged branches into master

Updates list of remote branches

$ git remote update origin --prune

List of merged branches into master

$ git branch --merged master

Then you can delete these branches with $ git branch -d <brach name>

More tips for clean up outdated branches in this article.

Remove any local branch if branch is merged

$ git branch -d <branchName>

if branch isn't merged

$ git branch -D <branchName>

remove any remote branch

$ git push <remote_name> --delete <branch_name> e.g. $ git push origin --delete <branchName>

Update branch according master

Two possibilities: rebase - it's better for later histrory reading

$ git checkout <branch-name>
$ git fetch
$ git rebase origin/master

merge: sometimes it's simpler, resolving conficts is easier, I don't know why, but that is

$ git checkout <branch-name>
$ git fetch
$ git merge origin/master

Update repository with -p and Git automatically prunes all stale references

$ git fetch -p

Submodules

$ git submodule add RepoUrl [<path>]
$ git submodule init
$ git submodule update

When the branch in repository with submodules are checked, it's necessary run git submodule update. Submodule reference commits are shared between branches, so git submodule update can be important.

Gets submodule hash number for particular branch

$ git checkout <branchName>
$ git submodule status
 c0ee71325b0b02ae440e210ae8685447821c8b1f app/src/share-dir (heads/master)

or without checking new branches, it usefull when merging some branches together

$ git ls-tree [-r <branchName>] @ <submodule-path>
$ git ls-tree -r master @ app/src/share-dir
160000 commit c0ee71325b0b02ae440e210ae8685447821c8b1f app/src/share-dir

Ignoring files

  • Use common .gitignore in repository
  • use global ~/.gitignore and set it via git config --global core.excludesfile ~/.gitignore
  • use repository locals git ignore in .git/info/exclude

Tells git to ignore file temporary.

$ git update-index --assume-unchanged <file>

and undo this

$ git update-index --no-assume-unchanged <file>

list of files which are temporary out of index

$ git ls-files -v | grep '^h'

more on SO

Speed tips

On large repositories, git can be very slow, specially on windows msys2 environment. Tips for speed it up. Speed up git status

$ git config --global core.preloadindex true # is true on git 2.1+
$ git config --global core.fscache true # is true on git 2.8+
$ git config --global gc.auto 256 # can makes some troubles

For windows with msys2

  • tip for speed up TAB path complition

But git in msys is very slow comparison with git for window aka git bash

gitk, git gui (tk based tool) on msys2

$ pacman -S mingw-w64-x86_64-tk

Then add msys64/mingw64/bin into to $PATH e.g. in ~/.bashrc PATH="${PATH}:/mingw64/bin" Gitk should work now, but you might get this problem with git gui icon loading. To fix, add env var GIT_GUI_LIB_DIR -> /usr/share/git-gui/lib. But git gui mostly doesn't work. Try to setup gitextensions.

Git Bash from msys

Git bash / Git for windows is best command line git for windows. It's optimized for speed, it's more quicker then git in msys git. But this bash is limite, there is no package management like Pacman in msys2. Best option is call git-bash directly as new shell from msys2. Git bash preserves home directory, so it's easy to use all .dot files setting (vim, git, ...) same from git bash and msys2.

After installation git bash it's recommended adjust /etc/profile.d/git-prompt.sh for removing support for tracking branches etc. from prompt. Pretty heavy prompt with current branch(es) is mostly slow.

Updating git bash without downloading new msi installer, just call git update-git-for-windows in git bash, nice feature.

Tags

Download or pull tags

git pull --tags

Create tag

git tag -a v1.0 -m 'tagging Version 1.0'

Delete tag

git tag -d v1.0

Move code - checkout

git checkout tags/v1.0

Create branch according tag

git checkout tags/v1.0 -b branchOnTag

Push tags

git push --tags

Executable permission

Git can store executable flag for file even on windows. If is set, then scripts works fine on Linux, Unix, without calling like bash script.sh, simple script.sh works.

Determine file permissions

git ls-files --stage scripts/*

The command will show you the current file permissions like 100644. Then change permission

git update-index --chmod=+x scripts/*.sh

You can see result on git status. Finally commit, push.

Tips

  • compact log of last 10 commits as diffs git log -n10 -p
  • git has some clever default algorithm for showing logs with default mode. Sometimes it doens't show some important changes in merge commits, which change some files forth and back. Sometimes is't usefull to show more about merge commits with command git log -p -m --first-parent <filePath>. It's more slower then usual git log.
  • difference between my current branch and master/other branch: commits git log master..., diff git log -p master..., only files git log --name-only --oneline master... Diff with git diff master... has sometimes different output then diff with git log.
  • clear all uncommited files and files, which are in git ignores - git clean -xfd. It's sometimes usefull, when some tools cannot rebuild your project after switching branches, when old binaries/objs remainded there from previous branch compilation.
  • squash all commits in branch into one commit
 $ git checkout feature_branch
 $ git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD)) 
 # or git reset --soft master when no master merges are provided before
 # Add files for the commit.
 $ git add ...
 $ git commit -m "fix: new commit message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment