Use of GIT (my crib sheet)

This is really a crib sheet for my-self.

Current file/folder status
  • git status
Get changes from remote branch (fetch and then merge):
  • git pull origin branch
  • git fetch origin branch
  • git merge origin branch
Get changes from all tracked branches
  • git pull
Storing changes 
  • git stash
  • git stash list
  • git stash apply
Push all changes from master to branch
  • git push origin master:branch
Create branch and switch to it.
  • git checkout -b branch
Switch to branch
  • git checkout branch
Merge changes from branch
  • git merge branch
Push my local changes to my remote branch
  • git push origin branch
Pull changes from the remote branch
  • git pull origin branch
Where branch is the name of the branch (locally or remote).


To clone a remote repository locally:
  • git clone git@gitlab.com:company/project.git
You can get the URL to use from the web interface.

After moving files use the following:
  •  git add -A .
Obtaining all branches:
  • git fetch --all
  • git fetch --prune
This will not update the local branches.
  • git pull -all
will update all local branches that are tracking remote branches

See all branches that are known about:
  • git branch --all
References:

Ignore changes to a file:

  • git update-index --assume-unchanged path_to_file
This is commonly used for application configuration files which have a set of defaults you don't want to checkin accidentally.

To undo use:
  • git update-index --no-assume-unchanged
To find all files that you have marked as not changed:
  • git ls-files -v | grep -E "^[a-z]"

Submodules

First time:
  • git submodule update --init --recursive
Subsequently
  • git submodule update --recursive --remote

Public/Private key generation:

  • ssh-keygen

Multiple SSH keys

Create a new SSH key pair:
  • ssh-keygen -t rsa -C "your.email@example.com" -b 4096
When prompted specify a non-default directory such as other_id_rsa. You will need this to be memorable as you will need to set the SSH key to use in each terminal as shown below.

Add the public key to gitlab/github.

For the current terminal session in bit-bash on Windows 10:
  • pushd ~/.ssh/
  • eval $(ssh-agent -s)
  • ssh-add ~/.ssh/other_id_rsa

Setting up SourceTree with gitlab.

Setup your public/private key locally using git (git-bash on Windows) and gitlab.
Clone the repository using the command line.
Use SourceTree with the local repository and use the command line to push/pull with remote repository.

Workflows

The following post 'A successful git branching model' is a good read.

The following branching scenario's probably have issues however they are the two I currently have to work with.

Staging Branch Workflow

Work in a local branch "mybranch".
Commit locally : $ git commit
Push changes to my remote branch: $ git push origin mybranch
Update your local branch from the remote staging branch: git pull origin staging
Fix the problems : git mergetool
Commit the changes: git commit
Push the changes to staging and my remote branch: $ git push origin mybranch:staging

The master branch is then updated from the staging branch. This means that access to master is stable and controlled.

Alternative Staging Branch Workflow (current approach)

1) $ git fetch --prune
2) Get your branch code working as you want, with changes committed to "mybranch"
3) Get the changes from staging: $ git pull origin staging
3a) Fix any issues and push back to "mybranch". This should make the merge with staging simpler.
4) Checkout staging: $ git checkout -B staging origin/staging
5) Merge your branch into staging:  git merge mybranch
6) Fix any conflicts: git mergetool
7) Commit changes: git commit
8) Push staging: git push origin staging
9) Switch back to mybranch: $ git checkout mybranch
10) Pull any changes back from staging: $ git pull origin staging

I can't help but think that this is more complex than it needs to be.

Diff/Merge Tools:

List of Merge/Diff tools on Windows.

The tool I am using is P4Merge.

Check that list of tools supports your tool and configure.
  • git mergetool --tool-help
  • git config --global merge.tool p4merge
  • git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'
If your tool is not supported life is a lot more difficult:
  • git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'
The tool will need to be in your path.

Note: occasionally you lose the combined view. The only way of restoring this is to delete the preferences file under AppData.

Visual Studio Advice

Learn the command line. You can not do everything in Visual Studio and some development shops don't allow you to install extensions into Visual Studio.

Advice On File Changes

If you are using Visual Studio merging changes to Visual Studio projects is a nightmare. Try and avoid large changes to such files or consider using CMake to define what you are building and generate the project files.

Shallow Clones

Only clone the last 35 commits:

  • git clone --depth 35 https://gitlab.com/XXXX/YYYY.git

Clone the last 35 commits and any branches:

  • git clone --depth 35 --no-single-branch https://gitlab.com/XXXX/YYYY.git

Given the way the previous command works you may wish to delete any remote branches you are not interested in.

  • git branch -r -d remote/branch

Also checkout any branches you are interested in locally to make sure they contain what you expect them too.

Comments