Git







Why Git?



https://www.atlassian.com/git/tutorials/why-git/git-for-designers


Git commands:

to add remote:git remote add <origin> url

to remove remote origin:git remote remove origin

to pull from master:git pull <origin> master



scenario:

Branches:

  • dropdown

  • develop-c1, c2(pushed already: c1 is the recent push)

  • master-I want to merge c2


create branch dropdown(dropdown will have ur c1)

git checkout develop

git reset --hard commitnum(it will delete c1.now u r at c2)

git push origin develop

git checkout master

git merge --no-ff develop

git push origin master




To get to a specific commit

git checkout -b newBranch thatCommitNum

directly we can’t do git checkout commitunm; It will detach HEAD and put you on noBranch

1.how to delete a particular commit in GIT?

git reset --hard commitnum//commit num should be the one which you want your HEAD point to

2.how to delete remote branch?

git push my_remote :my_remote_branch

(or)

git push my_remote --delete my_remote_branch

3.how to add branch in remote ?

git branch -b newBranch

git remote→ origin

git push origin newBranch

4.how to update the repository?

git fetch -p

git pull

5.how to create a new branch locally?

git checkout -b my_local_branch

6.how to push the local branch to the repository?

git push my_origin my_local_branch

7.how to delete a branch locally?

git branch -D my_local_branch


8.how to merge from remote to local branch?

update the master (remote branch)

git checkout master

git pull my_origin master

checkout to local branch

git checkout my_local_branch

merge from remote  to local

git merge my_remote/my_local_branch

8.1. How to merge other developer's changes(develop > feature_b) to our feature branch(develop > feature_a)

    To get feature_b branch to my local

            git fetch origin feature_b

            git checkout feature_b

            git pull origin feature_b

            git checkout feature_a            

            git merge feature_b

 8.2. How to stop merging?

               git merge --abort

                git reset --hard          

9.why GIT?

10.GIT vs SVN?

11.tags and version control in git

12.git stash vs git stash drop

13.git log

to show all the commits with commit message and commit a number

14.how to get all the commits?

git log

15.deleting non-recent commit, let’s assume it as second last one

git rebase -i commit_num^

(or)


git push my_remote -f

16.to delete the last commit in remote

git reset HEAD^ --hard
git push my_remote -f

to delete last commit locally

git reset --hard

17.rebase vs merge

rebase is an alternative to the merge, but there is a difference

rebase:

it is a way to cut offset of commits from a branch and apply those commits to another branch

merge:

merging new commit on top of the local branch to master


18.how to save a stash with a name?


  • git stash save “coverage levels member role”

  • to get it back,   

    • git stash apply stash^{/coverage}

  • to get all the stashes; all the stashes are stored in a stack

    • git stash list

  • To apply a stash and remove it from the stack

    • git stash pop stash@{n}

  • To apply a stash and keep it in the stack

    • git stash apply stash@{n}

  • here ‘n’ is the index of the stashed change

  • we can turn a stash into a branch if it is important

    • git stash branch <branchName> [<stash>]

    • This creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created, applies the changes recorded in <stash> to the new working tree and index, then drops the <stash> if that completes successfully. When no <stash> is given, applies the latest one.

    • This is useful if the branch on which you ran git stash save has changed enough that git stash apply fails due to conflicts. Since the stash is applied on top of the commit that was HEAD at the time the git stash was run, it restores the originally stashed state with no conflicts.

    • You can later rebase this new branch to some other place that's a descendent of where you were when you stashed.

    • [<stash>]--->>stash@{<revision>}


How to rename a local branch?

git branch -m oldName newName

How to remove untracked files:

    git clean -df

        or

    rm -r file1 file2

to show what will be deleted

    git clean -f -n 

To delete that untracked file

    git clean -f




How to edit the commit messages after push:

Through the command line:

scenario#1: commit has not been pushed online; it’s in your local

git commit --amend

https://help.github.com/articles/changing-a-commit-message

Using GUI:

There is an option above the commit message’s section

=========

to unstage staged files:

git reset HEAD [file-name-A.ext] [file-name-B.ext]

 

(or)

 

git reset

 

git add f1 f2

 

while committing only modified files without untracked stuff

 

git commit

=================


To stage a folder
git add <folderName>


git add -A

: stages all (A all)

git add .

: stages new and modified without deleted (dot without deleted)

git add -u

: stages modified and deleted without new (u without new )

 

git add file1, file2

to add specific files




To unstage files
git reset <commit> -- <path>
git reset -- README

By default, the commit parameter is optional: if you don’t specify it, it will be referring to HEAD.

So what does this command do?

This command will reset the index entries (the ones you added to your staging area) to their state at the specified commit (or HEAD if you didn’t specify any commits).

Also, we use the double dashes as argument disambiguation meaning that the argument that you are specifying may be related to two distinct objects: branches and directories for example.

To unstage all files

git reset

To unstage a specific folder from the staging area
git rm --cached -r <directory-name>





git clean -f

removes untracked files from working directory


