Git Cookbook

Checkout Git In Hindsight

Sample workflow: Start a new branch ‘MY_WORKING_BRANCH-12345’ for your work

$git pull
$git checkout master # point to master
$git checkout -b MY_WORKING_BRANCH-12345 # create branch from master
# Now do some work, edit files etc, when you're ready to commit  ..
$ git add .
$ git commit -m 'descriptive message'

Sample workflow: Merge your ‘MY_WORKING_BRANCH-12345’ into master

$ git checkout master # check out the target and then merge the branch into master
$ git merge --squash MY_WORKING_BRANCH-12345 # destroys commit history on the branch
$ git commit -m 'MY_WORKING_BRANCH-12345\n\n descriptive text'

Sample workflow: Reset your private branch

# to throw away, do
$ git reset --hard
# Assuming that there is a remote git server from which you've cloned your workspace,
# you can resync your local branch with the data on the remote git server by doing the 
# following:
$ git reset --hard origin/${remote-branch-name}
# e.g.
$ git reset --hard origin/master

Sample workflow: Stash your in-progress work off to the side so that you can work on something else for a bit without having to check in your code

# to see a list of existing stashes
$ git stash list
# to save the current changes to a stash
$ git stash save description
# to re-apply the last saved stash
$ git stash pop

Create a patch file

$ git format-patch

Create a shared repository on a server

$ mkdir {repository-dir}
$ chgrp GitUsers {repository-dir}
$ cd {repository-dir}
$ git --bare init --shared
# add new users to the GitUsers group

Per user, git client setup

a. install git on client machine
b. initialize the global defaults
$ git config --global user.email {user-email-address}
$ git config --global user.name {user-name}
$ git config --global push.default simple

Setup a local repository on a dev box

$ git clone ssh://{user}@{hostname}/{path/to/git/repro}
# e.g. git clone ssh://joedev@sulu/volume1/Git/Samples
# NOTE: may want to do below to share a global git ignore file between repositories:
$ git config --global core.excludesfile ~/.gitignore_global
# Also, edit .git/info/exclude in the git root to specify files to ignore
# NOTE: to get only one branch
$ git clone -b master --single-branch git@git.mycompany.com:repositories/myrepo.git

How to pull latest changes from shared repository to local repository
NOTE: use get pull origin to do both a fetch+merge

$ git pull
# - or -
$ git fetch origin # caches the shared repro files locally in the .git folder of your local repro
$ git log master..origin # shows changes in shared repro not in your local repro
$ git merge origin # merges changes from shared repro into your local files
# How to see changes for a file or set of files
$ git log --oneline --graph --decorate [${file-or-directory}]

How to purge untracked (i.e. un-added) files.
purge:

$ git clean -i #ask for each file.

How to view uncommitted changes for all the files

$ git status

How to view uncommited changes for existing files

$ git diff
# if added,
$ git diff --cached #difference between index (staging area) and repo

How to create a diff file

$ git diff HEAD >~/mychanges.out

How to apply a diff file to the current branch

$ git apply ~/mychanges.out

How to add a new file or folder

$ git add {path}

How to set the working-tree to a particular revision instead of the tip.

$ git checkout {change-id}
# e.g. git checkout 0a633bf5

How to create a branch

$ git checkout -b {branch-name} [{change-id}]
$ git push -u origin {branch-name} # to push a branch to the origin repro

How to reset the working-tree to the tip in the master branch

$ git checkout master

How to commit to local repository

$ git commit -a

How to commit to main repository

$ git commit -a
$ git push

How to see the current hash

$ git show

How to see the change on origin

$ git show origin/master

HOW TO REVERT
see: https://github.com/blog/2019-how-to-undo-almost-anything-with-git
How to revert local uncommited changes

$ git reset --hard
$ git checkout --
To remove all new files that haven't been added:
$ git clean -fd

How to revert local commited changes

$ git reset --hard origin/master #or alternately git reset --hard {hash}
$ git clean -fd

How to revert a push to origin. Say you just did a git push.

$ git revert {CommitHashValue}

How to commit to GitHub
The maintainer of that Github-hosted project will probably prefer
your changes to be on feature branches. They’ll ask you to work like this:

$ git checkout master [to make sure each new feature starts from the baseline]
$ git checkout -b newfeature
Make some changes
$ git add [not to be confused with svn add]
$ git commit
$ git push

