Git ~ Saving changes - rohit120582sharma/Documentation GitHub Wiki

Developing a project revolves around the basic Edit —› Stage —› Commit pattern.

First, you edit your files in the working directory. When you’re ready to save a copy of the current state of the project, you stage changes with git add. After you’re happy with the staged snapshot, you commit it to the project history with git commit.

The commands: git add, git status, and git commit are all used in combination to save a snapshot of a Git project's current state.

The git reset command is used to undo a commit or staged snapshot.




Save

This command adds a change in the working directory to the staging area.

$ git add <file-name>

This command is used to create a snapshot of the staged changes along a timeline of a Git projects history.

$ git commit -m "<message>"

This command helps to edit/update the previous commit message.

$ git commit --amend

In addition to git add —› commit, this command is utilised to send the committed changes to remote repositories for collaboration. This enables other team members to access a set of saved changes.

$ git push <remote> <branch>


Temporarily save

Git has an additional saving mechanism called ”stash”. The stash is local to your Git repository; stashes are not transferred to the server when you push.

This command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. At this point you're free to make changes, create new commits, switch branches, and perform any other Git operations; then come back and re-apply your stash when you're ready.

$ git stash -u -a

You can re-apply previously stashed changes to your working copy with git stash pop or git stash apply. Popping your stash removes the changes from your stash and reapplies them to your working copy where Applying doesn't remove from stash.

Basic uses:

### Saving the stash
# stash changes
$ git stash

# with a reference name
$ git stash save "WIP: making progress on foo"

# keep untracked files
$ git stash --include-untracked



### List the stashes
$ git stash list



### Show the content
$ git stash show stash@{0}



### Applying the stash
# apply the last stash
$ git stash apply

# apply a specific stash
$ git stash apply stash@{0}

# grab a single file from a stash
$ git checkout <stash name> -- <filename>



### Cleaning the stash
# remove the last stash and applying changes
$ git stash pop

# remove the last stash
$ git stash drop

# remove the nth stash
$ git stash drop stash@{n}

# remove all stashes
git stash clear


Ignore

Git sees every file in your working copy as one of three things:

  • tracked - a file which has been previously staged or committed;
  • untracked - a file which has not been staged or committed; or
  • ignored - a file which Git has been explicitly told to ignore.

A Git repository can be configured to ignore specific files or directories.

Ignored files are usually build artifacts and machine generated files that can be derived from your repository source or should otherwise not be committed like /node_modules, /coverage etc.

Ignored files are tracked in a special file named .gitignore that is checked in at the root of your repository.


⚠️ **GitHub.com Fallback** ⚠️