git diff --cached - to know the staged files

git diff HEAD

git diff



To commit single file:

git commit -m 'ur commit message' ur_file_with_specified_path


===========


To list out all the branches:

git branch -v

will get local and recently used branches

git branch -av

to get even non-local branches(remote) with recent commit numbers


To get all the commits:

git log


To view specific commit details:

git show commitnum


To view the content of stash without applying it:

git stash show -p stash@{index}


To compare the 2 branches:


Case-1:File that is being compared is in the same path in both the branches

git diff master..myBranch path/to/folder


Case-2:File that is being compared is in the different path in both the branches


Case-3: To compare 2 folders/directories

branch-1→ sub→ file1, file2, file3

branch-b→ file2, file3, file4

git diff branch-a:sub branch-b


To resolve conflicts while merging in stash using git:

https://confluence.atlassian.com/bitbucket/resolve-merge-conflicts-704414003.html

Summary from the above link:

  • Got conflict while merging changes from release(source) to develop(destination)

  • Git checkout destinationBranch

  • Git pull origin destinationBranch

  • Git checkout sourceBranch

  • Git pull origin sourceBranch

  • Git merge destinationBranch

  • Now u can see the conflicts; Resolve them

  • Git add .

  • Git commit -a -m “commit message goes here!!”

  • Git push origin sourceBranch

  • Done :)

When there are conflicts sometimes it shows as 

Git unmerged paths

At that time after resolving the conflicts just add every file if you want that to be in your commit as

Git add urFile urFile2

Git commit -a -m “commit msg goes here!!!”

Git push origin urBranch


develop branch and feature branch:(Jayanthi Benefitfocus)

we want to update the feature branch

  • update the develop branch using “git pull”

  • den create a new feature branch using “git checkout -b featureBranch”

  • Apply your changes in the feature branch

  • commit and push from the feature branch

  • and then push to feature branch

  • You should not update the develop after the creation of a new feature branch otherwise we will get the error while updating



NOTE: do git fetch here we have to pull again from develop after the creation of a feature branch from Jira



Adding SSH Keys:


https://help.github.com/articles/generating-ssh-keys/

NOTE: While entering passphrase don't give anything. Just enter 

A really great reference for newbies...


Scenario:


enrollment/mako->master repo

divyaYakkala/mako->forked repo

develop is one of the branches

update develop using git pull

Go to Jira issue and create a branch


repository-->divyayakkala/mako

branch type-->feature(as .2 is already done and this is one of the patches)

branch from-->release/v.15.y.jct

branch name-->feature/wr-x/title


checkout to that branch from terminal

  git checkout feature/wr-x/title

Apply your changes

   git commit -a

It will prompt you to enter the commit message. Press Insert key

then start entering your message. Once you are done Press:wq  to save our work and quit

   after that do git push origin feature/wr-x/title

go to repo and create a pull request



After updating the feature branch:


create a pull request

add before and after links like

Before:

 ![Before](link of Jira image)

After:

 ![After](link of Jira image)



In Jira update the status

and comment with “changes are in code review stash URL: stash overview link”




To install git GUI in Linux through the command line:

sudo apt-get install git-gui 

git cherry-pick:

when u r targeted to the wrong branch

For Example, 

ee/mako - main repo

dyakkala/mako - forked repo


and fix version is: 2015.2.3-ee(.2 release->3rd patch)

after code cutoff means the targeted branch should be release not develop.

but I have targeted to develop(by branching off my feature branch from develop)

I have my changes in my feature branch which is branched off from develop and committed(with commit num-12345xx) n pushed and even created a pull request

Now I want those changes in a new branch(bugfix/) which could be branched off from release/0/15.x

So, I have created a branch from release/0.15.x.

and then “git cherry-pick 12345xx”


it will be automatically commited..no need to commit again..

just push it..and create  a pull request


Git Credentials:
To store credentials in Git bash, run below command and then git pull 
    (any command which asks for the credentials):
        git config --global credential.helper store

How to reset password in Git:
 (after below command, run git pull - any command which asks for the credentials):
      git config --global credential.helper wincred  

Git reset:

HEAD:

tip of the current branch which is the most recent commit we have made to that branch

INDEX:

There are so many contexts where we use index

  • This is the staging area

  • This is a set of files that will become the next commit

WORKING COPY:

The current set of files you are working on in your file system

SOFT:


HARD:

MIXED:


To identify the Git URL on our local:

git config --get remote.origin.url


Errors:

E: you need to resolve your current index in git

S: git reset --merge



E: Sometimes git set us on no branch

S; Means you have detached HEAD


Git vs SVN:

http://boxysystems.com/index.php/5-fundamental-differences-between-git-svn/

http://stackoverflow.com/questions/871/why-is-git-better-than-subversion

http://www.codeforest.net/git-vs-svn

To get one file(path/to/app.js) from master to develop branch





git checkout develop               # first get back to develop
git checkout master -- path/to/app.js 
OR
git show master:path/to/app.js > app.js

No comments:

Post a Comment