1

I have already committed the following files in the local Git repository:

  • dir1/file1
  • dir1/file2
  • dir2/file3
  • dir2/file4

Now I want to push only two files out of four files, i.e., dir1/file1 and dir2/fil4 to GitLab. Is it possible? If yes, what is the appropriate Git command?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mitravind
  • 51
  • 6
  • 1
    The simple answer is "no": you don't push _files_, you push _commits_. The longer answer is that there are many ways of making a _new_ commit, but they require more details about what you're trying to achieve, and are probably well documented if you read some general git tutorials. – IMSoP Oct 22 '21 at 20:48

1 Answers1

2

I think this might be a duplicate of How can I push a specific commit to a remote, and not previous commits??

I'll warn you that the rebase command that answer mentions can be a bit confusing.

Depending on why you don't want to push all of the files:

  • If you only meant to commit 2 of the 4 in the first place you can use git reset --soft <the-commit-hash-before-you-commited>. Then you can re-add only the files you want to commit. BUT if you have already pushed some of those commits to remote then you have to do a --force push, which you may not have permission to do.
  • If you never wanted those files to be committed at all, you should add them to the .gitignore so they don't come up again.
bcy
  • 56
  • 3