Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Git Intro PDF

Uploaded by

Nikhil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Git Intro PDF

Uploaded by

Nikhil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Git and GitHub

1 Introduction 1.3 Commit

Assume you are developing a software application. As you Commit is a local operation that captures a snapshot of your
progress with your coding, you eventually reach a point where changes in the repository. First, add the files that you want to
you have a working version. At this stage, you would like to commit to the staging area:
save your progress as a milestone. This is crucial because as you
continue developing, you might introduce changes that cause git add .
conflicts, potentially breaking your code. If this happens, hav-
ing a saved milestone allows you to revert to a previous stable
The above command adds all the files to the staging area.
state, saving you from the frustration of fixing complex issues.
Mention name of file at the place of dot for specific ones.
Here comes Git to maintain the milestones. The process has Now, take the snapshot. When you commit, you are recording
two steps 1) staging: select which files to track, 2) commit: changes to the local repository with a message describing what
record the staged changes with a log message describing the was changed. This message helps you and others understand
current state of the code. the purpose of the changes:
In a big project when multiple programmers collaborate on
the same codebase, it is beneficial to host the code on a central
git commit -m "Initial commit"
server like GitHub. It needs to manually create an account and
repository at GitHub. Collaborators can get the code from this
repository on their local system using clone. Now they will Similar to the add command, you can use reset to remove
develop code on their local system and send updates to GitHub files from the staging area:
when reached to a milestone.
git reset file01.py

1.1 Initialisation We can use status command to get overview of which files
are staged and which are not.
Git init creates a new local repository in a folder.
git status
git init

To add remote directory with remote Now, see the log to get an overview of the commits:

git remote add git log

This command prints a unique hash related to the commit.


You can get more details of the commit by using this hash:
1.2 Get Code from Remote
git show <hash-code>
On your GitHub account, create a repository and from Clone
or Download copy the link (say <link>). Then use the follow-
ing command in your local directory to clone the repository: In case you want to see only the names of the files that were
changed without full details, use the --name-only option:
git clone <link>
git show --name-only <hash-code>
This will create a directory with the same name as the repos-
itory. Enter the newly created directory using cd and see the Another command, reflog, gives information about all com-
linkage with the remote directory using: mits on all branches:

git remote -v git reflog

1
1.4 Fetch This command combines git fetch and git merge in one
step. It fetches changes from the remote repository and imme-
The fetch command retrieves changes from the remote repos- diately merges them into your current branch.
itory to the local repository. It is used to download commits,
files, and references from a remote repository into your local
repository without merging them with your local branches. It 2 Undo and Redo Changes
updates your remote-tracking branches but does not modify
your working directory or current branches. This allows you to Locate a commit by typing git log and identify its hash. The
see what others have been working on without affecting your reversal can be done using:
local work.
Inspect the changes fetched using commands like git log git revert <hash-code>
origin/main or git diff origin/main. If you want to incor-
porate those changes into your local branch, you would typically This will create a new commit that undoes changes related
use git merge or git rebase. to the provided hash code. Another command is reset:

git reset <hash-code>


1.5 Branches
It removes all changes after the commit related to the pro-
Use the git branch command to see all the branches in the
vided hash code. There are two variations to the reset com-
project. An asterisk before the name specifies the active branch.
mand:
A new branch can be created as:
git reset --soft <hash-code>
git branch newBranchName
git reset --hard <hash-code>
Any commits that has been done on master branch after
creation of newBranchName will not be shown in newBranch- The soft reset disregards subsequent commits but keeps the
Name. Change the branch using checkout: changes they have introduced. However, the hard reset simply
disregards subsequent commits, including the changes. If no
git checkout newBranchName modifier is specified, it assumes --soft by default.
When you are only one working on the project you would
like to user reset. But, when there are multiple developers
working then you would like to do revert.
1.6 Push
The push command sends your committed changes from the
local repository to the remote server. Pushing is necessary to
3 Merging Branches
share your changes with others who have access to the remote Assume we have done some commits in newly created new-
repository: BranchName. and also in the master branch we have added
something and committed. Let us merge newBranchName to
git push master.

A modifier -f tells that the local branch is correct and that it git merge newBranchName
should force the override of the remote one.

git push -f
4 More commands
A local branch when pushed to master need to tell where to
puch the same. git diff sourceBranch targetBranch: shows the difference
between two branches.
git push -u origin newBranchName git tag: creates a sna[shot of the project at a certain time.
git stash: moves the changes of the project into a stash.
git stash pop: retrieves changes from the stash and applies
them.
1.7 Pull git rebase: takes the commits that happened in the original
branch and applies them into the current branch before the
The pull command retrieves commits from the central reposi- current changes.
tory to the local project: git clean: removes untracked files.

git pull

You might also like