Coder Social home page Coder Social logo

git_practice's Introduction

Introduction

Git commit is nothing but a state of our files. Like saving a file after typing a document. What if we update after saving, we lose the state we saved our files last time. If we did a commit, we could go back after saving second time update. $ git checkout k32k

  • When we make a commit, it is stored with a name that provides reference to that commit, k32k in our example is that name called sha.
  • We can commit using $ git commit -a -m 'Commit message' or $git commit -a which skips a step of staging. Well staging is a temporary step, to decide, go back or forward.
  • We can always go back the git tree using previous checkout command and $ git reset --hard if files are not commited $ git reset --hard 2de2 if files are commited but this step deletes all later commits,
  • $ git checkout k32k as seen before is safer that lets return to recent commit using $ git checkout master.
  • Set up upstream if git push fails with fatal error. fatal: The current branch gh-pages has no upstream branch. git push --set-upstream origin gh-pages

Remove files from staging

$ git rm --cached * remove all files added by $ git add . $ git rm --cached name-of-file.md for single file.

Create a new Repository

git init will create a git repository on local machine on a new project. git clone will download remote repo on local machine. It creates a default link to remote repo called origin.Create a working copy of a local repo without server and Using a remote Server git clone /path/toRepo git clone username@host:/path/toRepo. After a While if we want to download the updates we need to pull code $git pull or git pull --rebase if pull and if fetch git fetch upstream and git merge REMOTE_BRANCH.

Checkout a Repo and update the copy of project

git checkout -- file.txt Undo changes to a file on working directory.Non-reversible/ cannot be undone. git reset --hard HEAD Undo changes to staging and local directory. Non-reversible/ cannot be undone.

Resetting, Checking Out and Reverting

These commands combines to affect out working directory, cached snapshots and commit history.

Resetting

On our working directory if we have changed files and committed few times and if you just want to go back just because something is wrong, so git reset is the way to restore the state to a given commit and erase all newer commits permanently.

If we are just starting and make changes to few files, but didnot commit and want to discard changes then reset --hard can reset to the last commit. $ git reset --hard Permanently wipe the newer changes, and go back steps to a stable state $ git reset --hard 766f

Checking Out

This takes us back in commit history,while newer changes are preserved.Like Sci-fiction movie, if you edit and commit, you will be in new reality, a new branch.

$ git checkout 82f5
$ git checkout master~  ### LAST COMMIT

Files

We can choose only to restore particular files and subdirectories by appending them after the command $ git checkout 82f5 some.file another.file

Select by Commit Meessage and a number of commits back using master~number

$ git checkout :/"My first b" $ git checkout master~5

Reverting

git revert will undo just the commit with the given hash. The revert is recorded as a new commit, which you can confirm by running git log. git revert sha2

Git log and git diff

$ git log

$ git diff 
$ git diff "@{yesterday}
$ git diff 1b6d "master~2"
$ git whatchanged --since="2 weeks ago"

How git fetch works

To understand how git fetch works we have to talk about how git organizes and stores commits. Behind scenes, in the ropository's ./.git/objects/ directory. All commits from local or remote branches are kept safely there,seperated through use of branch refs. Local branches inside ./.git/refs/heads directory and remote branches inside ./.git/refs/remotes directory . We can see that if we type $ git branch or $ git branch -r. The same output as we type ls ./.git/refs/heads or ls ./.git/refs/remotes. Git fetch is used to download files from remote repository from other person. We need a link to their remote repo. That link is added with

  • $ git remote add origin remote_url_of_other_person add a reference to remote repo to download or upload as origin
  • $ git fetch origin master
  • $ git merge
  • Conflicts can occur, we resolve that in our text editor by removing unwanted changes, and make another commit. We make another commit if we remove a commit using $ git revert .

Origin is a shortname for remote repository

Our remote repository on github has a name like awesome_Project, proj_xyz and if we want to connect to our remote repo we need a URL of our remote repo. So, instead of typing URL we give it a name, that is origin, we can give any other names.

  • $ git remote add origin URL_remote_awesome_Project
  • $ git remote add origin URL_remote_OpenSource_Proj
    • $ git remote add upstream URL_remote_OpenSource_Proj Git stores seperate origin for each of different remote repository.

Pull is alternative that combines fetch and merge.

  • $ git pull

