Git Commands

Contents

  • Getting started

  • Making changes

  • Undoing changes

  • Branching

  • Working with remotes

  • Resources

Getting started

If git is not installed, download it from here: 

http://git-scm.com/download/ 

Check the version of git to confirm installation:

> git --version

git config

Show config variables:

> git config --list

Set the name to be used to label commits:

git config --global user.name <name>
> git config --global user.name "Jane Doe"

Set the email to be used to label commits:

git config --global user.email <email>
> git config --global user.email your-email@example.com

Making changes 

git init

Create a git repo:

> git init

git status

Check for changed files:

> git status

git add

Add the changed files to a staging area to be included in the commit:

> git add .

(Note: The “.”  indicates that you are adding all files in the current directory.)

Add individual files to the staging area:

git add <files...>
> git add file1.txt file2.txt

git commit

Save the staged files to the project’s history:

git commit -m <message>
> git commit -m "write your message here"

Undoing changes

git checkout

Undo the changes made to a file:

git checkout <file>
> git checkout file.txt

git reset

Remove a file from staging:

git reset HEAD <file>
> git reset HEAD file.txt

Undo the last commit:

> git reset --hard HEAD

Revert to a previous commit:

git reset <commit hash>
> git reset abc123f

git log

View the history of commits:

> git log

Branching

git branch

List branches:

> git branch

(Note: The branch you are currently on will be marked with an asterisk.)

Create a new branch:

git branch <branch name>
> git branch feature

Switch to a branch:

git checkout <branch name>
> git checkout feature

Create a branch and change to it:

git checkout -b <branch name>
> git checkout -b feature

Rename the current branch:

git branch -m <branch name>
> git branch -m feature123

Rename a different branch:

git branch -m <old name> <new name>
> git branch -m feature feature123

Delete a branch:

git branch -d <branch name>
> git branch -d feature

(Note: You cannot be on the branch that you are trying to delete.)

git merge

Merge a branch into your current branch:

git merge <current branch> <incoming branch> 
> git merge main feature

Working with Remotes

git clone

Download a remote repository:

git clone <url>
> git clone git@github.com:username/repo-name.git 

git remote

Show remote name and url:

> git remote -v

Add a remote to a project:

git remote add <remote name> <remote url>
> git remote add origin git@github.com:username/repo-name.git 

git pull

Fetch data from a remote branch and merge into the current branch:

> git pull

git push

Upload data to a remote:

git push <remote name> <branch name>
> git push origin main
Previous
Previous

Linux Commands