Ashish Singh Baghel's Blog

Basic Git commands

December 09, 2019

Basic git commands

git config

Your Identity

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Checking your settings

git config --list

git config global --list
git config system --list
git config local --list

Checking specific key’s value

in your config settings

git config <key>
git config user.name

1. git help

Getting Help

git help <verb>
git <verb> --help
man git-<verb>

git help config

2. git clone

clone an existing git repository.

git clone <remote-repository-url>
git clone <remote-repository-url> <custom-folder-name>

git clone https://github.com/libgit2/libgit2

3. git init

initialize a git repository

git init

4. git status

Checking the Status of Your Files

git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

5. git add

Tracking New Files or Staging Modified files

git add <file-name>

To stage all modified files / track all new files

git add .

6. git commit

commit your changes to git repository

git commit -m "Story 182: Fix benchmarks for speed"

Skip staging area by using -a option to the git commit command, this will make Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part

git commit -a -m 'added new benchmarks'

7. git rm

Removing Files from Git. git rm removes file from working directory

git rm <file-name>
git rm PROJECTS.md

8. git mv

Moving Files. To rename a file in Git, you can run

git mv <file-from> <file-to>
git mv README.md README

Ignoring files

in git repository .gitignore file

The rules for the patterns you can put in the .gitignore file are as follows:

  • Blank lines or lines starting with # are ignored.
  • Standard glob patterns work.
  • You can end patterns with a forward slash (/) to specify a directory.
  • You can negate a pattern by starting it with an exclamation point (!). Glob patterns are like simplified regular expressions that shells use.
  • An asterisk (*) matches zero or more characters;
  • [abc] matches any character inside the brackets (in this case a, b, or c);
  • a question mark (?) matches a single character; and
  • brackets enclosing characters separated by a hyphen([0-9]) matches any character in the range (in this case 0 through 9) .

Here is an example .gitignore file:

# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
A **/ pattern is available in Git since version 1.8.2.

9. git diff

To see what you have changed but not yet staged

git diff 

To see what you have staged that will go into your next commit

git diff --cached (or git diff --staged)

10. git log

Viewing the Commit History.

git log
git log -p -2
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph
git log --since=2.weeks
git log --oneline --decorate --graph --all

11. git remote

Working with Remotes. Manage remote repositories.

Showing Your Remotes.

git remote
origin
git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)

Adding Remote Repositories.

Note: git clone command implicitly adds the origin remote for you. To add a new remote Git repository as a shortname you can reference easily

git remote add <shortname> <url>
git remote add pb https://github.com/paulboone/ticgit

Inspecting a Remote. use the following command to see more information about a particular remote

git remote show <remote>
git remote show origin

Renaming Removing Remotes Following command will rename the pb remote to paul. this changes all your remote-tracking branch names, too. pb/master is now paul/master

git remote rename pb paul

To remove a remote use following command

git remote remove paul
git remote rm paul

12. git fetch

Fetching and Pulling from Your Remotes. To get data from your remote project you can run

git fetch <remote>
git fetch origin

Note: git fetch command only downloads the data to your local repository - it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.

13. git pull

git pull command is used to automatically fetch and then merge the remote branch into your current branch. Running git pull generally fetches data from server you originally cloned from and automatically tries to merge it into the code you’re currently working on.

git pull

14. git push

Pushing to Your Remotes. push committed changes in local repository up to remote repository.

git push
git push <remote> <branch>
git push origin master

Push a new local branch to a remote git repository and track it too

git push -u origin feature_branch_name

15. git tag

Tagging.

Listing Your Tags. Listing tag wildcards requires -l or --list option

git tag
git tag -l "v1.8.5*"

Annotated Tags

git tag -a v1.4 -m "my version 1.4"

Lightweight Tags

git tag v1.4-lw

To see tag information

git show v1.4
git show 1.4-lw

Tagging Later

git tag -a <tag-name> <commit-hash>
git tag -a v1.2 9fceb02

Sharing Tags

git push origin v1.5
git push origin --tags

Deleting Tags

git tag -d v1.4-lw

To delete a remote tag

git push origin --delete <tagname>

Checkout Tags

git checkout v2.0.0
git checkout -b version2 v2.0.0

16. git branch

Branch Management.

List your current branches

git branch

To see last commit on each branch

git branch -v

The useful --merged and --no-merged options can filter the list of branches that you have not yet merged into the branch you’re currently on.

git branch --merged
git branch --no-merged

Create a new branch

git branch testing

Delete a branch

git branch -d hotfix

Delete a branch from remote repo

git branch -a
git push origin --delete hotfix

Delete an un-merged branch or delete a branch and lose the work

git branch -D testing

17. git checkout

Switching Branches

git checkout testing

Creating a new branch and switching to it at the same time

git checkout -b <newbranchname>

18. git merge

merge branch to checked out branch

git checkout master
git merge hotfix

19. git rebase

to maintain topic branches

          A---B---C topic
         /
    D---E---F---G master
git rebase master
git rebase master topic
                  A'--B'--C' topic
                 /
    D---E---F---G master

NOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topic will remain the checked-out branch.

After resolving the conflict manually and updating the index with the desired resolution, you can continue the rebasing process with

git rebase --continue

You can undo git rebase with

git rebase --abort

© 2021 Ashish Singh Baghel, Built with Gatsby