Git fetch commands and options

git fetch upstream
git fetch <remote>
git fetch <remote> <branch>
git fetch --dry-run 

--dry-run option to perform a demo run ,fetch will not apply but example of output can be seen.

Pull Request

Fork > Clone > New branch > Make Changes > Push branch to Origin > Compare & Pull Req.

  • Fork from Public repo
  • git clone url
  • git checkout -b new_branch-name
  • Make changes - May be start with documentation to your open source repo
  • git push origin branch
  • Going to github repository, on right click compare and Pull request
  • Compare Local branch and Public repo master branch
  • New Pull Request.

    If maintainer of Public repo likes the changes He/She merges. Pull Request snapshot > How to make Pull Request i.e. make changes to merge with public repo

Sync your local working copy of repo with Public Repo

Once you fork and clone a project repo, You normally want to work on it in local machine. On this point in local project, we already have:

  • .git folder is already created to track changes.

  • Master branch is created and HEAD pointing it.

    local repo has remote branch origin and local has url to push master branch to github Over Time Public repo you forked your copy of working directory changes. To copy changes, an upstream is created and pulled into local working directory. And Pushed to Origin.

  • git remote add upstream url-public-repo

    Merge confilcts can happen when we pull or do a fetch. Which means editing the proposed changes and desired changes. Next steps can be done in two ways using pull or fetch/rebase.

  • git pull upstream master

or another way to pull from original project is like: add upstream snapshot

$ git fetch upstream/origin/<any remote branch>  // So the branch is downloaded
$ git checkout master  // head to master or branch
or
$ git checkout -b feature_branch
Finally merge the <remote> to <master>
$ git merge origin/master

$ git rebase upstream/master
$ git push origin master

Alternative to fetch is rebase which rewrites the commit history. So with fetch we have fetch>checkout>merge cycle to be remembered.

How to Sync your fork with Public repo

Common Workflows

  • Pull Request (Fork > Clone > Checkout Branch + Edit > Push to Remote repo = PR)
  • Sync and Develop (Fork > Clone > git remote add UPSTREAM > fetch/Rebase > Push to origin)
  • Branching commands git checkout -b new-branch creates and move to new branch. git branch -D branch to delete.
  • Merge conflicts (Editing the file from similar duplicate code sections to select one section and remove any git <--Code changes on first section->> like syntax before doing git add fileName and rebase or abort, $ git rebase --continue or --abort
  • $ git reset --hard to undo changes to last commit
  • $ git reset --hard HEAD~2 This command Moves the current branch backward by 2 commits,while
  • git revert 389004d is used to reverse a commit to a sha or undo a commit that has already been pushed to Github. Undo a commit
  • Stashing works with working directory which is saving its state while commit is saving changed state of the project. To apply stash $ git stash apply or to list stack of stash $ git stash list.
  • Sometimes a project wouldnot be pushed to remote origin because of recent changes, if you want to push and not ready to commmit just do, $ git stash. and sometimes you cannot push to remote, it happens when we pull the changes in remote to any feature branch, so to avoid this always make sure you are in master branch before a pull . so $ git checkout master followed by $ git pull or $ git pull upstream master
  • Saving a file in wordprocessing is similar to git Commit. Overwrite single file is saving a file in word processing while commit in git is about saving different states of a project by tracking multiple files and folders.
  • SVN is central tracking with remote has original copy while git is distributed control system with copies with local and remote.
  • Feature Branching Workflow This gist on git workflow, provides easy to follow guide for a project which explains git branching, checking out to remote branches, git pull while staying current to master or root directory so that staying sync with github repo makes creating features on team projects fun. Rebasing is also explained well along with alias in bash_profile.
  • To rebase your local feature branch off of the latest version of master, following these steps will be a guarantee every time.
   $ git pull                                   /* pull the latest from the remote 
   $ git checkout my-feature-branch      /* checkout the feature branch
   $ git push origin my-feature-branch  /* update your copy in the repo
   $ git rebase master              /* rebase on the master branch
   $ git push origin my-feature-branch --force   /* force update the remote

References:

Reference to first-contributions
why-should-i-use-git-instead-of-svn
git magic
git ready
Merging and rebasing.
Think like a git
Merge-conflicts

Different flags with important usage Git rebasea and EC2 AWS server connection

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.