Monthly Archives: September 2019

GIT Useful Info

Hi folks,

Today i am sharing some useful information on GIT and commands.

Hope this article will helps .

When to fork and when to branch: –

Forking is done if you want to create a new flavor of the original repo.  It is naturally divergent in nature. Forks are not meant to be merged like the case of Linux and its flavors. This is the standard model in open source world.

Branching is done with the idea of converging the code base at some point in future. This is better fit for private repos in enterprise businesses. This lends better to convergent evolution of the code.

To get repo and branch information
git remote show — gives the list of remote repos
git remote show origin — shows the git repository (origin in this case) URL from which the code is cloned
git remote -v — shows all the remote repos with names you have configured locally on your system
git branch –remotes – gives the list of branches in the remote repo or the origin
git branch — gives the list of branches in your local repo

Credential configuration (will be one time if you are using Git Shell) – 2 ways to do it – either store the credentials in file or store it in cache for x time after which it needs to be again given. 2 option is more secure

File based

Local for the current git project

Inside the repository where you want the credentials to be stored – git config credential.helper store

git pull – This will ask for username/password for the first time and store the same in a file (.git-credentials)
git pull – now the username/password will not be asked
Global for the entire sytem

git config –global credential.helper store
Step 2 and 3 will be same as for local
Cache based:
git config –global “your-user-name”
git config –global “your-email-id”
git config –global credential.helper “cache –timeout=36000” – this caches the credentials for 10 hours

git config core.longpaths true – enable this flag if you get this error – Cannot save the current worktree state+Filename too long

In case the certifcate is not picked properly, point it to the actual cert location using below command

git config –system http.sslcainfo “C:\Users\212607238\AppData\Local

Clone a repo
git clone -b security_scan – you will be prompted to enter username and password, Here the brach and repo url is specified

Branch operations
git branch – create a new branch
git checkout – to switch branch under the same git head. Code is automatically updated
git branch -d – delete a branch once prupose is achieved

Managing local changes
git add subfolder/filename.txt – staging new files
git rm subfolder/filename.txt – staging files to be deleted
git stash show – shows the staged local changed files
git stash – save local changes to stash
git stash -u – discard all local changes
git checkout –file.txt – undoes any local changes by getting the latest from remote repo
git reset subfolder/filename.txt – unstaging file

Sync code
git push origin master — push local committed changes to remote github repository. origin is the remote repo name that git gives by default to your repo, master is the branch name both in your local repo fork and remote origin repo. the syntax is git push :. but if only one branch name is specified then git assumes that both the local and remote branches are of the same name.
git pull origin master – for updating local repo

Commit Code
git commit -m “Removing files” – before committing ensure that there are no untracked files. either add the untracked files to be staged or put them in .gitignore

Revert a commit in remote
git pull origin branch-name — always pull before revert, if local does not have the changes that need to be reverted you get fatal: bad object error
git revert -m 1 d8dc9c0332d0c038784902c2f260ea24cd0f6238 — revert the commit from parent branch 1, which usually will be master. m is the option to refer parent branch and 1 tells which parent branch
git push origin master

Revert a local commit
git reset HEAD~

Revert untracked changes locally

git clean -fd – remove untracked files or directories

Revert local changes

1. If you have not yet added the changes to the index or committed them, then you just want to use the checkout command – this will change the state of the working copy to match the repository: (– is called bare double dashes and they indicate that we are resetting a file and not a branch)

git checkout — filepath
git reset — optimax-server/config/oimContext.xml – if you are running the command from optimax folder
2. If you want to reset the complete branch and discard all staged and tracked but unstaged changes then give the following command. This can also be used to revert any local commits

reset –hard devfork/Aut17_DEV

3. If you added it to the index already, use reset:

git reset A
4. If you had committed it, then you use the revert command:

# the -n means, do not commit the revert yet
git revert -n
# now make sure we are just going to commit the revert to A
git reset B
git commit
5. To ignore the untracked files mention those files/folders in .gitignore file

Diffing files (in git native console, it opens the diffs in vi editor where all the vi commands work. –word-diff shows only the changed words instead of complete lines which is the default)

Staged files – git diff –word-diff –cached
Unstaged files – git diff –word-diff

Open a Pull Request

git request-pull [-p] []

Merge a Pull Request

refs/pull/6/head. The refs/pull/8/head contains the commits of pull request #8head.

git fetch origin refs/pull/8/head
git merge FETCH_HEAD
git commit
git push…


git pull refs/pull/8/head – pull=fetch+merge
git commit
git push…

Merge code

Always checkout into the source (branch_name below). Pull the source through git pull. Checkout the first current branch into which we want to merge.
git merge repo/ – merge everything from to current checked out branch/master.
Push the merged code into remote destination branch through git push.
Cherry-pick option is used to merge specific commits made in one branch to another. To cherry-pick commits, have the SHA hash of the commit to merge. Checkout to the branch to merge and issue the command git cherry-pick
Links: Cherry-pick GIT documentation, how to cherry-pick changes with git
Resolve Merge conflicts

after running the merge command do following
git status – along with others this also gives conflicted files in red calling it unmerged paths
open those files in notepad++
search for the beginning of conflict marker – <<<<<<< HEAD
Lines below this marker are the original lines in destination branch
Then find marker – ======= This is a divider
Below the divider marker are the changes as in origin branch from which code is merged. This branch name is mentioned in the merge command
Choose the appropriate line and delete the rest with markers
Add or stage your changes – git add
git status – ensure all the ‘unmerged paths’ section is not shown now as you staged all the changes in previous step
Commit the changes – git commit -m “Conflicts resolved”
git pull – get the latest from remote repo
git push – push local repo changes to remote repo
Repo and Fork related activities

merging code between forks (ideally you would do a pull request, but this is the shortcut if you have permission)

git remote add security
git pull security master – security is the origin repo name and master is the branch in that repo
git push –set-upstream mainrepo – mainrepo is the destination repo name with the branch in that repo

pushing a newly created branch in my fork to main repo
git remote add mainrepo – add the main repo that can be associated with your fork.

git push mainrepo – pushes the newly created  branch in the local repo to main repo

if a new branch was created in main repo and you want to pull it to fork. then workflow is you fetch it to your local machine and push it to your remote fork
git remote add mainrepo
git fetch mainrepo — origin is the main repo, spr_17 is the new branch created in origin, get that locally to your machine
git push upstream — upstream is your fork – push the fetched branch to your remote fork

List commits made by a particular author

git log –author=”Jon”

List number of commits done by all authors

git shortlog

Create patch

git format-patch -x

where -x means how many commits back from the current head and it has to be integer.

For example if i want to generate patch for last 10 commits

git format-patch -10

The above example will generate 10 file patches.

Squash multiple patches into a single file

git format-patch -x –stdout > patch-ddmmyyy.patch

Note: The generated patches work with svn also.

Apply patch:

preview the patch:
git apply –stat a_file.patch
dry run the patch to detect errors
git apply –check a_file.patch
apply the patch
git am –signoff < a_file.patch
Abort applying patch
git am –abort
git diff head
git fetch upstream
git rebase upstream
git log – file history or all the change lists in the branch checked out

git ls-remote

Side Notes:
If you forked a repository, you simply clone the fork. If you branched a repository, you clone the repository and checkout the branch.
Forking is nothing more than a clone on the GitHub server side.


Resources to learn Git

Git Branching – Basic Branching and Merging

Useful git commands for everyday use!

Become a git guru

Happy Learning!!

Get glued to know more updates.

Have a nice day 🙂

Leave a comment

Posted by on September 14, 2019 in Uncategorized