Git&Github
Git&Github
Git is the free and open source distributed version control system that's
responsible for everything GitHub related that happens locally on your
computer. This cheat sheet features the most important and commonly
used Git commands for easy reference.
SETUP
Configuring user information used across all local repositories
git config --global user.name “[firstname lastname]” - set a name
that is identifiable for credit when review version history
git config --global user.email “[valid-email]” - set an email address
that will be associated with each history marker
git config --list - lists all the details you have inputed
# git –version - gives the version of github you are using
CLONE &INIT
Configuring user information, initializing and cloning repositories
git init - initialize an existing directory as a Git repository
git clone [url] - retrieve an entire repository from a hosted location via
URL
ADD &COMMIT
git add <- file name -> - adds new or changes files in your working
directory to the git staging area.
git commit -m “some message to signify the changes” - it is the
record of the change.
PUSH COMMAND
git push -u origin main - here by default all repo in our git
hub are remote repo among from them .A specific default repo is
picked .AND THAT default repo is origin .within this orgion we are referring
to the branch named main.
MERGE CODE
(merging 2 branches into 1 branch could be like applying the changes
recommended by jr coder into the main branch by senior developer)
WAY 1
git diff <-branch name-> - to compare commits, branches, files &
more (put the name of the branch you want to compare with the branch
you are currently on)
git merge <-branch name -> - to merge 2 branches (do this by going
onto the branch whose changes you want to apply from & put the name of
the branch you want to add it to)
git push - to push the changes you have made over to remote
WAY 2
Create PR( pull request) – lets you tell others about changes you’ve
pushed to a branch in a repository on github.
Click on Compare &Pull request(branch u are on) - -> SHOWs which
branch u are copying from and branch you are copying to add
comment create new pull request (github checks whether it can
automatically merge or whether it has some conflicts in there) Click on
MERGE PULL REQUEST confirm merge confirm pull request is formed
and ust be selected.
#to apply this changes from remote(github) to local , we use pull
command
git pull origin main - used to fetch and download content from a
remote repo and immediately update the local repo to match that
cantent.
UNDOING CHANGES
Case1 : staged changes (ADD CHANGES not commited yet)
git reset <-file name-> - file names will havethe name of the file
you want to undo changes from
git reset - FOR all the undoing all the changes
Case2 : commited changes (for those who are commited once)
git reset HEAD-1 - ALL Commits are stored within the git .HEAD
refers to the latest commit you have made . head-1 refers to going one
commit back
Case3 : commited changes (for when we want to go multiple commits
back)
git reset <-commit hash -> - All commits have their particular
hash, with reset it goes back to intial commits before you made any
commits
git reset –hard <-commit hast-> -
FORK