Now login to Github, switch to your newfeature branch, and issue a
“pull request” so that the maintainer can merge it.

List local branches

$ git branch

List remote branches

$ git branch -r

To see all git config properties

$ git config --list
user.email={useremail}
user.name={username}
push.default=simple
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=ssh://{username}@sulu/volume1/Git/Beholder
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

How do I find all the branches for a commit hash

$ git branch -r --contains 744016881447e36129526976e1b85cb8295a426c

Questions And Answers

Branch Management
Q: How do I create a branch off the master branch both locally and remotely?
A: First create your branch locally via git checkout -b desiredBranchName. Then create the branch remotely via git push -u origin desiredBranchName. There is some discussion of this topic on Stack Overflow here ( http://stackoverflow.com/questions/1519006/how-do-you-create-a-remote-git-branch )

Q: How do I merge changes from a branch (e.g. desiredBranchToMerge) to master?
A: First, make sure you are working in your master branch (e.g. git checkout master). Then simply issue the command git merge desiredBranchToMerge. e.g.

$ git checkout master
$ git pull origin # get the latest changes
$ git merge --no-commit desiredBranchToMerge # the --no-commit will not commit the changes allowing you to modify if needed

This will merge the branch and commit those changes to the local repo.

To uncheckout a particular file

$ git reset {filename}
$ git checkout -- {filename} # to throw away changes to file

To push the changes to origin,

$ git push origin

To throw away the changes and resync to what is stored on associated origin git repository

$ git reset --hard origin/master

Q: How do I determine who created a specific branch?
A:  Use the following to get a table

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n

Q: How do I delete a branch both locally and remotely?
A: First make sure you are working in say the master branch and then delete your branch locally via git branch -d branchToDelete. Then delete the branch remotely via git push origin :branchToDelete. There is some discussion of this topic on Stack Overflow here.
Identifying Changes

Q: I have created a branch in git to do some body of work. Now what I would like to do is to produce a diff file that identifies all of the changes I have made on that branch so that I can give it to someone without using git. How can I produce that diff file?
A: While on the master branch, issue the following command : git diff HEAD…branch > diffFile. Yes, that is correct three dots between HEAD and the branch name. e.g. git diff HEAD…jsr330spike > ~/jsr330spikechanges.txt
Hmmm, I usually just do git diff ..HEAD and it works fine. e.g. git diff master..HEAD – David Van Couvering. Thanks David, in my testing your approach works fine as well, without needing the step to first checkout master. Your approach is more efficient, unless of course you need to be doing work on master for some other reason.

Q: How do I show the files changed by a commit?
A:

git show --name-only

File Recovery
Q: I have deleted a file and committed that change to git, maybe even going to so far as to push that change to our central repository. Now I realize I still need that file, how do I get it back?
A: First, run the git command : git log –diff-filter=D –summary. This will give you a listing of all of the commits where a file has been deleted. Find the hash that identifies the commit where that file was deleted in that output. Lets say that hash is 37e3ce152783913278c427eb9a294cbca630632, and the relative path to that filename is libraries/someservice/somefile. Then issue the following command to recover that deleted file : git checkout 37e3ce152783913278c427eb9a294cbca630632~1 libraries/someservice/somefile. There may alternative solutions to this. An article on Stack Overflow that describes this and other solutions can be found here.

Q: How do I get only part of the repo
A: Assuming that you only want the most recent change for each file in the git repo, you can do something like:

$ git clone --depth=1

Q: How do I turn a depth=1 repo to normal
A: Right, unless you reset your local repo, it will not have the full history of the remote repo. To get it
to fetch the full history from the remote git repo:

$ git fetch --unshallow

Q: How do I change the remote.origin.url for a GIT repo
A: So you have a couple of ways to do this. 1 is that you can change the ‘origin’ repo to be a different url

$ git remote set-url origin git@gitlab.mycompany.com:my-repo/my-repo.git

On the other hand, if you want to also keep your original repo as the origin, you can add another url e.g.

$ git remote add alternate-remote git@gitlab.mycompany.com:my-repo/my-repo.git
# Now to push to this 'alternate-remote' instead of the origin, you do
$ git push -u alternate-remote my-branch 

Q: How do I see the remote.origin.url for a GIT repo
A: you can do 2 ways:

$ git remote -v
$ git config --get remote.origin.url