Git – Refresh a File or Folder from the Remote Branch
Git

1. Context

When you have messed up a file or made temporary changes locally (e.g. for debugging) and you want to get rid of the local changes, there is an easy way out. Depending on your scenario, you perhaps want to do one of the following:



  • Get the file or folder from the (remote) branch
  • Get the file or folder from another branch
  • Get the file or folder from a specific commit

In all the above cases, git checkout is the way to go. Let us explore.



2. Get a file or folder from the same (remote) branch

# Get file.txt from the remote branch
git checkout -- file.txt

# Get my-folder from the remote branch
git checkout -- my-folder

3. Get a file or folder from a different branch

# Get a file from a different branch
git checkout <another-feature-branch> -- file.txt

# Get a folder from a different branch
git checkout <another-feature-branch> -- my-folder

# When you are working on a feature branch and you want to get the content from master
git checkout master -- file.txt

4. Get a file or folder from a specific commit

# Get a file from a specified commit (hash)
git checkout <commit-hash> -- file.txt

# Get a folder from a specified commit (hash)
git checkout <commit-hash> -- my-folder
Git – Refresh a File or Folder from the Remote Branch
Be The First

Join our list to get instant access to new articles and weekly newsletter.

Tagged on:         
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x