0% found this document useful (0 votes)
39 views

GIT Commands

Git allows users to create branches to develop features independently from the master branch. Users can create a new branch, make changes on that branch, and then merge the branch back into master once the feature is complete. This keeps the master branch stable while still allowing independent work. Some key Git branch commands are git branch to view branches, git checkout to switch between branches, git merge to integrate one branch into another, and git push/pull to sync branches with a remote repository.

Uploaded by

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

GIT Commands

Git allows users to create branches to develop features independently from the master branch. Users can create a new branch, make changes on that branch, and then merge the branch back into master once the feature is complete. This keeps the master branch stable while still allowing independent work. Some key Git branch commands are git branch to view branches, git checkout to switch between branches, git merge to integrate one branch into another, and git push/pull to sync branches with a remote repository.

Uploaded by

Himanshu sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Git task Notes Git commands

git config --global user.name "Sam Smith"


Tell Git Configure the
git config --global user.email [email protected]
who you author name and
are email address to
be used with
your commits.
Note that
Git strips some
characters (for
example trailing
periods)
from user.name.

Create a git init


new local
repository

Check out Create a working git clone /path/to/repository


a copy of a local
repository repository:

For a remote git clone username@host:/path/to/repository


server, use:

Add files Add one or more git add <filename>


files to staging
git add *
(index):

Commit Commit changes git commit -m "Commit message"


to head (but not
yet to the remote
repository):

Commit any files git commit -a


you've added
with git add,
and also commit
any files you've
changed since
then:

Push Send changes to git push origin master


the master
branch of your
remote
repository:

Status List the files git status


you've changed
and those you
still need to add
or commit:

Connect If you haven't git remote add origin <server>


to a connected your
remote local repository
repository to a remote
server, add the
server to be able
to push to it:

List all currently git remote -v


configured
remote
repositories:

Branches Create a new git checkout -b <branchname>


branch and
switch to it:

Switch from one git checkout <branchname>


branch to
another:

List all the git branch


branches in your
repo, and also
tell you what
branch you're
currently in:

Delete the git branch -d <branchname>


feature branch:

Push the branch git push origin <branchname>


to your remote
repository, so
others can use it:
Push all branches git push --all origin
to your remote
repository:

Delete a branch git push origin :<branchname>


on your remote
repository:

Update Fetch and merge git pull


from the changes on the
remote remote server to
repository your working
directory:

To merge a git merge <branchname>


different branch
into your active
branch:

View all the git diff


merge conflicts: git diff --base <filename>
View the conflicts
against the base git diff <sourcebranch> <targetbranch>
file:

Preview changes,
before merging:

After you have git add <filename>


manually
resolved any
conflicts, you
mark the
changed file:

Tags You can use git tag 1.0.0 <commitID>


tagging to mark
a significant
changeset, such
as a release:

CommitId is the git log


leading
characters of the
changeset ID, up
to 10, but must
be unique. Get
the ID using:
Push all tags to git push --tags origin
remote
repository:

Undo If you mess up, git checkout -- <filename>


local you can replace
changes the changes in
your working
tree with the last
content in head:
Changes already
added to the
index, as well as
new files, will be
kept.

Instead, to drop git fetch origin


all your local
git reset --hard origin/master
changes and
commits, fetch
the latest history
from the server
and point your
local master
branch at it, do
this:

Search Search the git grep "foo()"


working directory
for foo():
Here's an example of how Git branches are useful. Let's say you need to work on
a new feature for a website. You create a new branch and start working. You
haven't finished your new feature, but you get a request to make a rush change
that needs to go live on the site today. You switch back to the master branch,
make the change, and push it live. Then you can switch back to your new feature
branch and finish your work. When you're done, you merge the new feature
branch into the master branch, and both the new feature and rush change are
kept!

For All the Commands Below


The commands below assume you've navigated to the folder for the Git repo.

See What Branch You're On

• Run this command:


• git status

List All Branches


NOTE: The current local branch will be marked with an asterisk (*).

• To see local branches, run this command:


• git branch
• To see remote branches, run this command:
• git branch -r
• To see all local and remote branches, run this command:
• git branch -a

Create a New Branch

• Run this command (replacing my-branch-name with whatever name you want):
• git checkout -b my-branch-name
• You're now ready to commit to this branch.

Switch to a Branch In Your Local Repo

• Run this command:


• git checkout my-branch-name

Switch to a Branch That Came From a Remote Repo

1. To get a list of all branches from the remote, run this command:
• git pull
2. Run this command to switch to the branch:
• git checkout --track origin/my-branch-name

Push to a Branch

• If your local branch does not exist on the remote, run either of these commands:
• git push -u origin my-branch-name
• git push -u origin HEAD

NOTE: HEAD is a reference to the top of the current branch, so it's an easy way
to push to a branch of the same name on the remote. This saves you from
having to type out the exact name of the branch!

• If your local branch already exists on the remote, run this command:
• git push

Merge a Branch

1. You'll want to make sure your working tree is clean and see what branch you're
on. Run this command:
• git status
2. First, you must check out the branch that you want to merge another branch into
(changes will be merged into this branch). If you're not already on the desired
branch, run this command:
• git checkout master
• NOTE: Replace master with another branch name as needed.
3. Now you can merge another branch into the current branch. Run this command:
• git merge my-branch-name
• NOTE: When you merge, there may be a conflict. Refer to Handling Merge
Conflicts (the next exercise) to learn what to do.

Delete Branches

• To delete a remote branch, run this command:


• git push origin --delete my-branch-name
• To delete a local branch, run either of these commands:
• git branch -d my-branch-name
• git branch -D my-branch-name
• NOTE: The -d option only deletes the branch if it has already been merged. The -
D option is a shortcut for --delete --force, which deletes the branch irrespective of
its merged status.

You might also like