DarkteK
DarkteK

Reputation: 881

Git pull just one commit

Actually the git repository and local files are exactly the same.

But the other website is far away from 5 commits, so I haven't pull in a while and I don't want to do it neither.

So now I wanna do some change in my local files and then push that into a new commit to git repository, THEN only be able to PULL that one commit and not all the others ... can I do this??

I don't even want to delete the commits there, just I want to be able to pull 1 commit, I hope you can help me

Upvotes: 18

Views: 43249

Answers (5)

Aryeh Beitz
Aryeh Beitz

Reputation: 2078

I had this same problem and I needed an answer that would

  1. work on any branch
  2. work in a specific case where i push several commits, do a hard reset several commits back and put back the commits one-by-one.

Cherry-pick wasn't a good solution for me, because it would show them as new commits.

Instead, I opted to use reset hard.

I also made it into an alias, so I just run git p1 and it pulls just one commit.

git config --global alias.p1 '!git fetch && git reset --hard $(git rev-list --reverse HEAD..@{u} | head -n1)'

When you reach the last commit, it does a reset hard on the last commit again.

Upvotes: 1

Gautam Krishna
Gautam Krishna

Reputation: 139

You can use git cherry-pick <commit> to do this.

Just copy the commit you want to add and it adds it. Beware: I guess, it only adds the changes of the commit, and no other in-between commits are fetched.

Upvotes: 0

nafg
nafg

Reputation: 2534

Assuming your remote is origin and your branch is master,

git fetch && git merge $(git rev-list master..origin/master | tail -n1)

This will:

  1. Run git fetch to update origin/master
  2. Get the list of commit hashes (git rev-list) from after master up to origin/master, and get the last line (tail), i.e., the first new commit
  3. Pass the result of the last step to git merge

Upvotes: 14

Matthieu Moy
Matthieu Moy

Reputation: 16497

git pull is essentially a shorthand for git fetch (download remote commits into remote-tracking branches) and then git merge (merge your HEAD, i.e. the current commit, with the ones you just downloaded).

You can get the flexibility you expect by decoupling both steps:

  • First, run git fetch, then inspect the history you just downloaded (if you work on the master branch, git fetch should have downloaded remote commits in branch origin/master which you can inspect with git log origin/master).

  • Then, merge the commit you want using git merge <commit>. Note: git merge will get all the changes in <commit> and its ancestry, so this works if <commit> is the oldest non-merged commit. If you do not have any unpushed commits, this will "fast-forward", i.e. it won't create a new commit but just advance HEAD to this commit. If you're not happy with git merge, you have other options like git rebase, git cherry-pick, ...

Upvotes: 19

chrismillah
chrismillah

Reputation: 3914

If you want to pull one commit, you can simply 'reset' to that commit..

git reset --hard <commit>

You could also make a new branch foo off that commit and pull only that branch down to your new environment. This will help maintain your codebase as you can continue to work on your original branch without having to think about affecting the new site.

git checkout -b foo

this is shorthand for

git branch foo
git checkout foo

You can then pull that branch onto whatever machine with

git clone -b *foo* http//dude@:bitbucket.org

or something like

git clone -b *foo* ssh://[email protected]/path/to/repo.git

Upvotes: 9

Related Questions