Git

 

These are my personal notes that I use as a quick help in my work.
You are welcome to read them.

Contents of current page Top-level home page
 
Index  Java Internet Oracle Notes
Linux Basics Web Basics SQL Notes
Informatica Servlets Apache BkpRstore SQL*Plus
Teradata   LDAP Storage PL/SQL
Windows     Tables OEM
UML   Net8 Portal
SQL Server Python perl Performance OLAP
Vmware Visual Basic PHP/MySQL User Mgmt  
Git        
More technical pages here

Contents

 


 

 

 

 

 

 


Configuration

 

Basic Configuration
git config --global user.name "My Name"
git config --global user.email e@mail.com

Show main configurations, with the related config file:
git config --show-origin --list


Store credentials

git config --global credential.helper store

To reset, remove the [credential] section in the .gitconfig file, and recreate with command above. The next time I enter the username and pw/token, they are stored.
Or, to reset, do:
git config --global --unset credential.helper

Instead of "global" try "local" for just the repository.

Instead of "store" use "cache", which stores for 15 minutes

To change the default password cache timeout, enter the following (default 15 minutes):
git config --global credential.helper 'cache --timeout=3600'
Of course, use a token instead of the password.


 

 


Regular Workflow

Start a new Repository


Development


Merge Remote Branch into Local Branch

When other team members have modified the remote code, you need to bring in those changes

Simplest command:

Sometimes, we want more control:

Merge into Master
git status                         # everything should be committed !
git branch -av                     # verify which branch I am on
git checkout master                # go to master 
git fetch origin                   # do a merge is something was downloaded
git merge origin/master            # bring in changes from server
git diff <working-branch>          # list the differences between master and branch
                                   # branch shows as minus, master as plus, q=quit
git merge <working-branch>         # merge into master
                                   # take note of the conflicts
git status
git diff <working-branch>          # do this again: there should be no differences
git push origin master             # push the merges up to repository
git fetch origin
git branch --merged                # shows branches safe to merge (any others not listed)
git branch -d <working-branch>     # remove the local branch, as it is merged into master
git push origin -d <working-branch># delete remote branch, as it is merged into master
git checkout -b v2                 # create and checkout a new branch, ready for new code
                                   # best practice to remove merged branches and start a new one
git fetch --all --prune            # remove deleted branchesf

 

 

Stash

Stash current modifications and go back to HEAD:
git stash push
Or simply:
git stash
Stash with a comment:
git stash push -m "a comment"
You may have to do "git add filename" before stashing a specific file with "git stash push filename".

Show all stashes:
git stash list
Show a specific stash:
git stash show stash@{0}

Apply modifications from a stash:
git stash apply stash@{2}
If no argument, it applies the most recent. The files go into stage if no conflict. When conflicts are resolved, do git add ...
Apply and remove (git stash apply does not remove):
git stash pop stash@{0}

 

 

Managing Remote
git remove -v List with details
git remote add abbrev git_url (OK if remote already exists)
git remote rename old_abbrev new_abbrev

 

 

Reviewing Differences

git diff # difference with what is committed
git diff main # difference with main
git diff main [space] -- [space] a/file/name # difference with main for a given file
git diff branch1 branch2 [space] -- [space] a/file/name # difference for a given file between two branches

Compare a local file in current branch to what is in the repo. Remember to fetch from the repo first.
git fetch origin # Remember to fetch first
git diff -- file origin/branch_name/path/file # Without path
git diff -- path/file origin/branch_name/path/file # With path

 

 

.gitignore

To ignore a whole directory, add a .gitignore file in the directory with itself and "*", as follows:

.gitignore
*

 

 

 

 


Correcting

History
git log   # ` <small><i>History of commits</i></small>
git log -p  <filename>
git log --oneline    # ` <small><i>Shows one line per commit</i></small>
git log --oneline --graph   # ` <small><i>Shows graph</i></small>
git log --no-merges <curr-br>..origin/master  # ` <small><i>Shows only changes to the origin/master and not to <curr-br></i></small>


Resolve Conflict
  1. git checkout main/master
  2. git fetch
  3. git checkout <working-branch>
  4. git fetch
  5. git merge main/master # Merge from main/master to branch
    This is a "reverse merge"
  6. git status # Look at conflicts and resolve with regular editor
  7. git add .
  8. git commit -m ". . ."
  9. git push origin <working-branch>
  10. In the UI, verify that the resolutions show in the pull request (or stay local)
  11. In the UI, merge the pull request (or stay local)
Destroy what was previously committed

git log filename
This shows the commit points. Select the commit point BEFORE the commit point you want to remove from the repo
Copy the code (remove .git from the copy after copying)

git reset commit-point-taken-from-above
This undoes all commits , but preserves the files

Commit code
git add .
git commit -m "..."

Verify that all code is correct. Compare to the copy made above.

At this point, there are two options:
1) If you did the reset to a local commit that is after the latest remote commit, then you are all set. Just do:
git push origin branch-name

2) If the remote repo has commits that have to be deleted, meaning that the commit point in the reset command above is before the latest remote commit, then create a new branch
git branch -b new-branch-name

Remove old branch
git branch -d old-branch
git push origin -d old-branch

 

Rename Current Branch

git branch -m <new-name>
Important: you have to delete the old branch on the server and push the new branch.
Important: before renaming, be sure that the local branch is later or the same as the remote branch.

 

Common Errors

"error: failed to push some refs"
This worked: added "-u" in "git push -u origin the-branch"

 

Look for an old version of a file:
on web site, under the main code page, click on the clock with the arrow around it.
Chose a commit point in the past. For something a long time ago, click on the calendar.

git reset the-old-commit-point
# here, I still have the new version of my file, but it is un-committed
git restore the-file-name
# now, I have the old version